Skip to content
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
23 changes: 17 additions & 6 deletions src/day-1-toy/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
# Write a function f1 that takes two integer positional arguments and returns
# the sum. This is what you'd consider to be a regular, normal function.

#def f1(...
def f1(num1, num2):
return num1+num2

print(f1(1, 2))

# Write a function f2 that takes any number of iteger arguments and prints the
# sum. Google for "python arbitrary arguments" and look for "*args"

# def f2(...
def f2(*args):
return sum(args)

print(f2(1)) # Should print 1
print(f2(1, 3)) # Should print 4
Expand All @@ -21,13 +23,17 @@
a = [7, 6, 5, 4]

# What thing do you have to add 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
# arguments. Google "python default arguments" for a hint.

#def f3(...
def f3(num1, num2=None):
if num2 is None:
return num1+1
else:
return num1+num2

print(f3(1, 2)) # Should print 3
print(f3(8)) # Should print 9
Expand All @@ -41,7 +47,11 @@
#
# Google "python keyword arguments".

#def f4(...
def f4(**kwargs):

for key, value in kwargs.items():
print(key, value)
# print("key: {}, value: {}".format(arg, kwargs[arg]))

# Should print
# key: a, value: 12
Expand All @@ -60,4 +70,5 @@
}

# What thing do you have to add to make this work?
f4(d)
f4(**d)

3 changes: 3 additions & 0 deletions src/day-1-toy/bar.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Hello line 1
How are you?
What s hot day!
3 changes: 2 additions & 1 deletion src/day-1-toy/bignum.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# Print out 2 to the 65536 power
# Print out 2 to the 65536 power
print(2**65536)
25 changes: 25 additions & 0 deletions src/day-1-toy/cal.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,28 @@
# docs for the calendar module closely.

import sys
import calendar
import datetime


year = input("Enter year: ")
month = input("Enter month: ")
now = datetime.datetime.now()

if not year and not month:
year = now.year
month = now.month
elif not year:
year = now.year
month = int(month)
elif not month:
month = now.month
year = int(year)
else:
year = int(year)
month = int(month)

def printCal(year, month):
print(calendar.month(year,month))

printCal(year, month)
12 changes: 11 additions & 1 deletion src/day-1-toy/comp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

y = []

for i in range(1,6):
y.append(i)

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 = []

for i in range(0,9):
y.append(i**3)

print(y)

# Write a list comprehension to produce the uppercase version of all the
Expand All @@ -18,6 +24,9 @@

y = []

for e in a:
y.append(e.upper())

print(y)

# Use a list comprehension to create a list containing only the _even_ elements
Expand All @@ -26,7 +35,8 @@
x = input("Enter comma-separated numbers: ").split(',')

# What do you need between the square brackets to make it work?
y = []

y = [e for e in x if int(e) % 2 == 0]

print(y)

4 changes: 2 additions & 2 deletions src/day-1-toy/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
y = "7"

# Write a print statement that combines x + y into the integer value 12
print(x + y)
print(x + int(y))

# Write a print statement that combines x + y into the string value 57
print(x + y)
print(str(x)+ y)
7 changes: 7 additions & 0 deletions src/day-1-toy/dicts.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@
"lat": 43,
"lon": -122,
"name": "a third place"
},
{
"lat": 40,
"lon": -120,
"name": "a third place"
}
]

# Write a loop that prints out all the field values for all the waypoints
for e in waypoints:
print(e["lat"], e["lon"], e["name"])

# Add a new waypoint to the list
13 changes: 11 additions & 2 deletions src/day-1-toy/fileio.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
# Use open to open file "foo.txt" for reading
f= open("foo.txt","r")

# Print all the lines in the file

contents = f.read()
print(contents)

# Close the file

f.close()

# Use open to open file "bar.txt" for writing
g= open("bar.txt","w+")

# Use the write() method to write three lines to the file
g.write('Hello line 1\n')
g.write('How are you?\n')
g.write('What s hot day!')

# Close the file
# Close the file
g.close()
6 changes: 5 additions & 1 deletion src/day-1-toy/func.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@
# Read a number from the keyboard
num = input("Enter a number: ")

# Print out "Even!" if the number is even. Otherwise print "Odd"
# Print out "Even!" if the number is even. Otherwise print "Odd"
if int(num) % 2 == 0:
print(True)
else:
print(False)
3 changes: 2 additions & 1 deletion src/day-1-toy/hello.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# Write Hello, world
# Write Hello, world
print('Hello, world')
8 changes: 7 additions & 1 deletion src/day-1-toy/lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,28 @@

# Change x so that it is [1, 2, 3, 4]
# [command here]
x.append(4)
print(x)

# Using y, change x so that it is [1, 2, 3, 4, 8, 9, 10]
# [command here]
x += y
print(x)

# Change x so that it is [1, 2, 3, 4, 9, 10]
# [command here]
x.remove(8)
print(x)

# Change x so that it is [1, 2, 3, 4, 9, 99, 10]
# [command here]
x.insert(-1,99)
print(x)

# Print the length of list x
# [command here]
print(len(x))

# Using a for loop, print all the element values multiplied by 1000
# Using a for loop, print all the element values multiplied by 1000
for num in x:
print(num*1000)
10 changes: 5 additions & 5 deletions src/day-1-toy/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@


# Print out the plaform from sys:
print()
print(sys.platform)

# Print out the Python version from sys:
print()
print(sys.version_info)



Expand All @@ -21,11 +21,11 @@
# See the docs for the OS module: https://docs.python.org/3.7/library/os.html

# Print the current process ID
print()
print(os.getpid())

# Print the current working directory (cwd):
print()
print(os.getcwd())

# Print your login name
print()
print(os.getlogin())

27 changes: 27 additions & 0 deletions src/day-1-toy/obj.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
# Make a class LatLon that can be passed parameters `lat` and `lon` to the
# constructor

class LatLon:
def __init__(self, lat, lon):
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.

class Waypoint(LatLon):
def __init__(self, name, lat, lon):
LatLon.__init__(self, lat, lon)
self.name = name

def printW(self):
return self.name, self.lat, 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?

class Geocache(Waypoint):
def __init__(self, name, difficulty, size, lat, lon):
Waypoint.__init__(self, name, lat, lon)
self.difficulty = difficulty
self.size = size

def printG(self):
return self.name, self.difficulty, self.size, self.lat, self.lon

# Make a new waypoint "Catacombs", 41.70505, -121.51521

wIns = Waypoint("Catacombs", 41.70505, -121.51521)
w = wIns.printW()
# Print it
#
# Without changing the following line, how can you make it print into something
Expand All @@ -17,5 +41,8 @@

# Make a new geocache "Newberry Views", diff 1.5, size 2, 44.052137, -121.41556

gIns = Geocache("Newberry Views", 1.5, 2, 44.052137, -121.41556)
g = gIns.printG()

# Print it--also make this print more nicely
print(g)
5 changes: 3 additions & 2 deletions src/day-1-toy/printf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Using the printf operator (%), print the following feeding in the values of x,
# y, and z:
# x is 10, y is 2.25, z is "I like turtles!"
print("x is %i, y is %.2f, z is %s" % (x, y, z))


# Use the 'format' string method to print the same thing
# Use the 'format' string method to print the same thing
print("x is {}, y is {}, z is {}".format(x, y, z))
2 changes: 2 additions & 0 deletions src/day-1-toy/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
x = 12

def changeX():
global x
x = 99

changeX()
Expand All @@ -19,6 +20,7 @@ def outer():
y = 120

def inner():
nonlocal y
y = 999

inner()
Expand Down
14 changes: 7 additions & 7 deletions src/day-1-toy/slice.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
a = [2, 4, 1, 7, 9, 6]

# Output the second element: 4:
print()
print(a[1])

# Output the second-to-last element: 9
print()
print(a[-2])

# Output the last three elements in the array: [7, 9, 6]
print()
print(a[3:])

# Output the two middle elements in the array: [1, 7]
print()
print(a[2:4])

# Output every element except the first one: [4, 1, 7, 9, 6]
print()
print(a[1:])

# Output every element except the last one: [2, 4, 1, 7, 9]
print()
print(a[:-1])

# For string s...

s = "Hello, world!"

# Output just the 8th-12th characters: "world"
print()
print(s[7:12])
Loading