Skip to content

Commit afca745

Browse files
committed
Merge pull request UWPCE-PythonCert#81 from robertstephen/master
New Pull Request
2 parents bcda4a3 + 599eedf commit afca745

File tree

11 files changed

+288
-0
lines changed

11 files changed

+288
-0
lines changed

students/robert/README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Python code for UWPCE-PythonCert class
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def FizzBuzz(n):
2+
for i in range(1,n+1):
3+
if i % 3 == 0 and i % 5 == 0:
4+
print ("FizzBuzz")
5+
elif i % 3 == 0:
6+
print ("Fizz")
7+
elif i % 5 == 0:
8+
print ("Buzz")
9+
else:
10+
print (i)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def OneDollarGame():
2+
p = int(input("Enter # of pennies: "))
3+
n = int(input("Enter # of nickels: "))
4+
d = int(input("Enter # of dimes: "))
5+
q = int(input("Enter # of quarters: "))
6+
total = p + 5 * n + 10 * d + 25 * q
7+
if total == 100:
8+
print ("Congrats, you won 'One Dollar' Game!")
9+
elif total > 100:
10+
print ("Sorry it's more than a Dollar")
11+
else:
12+
print ("Sorry it's less than a dollar")
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env python
2+
3+
def main_command():
4+
while True:
5+
user_choice = input("Type 1 for Send a Thank You; Type 2 for Create Report: Type 3 to quit: ->")
6+
try:
7+
user_choice = int(user_choice)
8+
if user_choice not in [1, 2, 3]:
9+
print("Invalid selection. Please try again.")
10+
else:
11+
break
12+
except:
13+
print("Invalid selection. Please try again.")
14+
finally:
15+
pass
16+
return user_choice
17+
18+
19+
20+
def donor_name():
21+
for i in dln.keys():
22+
print(i)
23+
24+
def Sending_a_Thank_You():
25+
prompt = True
26+
while prompt:
27+
full_name = input("Full Name: |'list'| ->")
28+
if full_name == 'list':
29+
donor_name()
30+
prompt = True
31+
elif full_name == 'main':
32+
break
33+
elif full_name in dln.keys():
34+
prompt = False
35+
else:
36+
dln[full_name] = []
37+
prompt = False
38+
promptN = True
39+
while promptN:
40+
donation = input("Donation Amount: ->")
41+
try:
42+
val = int(donation)
43+
except ValueError:
44+
promptN == True
45+
continue
46+
else:
47+
break
48+
dln[full_name] += [int(donation)]
49+
print ("Thank you, {name}, for your generous donation of {amount}".format(name = full_name, amount = int(donation)))
50+
51+
def getKey(item):
52+
return item[1]
53+
54+
def Creating_a_Report():
55+
list = []
56+
for k,v in dln.items():
57+
list += [[k,sum(v),len(v),sum(v)/len(v)]]
58+
listN = sorted(list,key=getKey)
59+
for x in listN:
60+
print ("{DN} {TD} {ND} {ADA}".format(DN = x[0], TD = x[1], ND = x[2], ADA = x[3]))
61+
62+
63+
if __name__ == "__main__":
64+
65+
#Create a list with 5 donors and info
66+
dln = {'donor1':[10],'donor2':[20,30],'donor3':[40],'donor4':[50,60,70],'donor5':[80]}
67+
68+
while True:
69+
choice = main_command()
70+
if choice in [1]:
71+
Sending_a_Thank_You()
72+
elif choice in [2]:
73+
Creating_a_Report()
74+
else:
75+
break
76+
77+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/python
2+
3+
4+
#Find and print file locations
5+
import pathlib
6+
pth = pathlib.Path('./')
7+
abpth = pth.absolute()
8+
for fn in pth.iterdir():
9+
print(str(abpth) + '\\' + str(fn))
10+
11+
# Copy file to designated location
12+
file_name = input ('file_name with file type: ')
13+
#'classinfo.txt'
14+
out_loc = input('Copy location (/): ')
15+
#'/Python Class/Copied Files/'
16+
with open(file_name, 'r') as f:
17+
read_data = f.read()
18+
f.closed
19+
#'/Python Class/Copied Files/classinfo_copy.txt'
20+
out_file = out_loc + "/" + file_name
21+
with open(out_file, 'w') as f1:
22+
for line in read_data:
23+
f1.write(line)
24+
25+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
HW
2+
"""
3+
Write a function that has four optional parameters (with defaults):
4+
fore_color
5+
back_color
6+
link_color
7+
visited_color
8+
Have it print the colors (use strings for the colors)
9+
Call it with a couple different parameters set
10+
Have it pull the parameters out with *args, **kwargs - and print those
11+
"""
12+
13+
#function to print colors
14+
def print_color(fore_color = 'Blue', back_color = 'Green', link_color = 'Grey', visited_color = 'Black'):
15+
print(fore_color,back_color,link_color,visited_color)
16+
17+
#Test
18+
print_color('red','yellow',visited_color = 'purple',link_color = 'orange')
19+
20+
#function to pass it with args and print
21+
def print_color(args):
22+
print ("{} {} {} {}".format(*args))
23+
24+
args = ['red','yellow','orange','purple']
25+
print_color(args)
26+
27+
#function to pass it with kwargs and print
28+
def print_color(kwargs):
29+
print ("{fore_color} {back_color} {link_color} {visited_color}".format(**kwargs))
30+
31+
kwargs = {'fore_color':'red','back_color':'yellow','link_color':'orange','visited_color':'purple'}
32+
print_color(kwargs)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python
2+
3+
#import math
4+
5+
"""
6+
def line(x):
7+
function = 2*x + 5
8+
return function
9+
10+
def line(x,A,w)
11+
function = A*math.sin(w*x)
12+
return function
13+
"""
14+
15+
def quadratic(x, A=0, B=0, C=0):
16+
return A * x**2 + B * x + C
17+
18+
def trapz(quadratic,a,b,n,A,B,C):
19+
#def trapz(line,a,b):
20+
"""
21+
Compute the area under the curve defined by
22+
y = fun(x), for x between a and b
23+
24+
:param fun: the function to evaluate
25+
:type fun: a function that takes a single parameter
26+
27+
:param a: the start point for teh integration
28+
:type a: a numeric value
29+
30+
:param b: the end point for the integration
31+
:type b: a numeric value
32+
"""
33+
f1 = 0
34+
for i in range(n-1):
35+
f1 += quadratic(a+ (b-a)/n*(i + 1),A,B,C)
36+
# f1 += line(a+ (b-a)/100*(i + 1))
37+
area = (b-a)/n*((quadratic(a,A,B,C)+quadratic(b,A,B,C))/2+f1)
38+
# area = (b-a)/100*((line(a)+line(b))/2+f1)
39+
return area
40+
# pass
41+
42+
"""
43+
area = (b-a)*line
44+
return area
45+
line = line()
46+
47+
"""
48+
coef = {'A':1,'B':3,'C':2}
49+
area = trapz(quadratic,0,10,1000000,**coef)
50+
#area = trapz(line,0,10)
51+
52+
print(area)
53+
54+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#from Trapezoidal_Rule_N import quadratic
2+
#from Trapezoidal_Rule_N import trapz
3+
from Trapezoidal_Rule import quadratic
4+
from Trapezoidal_Rule import trapz
5+
6+
import math
7+
8+
def test_1():
9+
assert math.isclose(int(trapz(quadratic,0,10,1000000,1,3,2)), int((1/3)*(10**3)+(3/2)*(10**2)+2*10),rel_tol=1e-9, abs_tol=0.0) is True
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
Return True if the party with the given values is successful,
9+
or False otherwise.
10+
"""
11+
12+
def cigar_party(c,w):
13+
if w:
14+
return True
15+
else:
16+
if c >=40 and c <=60:
17+
return True
18+
else:
19+
return False
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# you can change this import to test different versions
2+
from cigar_party import cigar_party
3+
# from cigar_party import cigar_party2 as cigar_party
4+
# from cigar_party import cigar_party3 as cigar_party
5+
6+
7+
def test_1():
8+
assert cigar_party(30, False) is False
9+
10+
11+
def test_2():
12+
assert cigar_party(50, False) is True
13+
14+
15+
def test_3():
16+
assert cigar_party(70, True) is True
17+
18+
19+
def test_4():
20+
assert cigar_party(30, True) is True
21+
22+
23+
def test_5():
24+
assert cigar_party(50, True) is True
25+
26+
27+
def test_6():
28+
assert cigar_party(60, False) is True
29+
30+
31+
def test_7():
32+
assert cigar_party(61, False) is False
33+
34+
35+
def test_8():
36+
assert cigar_party(40, False) is True
37+
38+
39+
def test_9():
40+
assert cigar_party(39, False) is False
41+
42+
43+
def test_10():
44+
assert cigar_party(40, True) is True
45+
46+
47+
def test_11():
48+
assert cigar_party(39, True) is True

0 commit comments

Comments
 (0)