Skip to content

Commit a52a7d5

Browse files
committed
2 parents 2e785d6 + c17c998 commit a52a7d5

33 files changed

Lines changed: 1201 additions & 0 deletions
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def FizzBuzz(n):
2+
"""print the first n numbers, if n is a multiple of 3 it will print Fizz,
3+
if n is a multiple of 5 it will print buzz, and if it is a multiple of both it
4+
will print FizzBuzz"""
5+
for i in range(1, n + 1):
6+
word = ""
7+
if (i) % 3 == 0:
8+
word += "Fizz"
9+
if (i) % 5 == 0:
10+
word += "Buzz"
11+
if len(word) > 0:
12+
print(word)
13+
else:
14+
print(i)
15+
16+
17+
FizzBuzz(100)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
print('+', '- ' * 4, end = '')
2+
print('+', '- ' * 4, end = '',)
3+
print('+')
4+
for i in range(4):
5+
print('|', ' ' * 4, end = '')
6+
print('|', ' ' * 4, end = '')
7+
print('|')
8+
print('+', '- ' * 4, end = '')
9+
print('+', '- ' * 4, end = '',)
10+
print('+')
11+
for i in range(4):
12+
print('|', ' ' * 4, end = '')
13+
print('|', ' ' * 4, end = '')
14+
print('|')
15+
print('+', '- ' * 4, end = '')
16+
print('+', '- ' * 4, end = '',)
17+
print('+')
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def print_grid(n):
2+
halfsize = int(n/2)
3+
print(halfsize)
4+
print('+', '- ' * halfsize, end = '')
5+
print('+', '- ' * halfsize, end = '',)
6+
print('+')
7+
for i in range(halfsize):
8+
print('|', ' ' * halfsize, end = '')
9+
print('|', ' ' * halfsize, end = '')
10+
print('|')
11+
print('+', '- ' * halfsize, end = '')
12+
print('+', '- ' * halfsize, end = '',)
13+
print('+')
14+
for i in range(halfsize):
15+
print('|', ' ' * halfsize, end = '')
16+
print('|', ' ' * halfsize, end = '')
17+
print('|')
18+
print('+', '- ' * halfsize, end = '')
19+
print('+', '- ' * halfsize, end = '',)
20+
print('+')
21+
22+
print_grid(6)
23+
print_grid(0)
24+
print_grid(1)
25+
print_grid(15)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def print_columns(gridsize, cellsize):
2+
for i in range(cellsize):
3+
for n in range(gridsize):
4+
print('|', ' ' * cellsize, end = '')
5+
print('|')
6+
7+
def print_row(gridsize, cellsize):
8+
for i in range(gridsize):
9+
print('+', '- ' * cellsize, end = '')
10+
print('+')
11+
12+
def print_grid2(gridsize, cellsize):
13+
for i in range(gridsize):
14+
print_row(gridsize, cellsize)
15+
print_columns(gridsize, cellsize)
16+
print_row(gridsize, cellsize)
17+
18+
19+
print_grid2(5, 3)
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
def fibonacci(n):
2+
"""return the nth value in the fibonacci sequence"""
3+
if n < 0:
4+
return None
5+
if n == 0:
6+
return 0
7+
if n == 1:
8+
return 1
9+
else:
10+
return fibonacci(n - 1) + fibonacci(n - 2)
11+
12+
def lucas(n):
13+
"""return the nth value in the lucas series"""
14+
if n < 0:
15+
return None
16+
if n == 0:
17+
return 2
18+
if n == 1:
19+
return 1
20+
else:
21+
return lucas(n - 2) + lucas(n - 1)
22+
23+
24+
def sum_series(n, value1 = 0, value2 = 1):
25+
"""return the nth value of the given series
26+
where value1 and value2 are the first two values in the series"""
27+
if n < 0:
28+
return None
29+
if n == 0:
30+
return value1
31+
if n == 1:
32+
return value2
33+
else:
34+
return sum_series(n - 2, value1, value2) + sum_series(n - 1, value1, value2)
35+
36+
37+
38+
def lucas2(n):
39+
"""return the nth number of the lucas numbers using the function sum_series"""
40+
return sum_series(n, 2, 1)
41+
42+
def fibonacci2(n):
43+
"""return the nth number of the fibonacci sequence using the function sum_series"""
44+
return sum_series(n, 0, 1)
45+
46+
if __name__ == "__main__":
47+
# run some tests
48+
49+
assert fibonacci(-1) is None
50+
assert fibonacci(-23) is None
51+
52+
assert fibonacci(0) == 0
53+
assert fibonacci(1) == 1
54+
assert fibonacci(2) == 1
55+
assert fibonacci(3) == 2
56+
assert fibonacci(4) == 3
57+
assert fibonacci(5) == 5
58+
assert fibonacci(6) == 8
59+
assert fibonacci(7) == 13
60+
61+
assert lucas(-1) is None
62+
assert lucas(-23) is None
63+
64+
# do these with a loop:
65+
tests = [(0, 2),
66+
(1, 1),
67+
(2, 3),
68+
(3, 4),
69+
(4, 7),
70+
(5, 11),
71+
(6, 18),
72+
(7, 29),
73+
]
74+
for input, output in tests:
75+
assert lucas(input) == output
76+
77+
# test if sum_series matched Fibonacci
78+
for n in range(0, 10):
79+
assert sum_series(n) == fibonacci(n)
80+
81+
# test if sum_series matched lucas
82+
for n in range(0, 10):
83+
assert sum_series(n, 2, 1) == lucas(n)
84+
85+
# test if lucas1 matched lucas
86+
for n in range(0, 10):
87+
assert lucas2(n) == lucas(n)
88+
89+
# test if fibonacci2 matched fibonacci
90+
for n in range(0, 10):
91+
assert fibonacci2(n) == fibonacci(n)
92+
93+
print("tests passed")

‎students/TianYen/readme.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TianYen
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env python
2+
"""
3+
Print number 1..100 unless number is divisible by 3
4+
then print Fizz, or if by 5, print Buzz, or if by
5+
both 3 and 15, print FizzBuzz
6+
"""
7+
def fizz_buzz(n=100):
8+
for i in range(1,n+1):
9+
msg = "Fizz" if i%3==0 else ""
10+
msg += "Buzz" if i%5==0 else ""
11+
print(msg or i)
12+
13+
if __name__== "__main__":
14+
fizz_buzz(30)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env python
2+
3+
''' Construct grid that is composed of ndim boxes in
4+
either direction, and each box is composed of ndash
5+
lines both horizontally and vertically '''
6+
7+
### First method uses global vars and functions for lines and verts
8+
### then a caller function to print it all
9+
10+
# define global varaibles
11+
plus = "+"
12+
minus = "-"
13+
space = " "
14+
dash = '|'
15+
16+
def make_line(ndim,ndash):
17+
line = ''
18+
for n in range(ndim):
19+
line += plus + ndash*minus
20+
line += plus
21+
return line
22+
23+
def make_vert(ndim,ndash):
24+
vert = ''
25+
for n in range(ndim):
26+
vert += dash + ndash*space
27+
vert += dash
28+
return vert
29+
30+
def print_grid1(ndim,ndash):
31+
for i in range(ndim):
32+
print(make_line(ndim,ndash))
33+
for j in range(ndash):
34+
print(make_vert(ndim,ndash))
35+
print(make_line(ndim,ndash))
36+
37+
### Second method uses just one general construction function
38+
### and a caller function to print it all
39+
40+
def make_row(ndim,ndash,char1,char2):
41+
row = ''
42+
for n in range(ndim):
43+
row += char1 + ndash*char2
44+
row += char1
45+
return row
46+
47+
def print_grid(ndim,ndash):
48+
for i in range(ndim):
49+
print(make_row(ndim,ndash,'+','-'))
50+
for j in range(ndash):
51+
print(make_row(ndim,ndash,'|',' '))
52+
print(make_row(ndim,ndash,'+','-'))
53+
54+
### Now try the simple case where ndim is always 2
55+
### This is not producing the expected result for n = 11
56+
57+
def roundno(no):
58+
''' round to nearest whole integer '''
59+
return int(no//1 + ((no%1)/0.5)//1)
60+
61+
def print_grid0(n):
62+
## handle odd n by rounding to nearest int
63+
(ndim,ndash) = (2,roundno((n-1)/2))
64+
print_grid(ndim,ndash)
65+
66+
67+
if __name__=="__main__":
68+
print_grid(3,4)
69+
print_grid(5,3)
70+
print_grid(2,1)
71+
print_grid(2,7)
72+
print_grid(2,4)
73+
print_grid0(3)
74+
print_grid0(15)
75+
print_grid0(11)

‎students/eowyn/session02/series.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python
2+
"""Return fibbonaci,lucas, and generic summed series series"""
3+
4+
def fib(n):
5+
""" Print the nth element of the fibbonaci sequence"""
6+
return sum_series(n)
7+
8+
def lucas(n):
9+
""" Print the nth element of the lucas sequence"""
10+
return sum_series(n,2,1)
11+
12+
def sum_series(n,n0=0,n1=1):
13+
""" Print the nth element of the sum series sequence
14+
that begins with the elements n0 and n1"""
15+
if n == 0:
16+
return n0
17+
elif n == 1:
18+
return n1
19+
else:
20+
return sum_series(n-2,n0,n1)+sum_series(n-1,n0,n1)
21+
22+
if __name__=="__main__":
23+
print("testing fibnnoci sequence")
24+
assert fib(0)==0
25+
assert fib(1)==1
26+
assert fib(2)==1
27+
assert fib(3)==2
28+
assert fib(7)==13
29+
print("testing lucas sequence")
30+
assert lucas(0)==2
31+
assert lucas(1)==1
32+
assert lucas(5)==11
33+
assert lucas(7)==29
34+
print("testing generic sequence")
35+
assert sum_series(0)==0
36+
assert sum_series(5,2,1)==11
37+
assert sum_series(3,0,1)==2
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env python3
2+
def name_e():
3+
#name error
4+
return alpha
5+
6+
def syntax_e():
7+
#syntax error
8+
a = 't
9+
10+
def type_e():
11+
#type error
12+
a = list()
13+
b = 1 + a
14+
15+
def attribute_error():
16+
#attribute error
17+
alpha =''
18+
alpha.append('t')

0 commit comments

Comments
 (0)