Skip to content

Commit 375d7f6

Browse files
authored
Merge pull request UWPCE-PythonCert#31 from UWPCE-PythonCert/master
SESSION 6 Passing functions to functions
2 parents 265acd5 + 5e7320c commit 375d7f6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+4539
-63
lines changed

jingdai/cigar_party.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
When squirrels get together for a party, they like to have cigars.
5+
A squirrel party is successful when the number of cigars is between
6+
40 and 60, inclusive. Unless it is the weekend, in which case there
7+
is no upper bound on the number of cigars.
8+
9+
Return True if the party with the given values is successful,
10+
or False otherwise.
11+
"""
12+
13+
14+
def cigar_party(cigars, is_weekend):
15+
if is_weekend is False:
16+
if cigars>=40 and cigars <=60:
17+
return True
18+
else:
19+
return False
20+
else:
21+
if cigars>=40:
22+
return True
23+
else:
24+
return False
25+
26+
#a=cigar_party(30,True)
27+
#print(a)

jingdai/mailroom.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ def send_a_thankyou(donation):
1717

1818
if user_prompt1 in names:
1919
user_prompt2=input("Amount > ")
20+
try:
21+
user_prompt2<0
22+
except:
23+
print('Please enter a positive number')
2024
new_pair=(user_prompt1,float(user_prompt2))
2125
donation.append(new_pair)
2226
else:
@@ -46,14 +50,14 @@ def create_a_report(donation):
4650
count=1+count
4751
sum_list=(i,sum,count,float(sum/count))
4852
sum_lists.append(sum_list)
49-
53+
print(sum_lists)
5054

5155
header_line='{:>12} |{:>12} |{:>12} |{:>12} '.format("Donor Name","Total Given","Num Gifts","Average Gift")
5256
print(header_line)
5357
for sum_list in sum_lists:
5458
line_new = '{:>12} ${:>12} {:>12} ${:+.2f} '.format(*sum_list)
5559
print(line_new)
56-
return
60+
return sum_lists
5761

5862
def send_a_letter(donation):
5963
names=[] #create a list which has all the donor's name
@@ -85,13 +89,15 @@ def send_a_letter(donation):
8589

8690
if __name__ == "__main__":
8791
donation=[("Andy","Thomas", 300),("Sally", "Bui", 400),("Andy","Tom", 100),("Tom","Colin", 700),("Lisa","Dodo",90),("Abby","Lust",1000),("Abby","Lust",200)]
88-
while True:
92+
while True: #while loop if input value not in the defined value, it just go back to the first input.
8993
user_prompt=input("1.Send a Thank You; 2.Create a Report; 3.Send a Letter; 4.quit > ")
94+
if user_prompt not in '1234':
95+
print("Please select a number that is in the range of 1 to 4.")
9096

9197
if user_prompt=="2":
9298
create_a_report(donation)
9399
if user_prompt=="1":
94-
send_a_thankyou(donation)
100+
end_a_thankyou(donation)
95101
if user_prompt=="3":
96102
send_a_letter(donation)
97103
if user_prompt=="4":
@@ -101,3 +107,4 @@ def send_a_letter(donation):
101107

102108

103109

110+

jingdai/mailroom_unitest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env python
2+
3+
import mailroom
4+
5+
def test_1():
6+
donation=[("Andy","Thomas", 300),("Sally", "Bui", 400),("Andy","Tom", 100),("Tom","Colin", 700),("Lisa","Dodo",90),("Abby","Lust",1000),("Abby","Lust",200),("Abby","Lust",300)]
7+
assert mailroom.create_a_report(donation)==[('AndyThomas', 300, 1, 300.0), ('SallyBui', 400, 1, 400.0), ('AndyTom', 100, 1, 100.0), ('TomColin', 700, 1, 700.0), ('LisaDodo', 90, 1, 90.0), ('AbbyLust', 1500, 3, 500.0)]

jingdai/test_cigar_party.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
When squirrels get together for a party, they like to have cigars.
5+
A squirrel party is successful when the number of cigars is between
6+
40 and 60, inclusive. Unless it is the weekend, in which case there
7+
is no upper bound on the number of cigars.
8+
9+
Return True if the party with the given values is successful,
10+
or False otherwise.
11+
"""
12+
13+
14+
# you can change this import to test different versions
15+
from cigar_party import cigar_party
16+
# from cigar_party import cigar_party2 as cigar_party
17+
# from cigar_party import cigar_party3 as cigar_party
18+
19+
20+
def test_1():
21+
assert cigar_party(30, False) is False
22+
23+
24+
def test_2():
25+
assert cigar_party(50, False) is True
26+
27+
28+
def test_3():
29+
assert cigar_party(70, True) is True
30+
31+
32+
def test_4():
33+
assert cigar_party(30, True) is False
34+
35+
36+
def test_5():
37+
assert cigar_party(50, True) is True
38+
39+
40+
def test_6():
41+
assert cigar_party(60, False) is True
42+
43+
44+
def test_7():
45+
assert cigar_party(61, False) is False
46+
47+
48+
def test_8():
49+
assert cigar_party(40, False) is True
50+
51+
52+
def test_9():
53+
assert cigar_party(39, False) is False
54+
55+
56+
def test_10():
57+
assert cigar_party(40, True) is True
58+
59+
60+
def test_11():
61+
assert cigar_party(39, True) is False

jingdai/trigram.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
#!/usr/bin/env python3import randomdef open_file(file): with open(file,'r') as f: s=f.read() s = s.replace("\n", " ") # remove line feed s = s.replace("--"," ") s = s.replace(",","") s = s.replace(".","") s = s.replace("(","") s = s.replace(")","") return sdef build_dict(s): l=s.split(' ') d={} for i in range(len(l)-2): values=[] key=l[i]+ ' '+l[i+1] value=l[i+2] values.append(value) if key in d: for i in range(len(d[key])): # get all the elements from the value list values.append(d[key][i]) values.reverse() d[key]=values key_count={} # store number of values of a key key_list=list(d.keys()) for key in key_list: if key in d.keys(): count=len(d[key]) key_count[key]=count return (d, key_count, key_list)def trigram(d,key_count,key_list): seed=random.randint(0,len(key_list)) # choose a random number as a starting point start_point=key_list[seed-1] new_string=start_point key_point={} # store the number of how many times a key was hit key_point=key_count.copy() for key in key_point: values=0 key_point[key]=values for i in range(len(d)): nl=new_string.split(' ') new_key=nl[i] + ' '+nl[i+1] if new_key in d: if key_count[new_key]==1: new_string= new_string+ ' ' + d[new_key][0] key_point[new_key]=1 elif key_count[new_key]>1: if key_point[new_key]<key_count[new_key]: i=0 new_string= new_string+ ' ' + d[new_key][key_point[new_key]] # if a key has multiple values, then return each value one after another key_point[new_key]=(key_point[new_key]+1) else: break return(new_string)if __name__ == "__main__": file='/home/vagrant/github/Py100-2017q1/jingdai/sherlock_small.txt' try: s=open_file(file)#read file and transform file except ImportError: raise d, key_count, key_list=build_dict(s) string=trigram(d, key_count, key_list) print(string)
1+
#!/usr/bin/env python3import randomdef open_file(file): with open(file,'r') as f: s=f.read() s = s.replace("\n", " ") # remove line feed s = s.replace("--"," ") s = s.replace(",","") s = s.replace(".","") s = s.replace("(","") s = s.replace(")","") return sdef build_dict(s): l=s.split(' ') d={} for i in range(len(l)-2): values=[] key=l[i]+ ' '+l[i+1] value=l[i+2] values.append(value) if key in d: for i in range(len(d[key])): # get all the elements from the value list values.append(d[key][i]) values.reverse() d[key]=values key_count={} # store number of values of a key key_list=list(d.keys()) for key in key_list: if key in d.keys(): count=len(d[key]) key_count[key]=count return (d, key_count, key_list)def trigram(d,key_count,key_list): seed=random.randint(0,len(key_list)) # choose a random number as a starting point start_point=key_list[seed-1] new_string=start_point key_point={} # store the number of how many times a key was hit key_point=key_count.copy() for key in key_point: values=0 key_point[key]=values for i in range(len(d)): nl=new_string.split(' ') new_key=nl[i] + ' '+nl[i+1] if new_key in d: if key_count[new_key]==1: new_string= new_string+ ' ' + d[new_key][0] key_point[new_key]=1 elif key_count[new_key]>1: if key_point[new_key]<key_count[new_key]: i=0 new_string= new_string+ ' ' + d[new_key][key_point[new_key]] # if a key has multiple values, then return each value one after another key_point[new_key]=(key_point[new_key]+1) else: break return(new_string)if __name__ == "__main__": file='/home/vagrant/github/Py100-2017q1/jingdai/sherlock_small.txt' try: f = open(file) except OSError: print("file is not there!!") raise s=open_file(file)#read file and transform file d, key_count, key_list=build_dict(s) string=trigram(d, key_count, key_list) print(string)

jingdai/trigram_2.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env python3
2+
import random
3+
4+
def open_file(file):
5+
6+
with open(file,'r') as f:
7+
s=f.read()
8+
s = s.replace("\n", " ") # remove line feed
9+
s = s.replace("--"," ")
10+
s = s.replace(",","")
11+
s = s.replace(".","")
12+
s = s.replace("(","")
13+
s = s.replace(")","")
14+
15+
return s
16+
17+
18+
def build_dict(s):
19+
l=s.split(' ')
20+
d={}
21+
for i in range(len(l)-2):
22+
values=[]
23+
key=l[i]+ ' '+l[i+1]
24+
value=l[i+2]
25+
values.append(value)
26+
if key in d:
27+
for i in range(len(d[key])): # get all the elements from the value list
28+
values.append(d[key][i])
29+
values.reverse()
30+
d[key]=values
31+
32+
key_count={} # store number of values of a key
33+
key_list=list(d.keys())
34+
for key in key_list:
35+
if key in d.keys():
36+
count=len(d[key])
37+
key_count[key]=count
38+
39+
return (d, key_count, key_list)
40+
41+
def trigram(d,key_count,key_list):
42+
seed=random.randint(0,len(key_list)) # choose a random number as a starting point
43+
start_point=key_list[seed-1]
44+
new_string=start_point
45+
key_point={} # store the number of how many times a key was hit
46+
key_point=key_count.copy()
47+
for key in key_point:
48+
values=0
49+
key_point[key]=values
50+
51+
for i in range(len(d)):
52+
53+
nl=new_string.split(' ')
54+
new_key=nl[i] + ' '+nl[i+1]
55+
if new_key in d:
56+
if key_count[new_key]==1:
57+
new_string= new_string+ ' ' + d[new_key][0]
58+
key_point[new_key]=1
59+
elif key_count[new_key]>1:
60+
if key_point[new_key]<key_count[new_key]:
61+
i=0
62+
new_string= new_string+ ' ' + d[new_key][key_point[new_key]] # if a key has multiple values, then return each value one after another
63+
key_point[new_key]=(key_point[new_key]+1)
64+
else:
65+
break
66+
67+
return(new_string)
68+
69+
70+
if __name__ == "__main__":
71+
file='/home/vagrant/github/Py100-2017q1/jingdai/sherlock_small.txt'
72+
try:
73+
f = open(file)
74+
except OSError:
75+
print("file is not there!!")
76+
raise
77+
78+
79+
s=open_file(file)#read file and transform file
80+
d, key_count, key_list=build_dict(s)
81+
string=trigram(d, key_count, key_list)
82+
print(string)
83+
84+
85+

mbozee/mailroom_ui/Procfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: gunicorn app:app

mbozee/mailroom_ui/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Deployed on Heroku: http://mailroomapp.herokuapp.com/
2+
3+
Uses Python to generate fictitious donation data. Flask deployed on Heroku.

mbozee/mailroom_ui/app.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from flask import Flask, render_template
2+
3+
app = Flask(__name__)
4+
5+
6+
@app.route("/")
7+
def index():
8+
donors = {
9+
'Donovan Leitch': [100, 20, 1000],
10+
'Stevie Nicks': [200, 500],
11+
'Ian Anderson': [190],
12+
'Mavis Staples': [170, 40, 130],
13+
'Suzanne Vega': [160, 300]
14+
}
15+
16+
donors_sorted = sorted(donors.items(),
17+
key=lambda t: sum(t[1]), reverse=True)
18+
19+
for donor in donors_sorted:
20+
donor_name = donor[0]
21+
total_given = sum(donor[1])
22+
num_gifts = len(donor[1])
23+
avg_gift = round(sum(donor[1])/len(donor[1]))
24+
25+
return render_template("index.html", donors=donors, donors_sorted=donors_sorted, donor_name=donor_name, total_given=total_given, num_gifts=num_gifts, avg_gift=avg_gift)
26+
27+
28+
if __name__ == "__main__":
29+
app.run(debug=True, port=8000, host='0.0.0.0')

0 commit comments

Comments
 (0)