Skip to content

Commit 7c3a34a

Browse files
committed
Merge pull request #5 from tzane/master
Up to session3 homework
2 parents e448509 + f5b6bc0 commit 7c3a34a

File tree

7 files changed

+337
-0
lines changed

7 files changed

+337
-0
lines changed

Students/Tyler G/session2/Ack.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import sys
2+
3+
def Ack(m, n):
4+
"""
5+
This is the Ackerman function
6+
which is recursive and I don't understand
7+
all that well. It returns a non-negative integer.
8+
"""
9+
if m < 0 or n < 0:
10+
return None
11+
if m == 0:
12+
return n + 1
13+
elif m > 0 and n == 0:
14+
return Ack(m-1,1)
15+
elif m > 0 and n > 0:
16+
return Ack(m-1,Ack(m,n-1))
17+
18+
def main():
19+
m_entry = int((sys.argv[1]))
20+
n_entry = int((sys.argv[2]))
21+
22+
print Ack(m_entry, n_entry)
23+
24+
if __name__ == "__main__":
25+
main()
26+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
3+
#Session 2 Extra Credit
4+
def FizzBuzzFrodoBilboGandalf(n):
5+
for i in range(1,n+1):
6+
holder = " "
7+
if i % 3 == 0:
8+
holder += "Fizz"
9+
if i % 5 == 0:
10+
holder += "Buzz"
11+
if i % 7 == 0:
12+
holder += "Frodo"
13+
if i % 11 == 0:
14+
holder += "Bilbo"
15+
if i % 13 == 0:
16+
holder += "Gandalf"
17+
if holder == " ":
18+
print i
19+
else:
20+
print holder.lstrip(" ")
21+
22+
23+
FizzBuzzFrodoBilboGandalf(100)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
def fibonacci(n):
2+
"""
3+
Returns the nth value of the fibonacci sequence.
4+
"""
5+
sequence = [0,1]
6+
for i in range(n-2):
7+
sequence.append(sequence[len(sequence)-2] + sequence[len(sequence)-1])
8+
return sequence[n-1]
9+
10+
def lucas(n):
11+
"""
12+
Returns the nth value of the lucas sequence. It is
13+
the same as the fibonacci series except the starting
14+
numbers are 2 and 1 instead of 0 and 1.
15+
"""
16+
sequence = [2,1]
17+
for i in range(n-2):
18+
sequence.append(sequence[len(sequence)-2] + sequence[len(sequence)-1])
19+
return sequence[n-1]
20+
21+
def sum_series(n, o = 0, p = 1):
22+
"""
23+
First parameter returns nth value of series. The second
24+
and third optional parameters allows the user to define
25+
the first two values in the fibonacci sequence.
26+
"""
27+
sequence = [o, p]
28+
for i in range(n-2):
29+
sequence.append(sequence[len(sequence)-2] + sequence[len(sequence)-1])
30+
return sequence[n-1]
31+
32+
def main():
33+
n_entry = int((sys.argv[1]))
34+
o_entry = int((sys.argv[2]))
35+
p_entry = int((sys.argv[3]))
36+
37+
print sum_series(n_entry, o_entry, p_entry)
38+
39+
if __name__ == "__main__":
40+
main()
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
fruits = ["Apples", "Pears", "Oranges", "Peaches"]
2+
3+
for fruit in fruits:
4+
print fruit
5+
6+
new_fruit = raw_input("What kind of fruit would you like you to add to the list? ")
7+
fruits.append(new_fruit)
8+
9+
for fruit in fruits:
10+
print fruit
11+
12+
number = raw_input("Enter a whole number: ")
13+
try:
14+
number = int(number)
15+
except ValueError:
16+
print "Must be a whole number!"
17+
18+
x = 0
19+
for i in range(number,number + len(fruits)):
20+
print "%i is to %s." % (number, fruits[x])
21+
number += 1
22+
x += 1
23+
24+
fruits.insert(0,"Bananas")
25+
26+
print "Only fruits which start with 'p':"
27+
for fruit in fruits:
28+
if fruit[0].lower() == "p":
29+
print fruit
30+
31+
print "Full list:"
32+
for fruit in fruits:
33+
print fruit
34+
35+
fruits.pop(0)
36+
37+
print "First element removed:"
38+
for fruit in fruits:
39+
print fruit
40+
41+
removed_fruit = str(raw_input("Type the name of the fruit you wish to remove from the list: "))
42+
43+
list_size = len(fruits)
44+
for fruit in fruits:
45+
if removed_fruit.lower() == fruit.lower():
46+
fruits.remove(fruit)
47+
if list_size == len(fruits):
48+
print "Could not find '%s' in the list of fruits!" % removed_fruit
49+
50+
print "Full list:"
51+
for fruit in fruits:
52+
print fruit
53+
54+
55+
for fruit in fruits:
56+
print "Do you like %s?" % fruit
57+
removed_fruits = []
58+
while True:
59+
question = raw_input("Enter 'yes' or 'no': ")
60+
if question.lower() == "no":
61+
print "Removing %s.." % fruit
62+
removed_fruits.append(fruit)
63+
break
64+
elif question.lower() == "yes":
65+
print "Keeping %s.." % fruits
66+
break
67+
else:
68+
print "You must enter 'yes' or 'no'!"
69+
for fruit in fruits:
70+
if fruit in removed_fruits:
71+
fruits.remove(fruit)
72+
73+
74+
print "Here's the list of fruits again: "
75+
for fruit in fruits:
76+
print fruit
77+
78+
new_fruits = []
79+
80+
for i in fruits:
81+
new_fruits.append(i[::-1])
82+
83+
print "Now here's the fruit list with each element reversed: "
84+
for i in new_fruits:
85+
print i
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
from textwrap import dedent
2+
import math
3+
4+
# Original list of historical donors and their amounts donated
5+
donations = []
6+
donations.append( ("Marshawn Lynch", [238.99, 159.23]) )
7+
donations.append( ("Russell Wilson", [9910.00, 159.23, 357.00]) )
8+
donations.append( ("Richard Sherman", [426.48, 3859.94, 5496.00]) )
9+
donations.append( ("Kam Chancellor", [999.99]) )
10+
donations.append( ("Bobby Wagner", [999.99]) )
11+
12+
def show_donations():
13+
x = 0
14+
print "\nThe names of previous donors are: "
15+
for person in donations:
16+
print donations[x][0]
17+
x += 1
18+
19+
def write_thank_you(name, amount):
20+
print dedent('''
21+
Dear %s,
22+
Thank you for your very kind donation of $%d.
23+
It will be put to very good use.
24+
Sincerely,
25+
-The Team
26+
''') % (name, amount)
27+
28+
def check_add_donators(name):
29+
x = 0
30+
move_on = False
31+
for person in donations:
32+
if name.lower() == donations[x][0].lower():
33+
print "\n%s is a previous donor!" % donations[x][0]
34+
choice = None
35+
while not choice:
36+
amount = raw_input("Please add another donation entry for this individual. Be sure to enter an integer or float: ")
37+
try:
38+
choice = float(amount)
39+
except ValueError:
40+
print "Must be an integer or float value!"
41+
donations[x][1].append(float(amount))
42+
write_thank_you(donations[x][0], float(amount))
43+
move_on = True
44+
x += 1
45+
if move_on == False:
46+
print "\n%s is a new donor! Adding the name to registry." % name
47+
choice = None
48+
while not choice:
49+
amount = raw_input("How much $ did this individual donate? Be sure to enter an integer or float: ")
50+
try:
51+
choice = float(amount)
52+
except ValueError:
53+
print "Must be an integer or float value!"
54+
donations.append( (name, [float(amount)]) )
55+
write_thank_you(name, float(amount))
56+
57+
def sort_key(item):
58+
return item[1]
59+
60+
def create_report():
61+
report = []
62+
for (name, amount) in donations:
63+
total = sum(amount)
64+
count = len(amount)
65+
avg = total / count
66+
report.append( (name, total, count, avg) )
67+
report.sort(key=sort_key)
68+
print "%25s | %11s | %9s | %12s" % ("Name", "Total Donated", "Freq Donated", "Avg Donated")
69+
print "-"*66
70+
x = 0
71+
for row in report:
72+
print "%25s %11.2f %12i %17.2f" % row
73+
74+
print "\n"
75+
print "*"*10, "Welcome to the Mailroom command-line script", "*"*10
76+
while True:
77+
print dedent('''
78+
Type "list" to show the names of our donors. Type "Thank You" to generate
79+
a thank-you letter to a donor. Type "Create Report" to generate a report
80+
of our donors which includes their number of donations, average donation,
81+
and total donation amounts. Finally, type "Exit" to exit the program.''')
82+
response = raw_input("\nWhich option you like to select?: ")
83+
if response.lower() == "list":
84+
show_donations()
85+
if response.lower() == "thank you":
86+
name_query = raw_input("Enter the full name of the donor: ")
87+
check_add_donators(name_query)
88+
if response.lower() == "create report":
89+
create_report()
90+
if response.lower() == "exit":
91+
print "Exiting now..."
92+
break
93+

Students/Tyler G/session3/rot13.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Returns a dictionary of the alphabet encrypted. The shift value is 13 by default.
2+
def code_shift(shift = None):
3+
if shift == None:
4+
shift = 13
5+
letters = "abcdefghijklmnopqrstuvwxyz"
6+
coder = {}
7+
x = 0
8+
for i in range(len(letters)):
9+
if i < (len(letters)-shift):
10+
coder[letters[i]] = letters[i + shift]
11+
else:
12+
coder[letters[i]] = letters[x]
13+
x += 1
14+
return coder
15+
16+
def rot13(n):
17+
encrypted = code_shift()
18+
holder = " "
19+
for char in n:
20+
if char.lower() in encrypted:
21+
if char == char.lower():
22+
holder += encrypted[char.lower()]
23+
else:
24+
holder += encrypted[char.lower()].upper()
25+
else:
26+
holder += char
27+
return holder.lstrip(" ")
28+
29+
30+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Slicing lab
2+
def switch(n):
3+
return n[-1:] + n[1:-1] + n[:1]
4+
5+
def every_other[n]
6+
return n[::2]
7+
8+
def big_string(n):
9+
return every_other(n[4:-4])
10+
11+
def big_strip2(n):
12+
return n[4:-4:2]
13+
14+
def reverse_sequence(n):
15+
return n[::-1]
16+
17+
def third_chop(n):
18+
thirds = len(n) / 3
19+
first_third = n[:thirds]
20+
middle_third = n[thirds:2*thirds]
21+
last_third = n[2*thirds:]
22+
return middle_third + last_third + first_third
23+
24+
25+
26+
# Session 3 Extra Credit
27+
def FizzBuzzAll(n, dictionary = None):
28+
if dictionary == None:
29+
dictionary = {3: "Fizz", 5: "Buzz", 7: "Frodo", 11: "Bilbo", 13: "Gandalf"}
30+
assert type(n) == int and type(dictionary) == dict, "You must insert an integer and an optional dictionary."
31+
for num in range(1, n + 1):
32+
holder = " "
33+
for value in dictionary:
34+
if num % value == 0:
35+
holder += dictionary[value]
36+
if holder != " ":
37+
print holder.lstrip(" ")
38+
else:
39+
print num
40+

0 commit comments

Comments
 (0)