Skip to content

Complete Assignment #1144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/06_tuples.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
23 changes: 16 additions & 7 deletions src/07_slices.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
print('Output just the 8th-12th characters: "world"')
print(s[7:12])
14 changes: 7 additions & 7 deletions src/08_comprehensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -26,16 +26,16 @@

a = ["foo", "bar", "baz"]

y = []
y = [item.upper() for item in a]

print(y)

# Use a list comprehension to create a list containing only the _even_ elements
# 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)
print(y)
12 changes: 10 additions & 2 deletions src/09_dictionaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
# YOUR CODE HERE
for item in waypoints:
print(item)
8 changes: 7 additions & 1 deletion src/10_functions.py
Original file line number Diff line number Diff line change
@@ -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: ")
Expand All @@ -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")
25 changes: 23 additions & 2 deletions src/11_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -62,4 +83,4 @@
}

# How do you have to modify the f4 call below to make this work?
f4(d)
f4(**d)
3 changes: 3 additions & 0 deletions src/12_scopes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -19,6 +21,7 @@ def outer():
y = 120

def inner():
nonlocal y
y = 999

inner()
Expand Down
10 changes: 9 additions & 1 deletion src/13_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
# 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())
34 changes: 30 additions & 4 deletions src/14_cal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
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())
29 changes: 28 additions & 1 deletion src/15_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
3 changes: 3 additions & 0 deletions src/bar.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
this is line 1
this is line 2
this is line 3