From 5c32e9d3eee01fe5e36b81eefe198fb7344bdda1 Mon Sep 17 00:00:00 2001 From: Johnny Ilmo Koo Date: Tue, 8 Sep 2020 10:23:15 +0900 Subject: [PATCH 01/10] Complete 06 tuples --- src/06_tuples.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/06_tuples.py b/src/06_tuples.py index 36754da73b..42bbf8fcb7 100644 --- a/src/06_tuples.py +++ b/src/06_tuples.py @@ -35,10 +35,13 @@ def dist(a, b): # Write a function `print_tuple` that prints all the values in a tuple # YOUR CODE HERE +def print_tuple(tup_input): + for item in tup_input: + print(item) t = (1, 2, 5, 7, 99) print_tuple(t) # Prints 1 2 5 7 99, one per line # Declare a tuple of 1 element then print it -u = (1) # What needs to be added to make this work? +u = (1,) # What needs to be added to make this work? print_tuple(u) From ddefc2b8d7b7cfbefee56a0ca22d9ff886280bd3 Mon Sep 17 00:00:00 2001 From: Johnny Ilmo Koo Date: Tue, 8 Sep 2020 10:43:02 +0900 Subject: [PATCH 02/10] Complete 07 slices without two in the middle --- src/07_slices.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/07_slices.py b/src/07_slices.py index 5e0b3bd8ee..071ef6d765 100644 --- a/src/07_slices.py +++ b/src/07_slices.py @@ -12,26 +12,35 @@ a = [2, 4, 1, 7, 9, 6] # Output the second element: 4: -print() +print('Output the second element: 4:') +print(a[1]) # Output the second-to-last element: 9 -print() +print('Output the second-to-last element: 9') +print(a[-2]) # Output the last three elements in the array: [7, 9, 6] -print() +print('Output the last three elements in the array: [7, 9, 6]') +print(a[-3:]) # Output the two middle elements in the array: [1, 7] -print() +print('Output the two middle elements in the array: [1, 7]') +middle = int(len(a)/2) +print(a[middle]) +# can't output two in the middle # Output every element except the first one: [4, 1, 7, 9, 6] -print() +print('Output every element except the first one: [4, 1, 7, 9, 6]') +print(a[1:]) # Output every element except the last one: [2, 4, 1, 7, 9] -print() +print('Output every element except the last one: [2, 4, 1, 7, 9]') +print(a[:-1]) # For string s... s = "Hello, world!" # Output just the 8th-12th characters: "world" -print() \ No newline at end of file +print('Output just the 8th-12th characters: "world"') +print(s[7:12]) \ No newline at end of file From fe151c25a1eae550adabdd04c53a66d0f02fa379 Mon Sep 17 00:00:00 2001 From: Johnny Ilmo Koo Date: Tue, 8 Sep 2020 10:56:25 +0900 Subject: [PATCH 03/10] Complete 08 list comprehensions --- src/08_comprehensions.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/08_comprehensions.py b/src/08_comprehensions.py index 67eb742e50..44693f726c 100644 --- a/src/08_comprehensions.py +++ b/src/08_comprehensions.py @@ -10,14 +10,14 @@ # Write a list comprehension to produce the array [1, 2, 3, 4, 5] -y = [] +y = [item for item in range(1, 6)] -print (y) +print(y) # Write a list comprehension to produce the cubes of the numbers 0-9: # [0, 1, 8, 27, 64, 125, 216, 343, 512, 729] -y = [] +y = [item**3 for item in range(10)] print(y) @@ -26,7 +26,7 @@ a = ["foo", "bar", "baz"] -y = [] +y = [item.upper() for item in a] print(y) @@ -34,8 +34,8 @@ # the user entered into list x. x = input("Enter comma-separated numbers: ").split(',') - +print(x) # What do you need between the square brackets to make it work? -y = [] +y = [int(item) for item in x if int(item) % 2 == 0] -print(y) \ No newline at end of file +print(y) From 0cee9cb245610f4c2aa0757dc34188490eab3eaa Mon Sep 17 00:00:00 2001 From: Johnny Ilmo Koo Date: Tue, 8 Sep 2020 11:03:36 +0900 Subject: [PATCH 04/10] Complete 09 dictionary --- src/09_dictionaries.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/09_dictionaries.py b/src/09_dictionaries.py index a8b2911f64..7181fb733b 100644 --- a/src/09_dictionaries.py +++ b/src/09_dictionaries.py @@ -35,13 +35,21 @@ # Add a new waypoint to the list # YOUR CODE HERE - +waypoints.append({ + "lat": 45, + "lon": -101, + "name": "a new waypoint" +}) # Modify the dictionary with name "a place" such that its longitude # value is -130 and change its name to "not a real place" # Note: It's okay to access the dictionary using bracket notation on the # waypoints list. # YOUR CODE HERE +waypoints[0]['lon'] = -130 +waypoints[0]['name'] = 'not a real place' # Write a loop that prints out all the field values for all the waypoints -# YOUR CODE HERE \ No newline at end of file +# YOUR CODE HERE +for item in waypoints: + print(item) From c5e4ad92ff4c734c9c2333caebdd1c2443c73c66 Mon Sep 17 00:00:00 2001 From: Johnny Ilmo Koo Date: Tue, 8 Sep 2020 11:08:22 +0900 Subject: [PATCH 05/10] Complete 10 functions --- src/10_functions.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/10_functions.py b/src/10_functions.py index 5830100c2c..fff5ef5f38 100644 --- a/src/10_functions.py +++ b/src/10_functions.py @@ -1,6 +1,9 @@ # Write a function is_even that will return true if the passed-in number is even. # YOUR CODE HERE +def is_even(val): + return True if val % 2 == 0 else None + # Read a number from the keyboard num = input("Enter a number: ") @@ -9,4 +12,7 @@ # Print out "Even!" if the number is even. Otherwise print "Odd" # YOUR CODE HERE - +if is_even(num): + print("Even") +else: + print("Odd") From 8cc74be0b8ef4abb9e06f1adc4faafe6dbde69a1 Mon Sep 17 00:00:00 2001 From: Johnny Ilmo Koo Date: Tue, 8 Sep 2020 11:33:37 +0900 Subject: [PATCH 06/10] Complete 11 args --- src/11_args.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/11_args.py b/src/11_args.py index 8c467ea47f..d2d1e53627 100644 --- a/src/11_args.py +++ b/src/11_args.py @@ -6,6 +6,10 @@ # YOUR CODE HERE +def f1(*arg): + return int(arg[0]) + int(arg[1]) + + print(f1(1, 2)) # Write a function f2 that takes any number of integer arguments and returns the @@ -14,6 +18,11 @@ # YOUR CODE HERE + +def f2(*args): + return sum(int(item) for item in args) + + print(f2(1)) # Should print 1 print(f2(1, 3)) # Should print 4 print(f2(1, 4, -12)) # Should print -7 @@ -22,7 +31,7 @@ a = [7, 6, 5, 4] # How do you have to modify the f2 call below to make this work? -print(f2(a)) # Should print 22 +print(f2(*a)) # Should print 22 # Write a function f3 that accepts either one or two arguments. If one argument, # it returns that value plus 1. If two arguments, it returns the sum of the @@ -31,6 +40,14 @@ # YOUR CODE HERE + +def f3(*args): + if len(args) == 1: + return args[0]+1 + elif len(args) == 2: + return args[0]+args[1] + + print(f3(1, 2)) # Should print 3 print(f3(8)) # Should print 9 @@ -44,6 +61,10 @@ # Note: Google "python keyword arguments". # YOUR CODE HERE +def f4(**kwargs): + for key in kwargs: + print(f'key: {key}, value: {kwargs[key]}') + # Should print # key: a, value: 12 @@ -62,4 +83,4 @@ } # How do you have to modify the f4 call below to make this work? -f4(d) +f4(**d) From 53e147a89aa736b11c971583717c7419087a9702 Mon Sep 17 00:00:00 2001 From: Johnny Ilmo Koo Date: Tue, 8 Sep 2020 11:44:09 +0900 Subject: [PATCH 07/10] Complete 12 scopes --- src/12_scopes.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/12_scopes.py b/src/12_scopes.py index bc467fa423..022b6db560 100644 --- a/src/12_scopes.py +++ b/src/12_scopes.py @@ -4,9 +4,11 @@ # When you use a variable in a function, it's local in scope to the function. x = 12 + def change_x(): x = 99 + change_x() # This prints 12. What do we have to modify in change_x() to get it to print 99? @@ -19,6 +21,7 @@ def outer(): y = 120 def inner(): + nonlocal y y = 999 inner() From b6e5e5b60f8558a1b6ed45a5b08555e718e6c392 Mon Sep 17 00:00:00 2001 From: Johnny Ilmo Koo Date: Tue, 8 Sep 2020 11:56:42 +0900 Subject: [PATCH 08/10] Complete 13 file io --- src/13_file_io.py | 10 +++++++++- src/bar.txt | 3 +++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 src/bar.txt diff --git a/src/13_file_io.py b/src/13_file_io.py index 3c68f8aba2..a56e12156b 100644 --- a/src/13_file_io.py +++ b/src/13_file_io.py @@ -11,9 +11,17 @@ # YOUR CODE HERE +with open("src/foo.txt", mode='r', encoding='utf-8') as f: + print(f.read()) + # Open up a file called "bar.txt" (which doesn't exist yet) for # writing. Write three lines of arbitrary content to that file, # then close the file. Open up "bar.txt" and inspect it to make # sure that it contains what you expect it to contain -# YOUR CODE HERE \ No newline at end of file +# YOUR CODE HERE +with open("src/bar.txt", mode="w+") as f: + for i in range(3): + f.write("this is line %d\r\n" % (i+1)) +with open("src/bar.txt", mode='r', encoding='utf-8') as f: + print(f.read()) diff --git a/src/bar.txt b/src/bar.txt new file mode 100644 index 0000000000..9ed92ce695 --- /dev/null +++ b/src/bar.txt @@ -0,0 +1,3 @@ +this is line 1 +this is line 2 +this is line 3 From 9e81a4374e14651028cf09e2280d310e597a702a Mon Sep 17 00:00:00 2001 From: Johnny Ilmo Koo Date: Tue, 8 Sep 2020 22:10:33 +0900 Subject: [PATCH 09/10] Complete 14 cal --- src/14_cal.py | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/14_cal.py b/src/14_cal.py index 30bb10d113..7e4daa3e2a 100644 --- a/src/14_cal.py +++ b/src/14_cal.py @@ -18,15 +18,41 @@ the format that your program expects arguments to be given. Then exit the program. -Note: the user should provide argument input (in the initial call to run the file) and not +Note: the user should provide argument input (in the initial call to run the file) and not prompted input. Also, the brackets around year are to denote that the argument is optional, as this is a common convention in documentation. -This would mean that from the command line you would call `python3 14_cal.py 4 2015` to -print out a calendar for April in 2015, but if you omit either the year or both values, +This would mean that from the command line you would call `python3 14_cal.py 4 2015` to +print out a calendar for April in 2015, but if you omit either the year or both values, it should use today’s date to get the month and year. """ import sys import calendar -from datetime import datetime \ No newline at end of file +from datetime import datetime, date + + +def get_calendar(): + c = calendar.TextCalendar() + + dt = datetime.today() + + user_input = sys.argv[1:] + + len_input = len(user_input) + + if len_input == 0: + year = dt.year + month = dt.month + elif len_input == 1: + year = dt.year + month = int(user_input[0]) + elif len_input == 2: + year = int(user_input[1]) + month = int(user_input[0]) + + str = c.formatmonth(year, month) + return str + + +print(get_calendar()) From cf910ff3bdb57d61fd0e3244ae0a1d5a6c092356 Mon Sep 17 00:00:00 2001 From: Johnny Ilmo Koo Date: Tue, 8 Sep 2020 22:44:41 +0900 Subject: [PATCH 10/10] Complete 15 classes --- src/15_classes.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/15_classes.py b/src/15_classes.py index 2355dd20b7..d98881c704 100644 --- a/src/15_classes.py +++ b/src/15_classes.py @@ -2,20 +2,47 @@ # constructor # YOUR CODE HERE +class LatLon: + def __init__(self, lat=41.705, lon=44.052): + self.lat = lat + self.lon = lon + # Make a class Waypoint that can be passed parameters `name`, `lat`, and `lon` to the # constructor. It should inherit from LatLon. Look up the `super` method. # YOUR CODE HERE +class Waypoint(LatLon): + def __init__(self, lat, lon, name='point x'): + self.name = name + super().__init__(lat, lon) + + def __str__(self): + return f'Waypont name: {self.name}, lattitude: {self.lat}, longitude: {self.lon}' # Make a class Geocache that can be passed parameters `name`, `difficulty`, # `size`, `lat`, and `lon` to the constructor. What should it inherit from? # YOUR CODE HERE + +class Geocache(Waypoint): + def __init__(self, lat, lon, name, difficulty=1, size=1): + self.difficulty = difficulty + self.size = size + super().__init__(lat, lon, name) + + def __str__(self): + return f'Geocach name: {self.name}, lat: {self.lat}, lon: {self.lon}, diff: {self.difficulty}, size: {self.size}' + # Make a new waypoint and print it out: "Catacombs", 41.70505, -121.51521 + # YOUR CODE HERE +waypoint = Waypoint(41.70505, -121.51521, "Catacombs") +print(waypoint.lat) +print(waypoint.lon) +print(waypoint.name) # Without changing the following line, how can you make it print into something # more human-readable? Hint: Look up the `object.__str__` method @@ -24,6 +51,6 @@ # Make a new geocache "Newberry Views", diff 1.5, size 2, 44.052137, -121.41556 # YOUR CODE HERE - +geocache = Geocache(44.052137, -121.41556, 'Newberry Views', 1.5, 2) # Print it--also make this print more nicely print(geocache)