Hello everyone. I would like to present you with my solutions to CS50P Problem Set 0.
Indoor Voice
I needed to implement a program in Python that prompts the user for input and then outputs it in lowercase.
Solution:
# ask the user for input
user_input = input("Input string: ")
# convert input to lowercase
user_input = user_input.lower()
# output converted input
print(user_input)
Playback Speed
I had to implement a program in Python that prompts the user or input and outputs the input, replacing each space with “…” (three full stops).
Solution:
# ask the user for input
user_input = input()
# replace whitespace with "..."
user_input = user_input.strip().replace(" ", "...")
# print result
print(user_input)
Making Faces
I replaced emoticons with emojis. Happy face emoticons were changed to slightly smiling face emojis and sad face emoticons to slightly frowning face emojis. I implemented a program that accepts a string as input and returns that input with any :)
converted to 🙂
and any :(
to 🙁
.
Solution:
# define the main function
def main():
# prompt user for input
user_input = input("Enter input: ")
# call convert on the input
user_input = convert(user_input)
# print the result
print(user_input)
# implement a function called convert
def convert(str_input):
ans = str_input.replace(":)","🙂").replace(":(","🙁")
return ans
# call the main function
main()
Einstein
I have implemented a Python program that utilizes Einstein's famous equation:
$$E = mc^2$$
In this equation, E represents Energy measured in Joules, m represents mass measured in kilograms, and c represents the speed of light measured as 300000000 meters per second, as stated by Albert Einstein. The program prompts the user to input mass as an integer value and then calculates the Energy in Joules as an integer using the given formula.
Solution:
# ask user for mass as int
mass = int(input("m: "))
# calculate joules
joules = mass * pow(300000000, 2)
# print answer
print(f"E: {joules}")
Tip Calculator
I was given a program that calculates the tip based on the meal's cost. I needed to implement the dollars_to_float and percent_to_float functions, with the main function already provided.
Solution:
def main():
dollars = dollars_to_float(input("How much was the meal? "))
percent = percent_to_float(input("What percentage would you like to tip? "))
tip = dollars * percent
print(f"Leave ${tip:.2f}")
def dollars_to_float(d):
d = d.replace("$", "")
d = float(d)
return d
def percent_to_float(p):
p = p.replace("%", "")
p = float(p)
p = p/100
return p
main()
Conclusion
I hope you found these solutions useful. Remember the journey to learning to code is all about learning different perspectives and problem-solving techniques. Don't hesitate to experiment with these solutions and explore alternative ways to approach problems, using your unique approach. Keep coding and keep learning.