Skip to content

Commit f5b6bc0

Browse files
committed
Add session3 HW
1 parent a6c846b commit f5b6bc0

File tree

4 files changed

+248
-0
lines changed

4 files changed

+248
-0
lines changed
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)