File tree 12 files changed +215
-0
lines changed
12 files changed +215
-0
lines changed Original file line number Diff line number Diff line change
1
+ # ignore emacs's backup files
2
+ * ~
Original file line number Diff line number Diff line change
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 ()
Original file line number Diff line number Diff line change
1
+ #! /bin/sh -e
2
+
3
+ check50 --local cs50/problems/2022/python/bank
Original file line number Diff line number Diff line change
1
+ #! /bin/sh -e
2
+
3
+ check50 --local cs50/problems/2022/python/deep
Original file line number Diff line number Diff line change
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 ()
Original file line number Diff line number Diff line change
1
+ #! /bin/sh -e
2
+
3
+ check50 --local cs50/problems/2022/python/extensions
Original file line number Diff line number Diff line change
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 ()
Original file line number Diff line number Diff line change
1
+ #! /bin/sh -e
2
+
3
+ check50 --local cs50/problems/2022/python/interpreter
Original file line number Diff line number Diff line change
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 ()
Original file line number Diff line number Diff line change
1
+ #! /bin/sh -e
2
+
3
+ check50 --local cs50/problems/2022/python/meal
Original file line number Diff line number Diff line change
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 ()
Original file line number Diff line number Diff line change
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 ()
You can’t perform that action at this time.
0 commit comments