Skip to content

Commit 9d195c6

Browse files
author
Ali Bozorgzadeh
committed
Add week1; update .gitignore
1 parent 2887e99 commit 9d195c6

File tree

12 files changed

+215
-0
lines changed

12 files changed

+215
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# ignore emacs's backup files
2+
*~

week1/bank/bank.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# $0 : hello, hello there, ...
2+
# $20 : hey, hey there, ...
3+
# $100: not starting with h
4+
5+
# Resources: https://docs.python.org/3.10/library/stdtypes.html#str.startswith
6+
7+
8+
def main():
9+
# Prompt the user for greeting
10+
greeting = input("Greeting: ").lstrip().lower()
11+
12+
# Evaluate the user's input
13+
print("$", greet_eval(greeting), sep='')
14+
15+
16+
def greet_eval(str) -> int:
17+
if str.startswith('h'):
18+
if str.startswith("hello"):
19+
return 0
20+
return 20
21+
else:
22+
return 100
23+
24+
25+
main()

week1/bank/check.sh

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh -e
2+
3+
check50 --local cs50/problems/2022/python/bank

week1/deep/check.sh

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh -e
2+
3+
check50 --local cs50/problems/2022/python/deep

week1/deep/deep.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Let the user know if they know the "meaning of life, universe and
2+
# everything"!
3+
4+
5+
def main():
6+
# Prompt user for meaning of life
7+
mof = input("Meaning of Life: ").lower().strip()
8+
9+
# Respond to user's answer
10+
if mof == "42" or mof == "forty-two" or mof == "forty two":
11+
print("Yes")
12+
else:
13+
print("No")
14+
15+
16+
main()

week1/extensions/check.sh

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh -e
2+
3+
check50 --local cs50/problems/2022/python/extensions

week1/extensions/extensions.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# A program that prompts the user for the name of a file and then
2+
# outputs that file's media type.
3+
4+
# Resources: https://docs.python.org/3.10/library/stdtypes.html#str.endswith
5+
6+
7+
def main():
8+
# Ask the user for file name
9+
filename = input("File: ").strip()
10+
11+
# Determine the MIME type
12+
print(mime_type(filename))
13+
14+
15+
def mime_type(filename: str) -> str:
16+
fn_lower = filename.lower()
17+
18+
if fn_lower.endswith(".gif"):
19+
return "image/gif"
20+
elif fn_lower.endswith((".jpg", ".jpeg")):
21+
return "image/jpeg"
22+
elif fn_lower.endswith(".png"):
23+
return "image/png"
24+
elif fn_lower.endswith(".pdf"):
25+
return "application/pdf"
26+
elif fn_lower.endswith(".txt"):
27+
return "text/plain"
28+
elif fn_lower.endswith(".zip"):
29+
return "application/zip"
30+
else:
31+
return "application/octet-stream"
32+
33+
34+
main()

week1/interpreter/check.sh

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh -e
2+
3+
check50 --local cs50/problems/2022/python/interpreter

week1/interpreter/interpreter.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Math interpreter
2+
3+
4+
5+
def main():
6+
statement = input("> ")
7+
print(intrprt(statement))
8+
9+
10+
def intrprt(statement: str) -> int:
11+
lhs, op, rhs = statement.strip().split()
12+
13+
if op == "+":
14+
return float(lhs) + float(rhs)
15+
elif op == "-":
16+
return float(lhs) - float(rhs)
17+
elif op == "/":
18+
return float(lhs) / float(rhs)
19+
elif op == "*":
20+
return float(lhs) * float(rhs)
21+
else:
22+
return None
23+
24+
25+
main()

week1/meal/check.sh

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh -e
2+
3+
check50 --local cs50/problems/2022/python/meal

week1/meal/meal.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Meal Time:
2+
#
3+
# Notify's the user whether they need to eat in the given time of the day
4+
5+
6+
7+
def main():
8+
# Prompt the user for time
9+
time = input("Time: ").strip()
10+
11+
# Get the numerical 24 hours value of the time
12+
time = convert(time)
13+
14+
# Notify the user
15+
if convert("7:00") <= time <= convert("8:00"):
16+
print("breakfast time")
17+
elif convert("12:00") <= time <= convert("13:00"):
18+
print("lunch time")
19+
elif convert("18:00") <= time <= convert("19:00"):
20+
print("dinner time")
21+
22+
23+
def convert(time: str) -> float:
24+
"""
25+
converts time into a numerical value in 24-hour times
26+
27+
example: 13:30 --> 13.5
28+
"""
29+
30+
hours, minutes = time.split(':')
31+
32+
# There are 60 minutes in 1 hour
33+
return float(hours) + (float(minutes) / 60)
34+
35+
36+
main()

week1/meal/meal_12hr.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Meal Time:
2+
#
3+
# Notify's the user whether they need to eat in the given time of the day
4+
#
5+
# This version supports 12-hour times too
6+
7+
8+
def main():
9+
# Prompt the user for time
10+
time = input("Time: ").strip()
11+
12+
# Get the numerical 24 hours value of the time
13+
time = convert(time)
14+
15+
# Notify the user
16+
if convert("7:00") <= time <= convert("8:00"):
17+
print("breakfast time")
18+
elif convert("12:00") <= time <= convert("13:00"):
19+
print("lunch time")
20+
elif convert("18:00") <= time <= convert("19:00"):
21+
print("dinner time")
22+
23+
24+
def convert(time: str) -> float:
25+
"""
26+
converts time into a numerical value in 24-hour times
27+
example: 13:30 -> 13.5
28+
example: 12:30 a.m. -> 0.5
29+
example: 12:30 p.m. -> 12.5
30+
"""
31+
32+
time = time.strip()
33+
34+
# 24-hour times
35+
if not is12hr(time):
36+
hours, minutes = time.split(':')
37+
return float(hours) + (float(minutes) / 60)
38+
39+
# 12-hours times
40+
time, format = time.split()
41+
hours, minutes = time.split(':')
42+
hourse = float(hours)
43+
minutes = float(minutes)
44+
45+
if format == "p.m.":
46+
if hours == 12:
47+
return 12 + (minutes / 60)
48+
else:
49+
return 12 + hours + (minutes / 60)
50+
else:
51+
if hours == 12:
52+
return minutes / 60
53+
else:
54+
return hours + (minutes / 60)
55+
56+
57+
def is12hr(time: str) -> bool:
58+
if time.find("a.m.") != -1 or time.find("p.m") != -1:
59+
return True
60+
61+
62+
main()

0 commit comments

Comments
 (0)