Skip to content

Commit c17c998

Browse files
authored
Merge pull request UWPCE-PythonCert#29 from guillaumethomas/master
session 02
2 parents 026e2ac + fb05a45 commit c17c998

8 files changed

Lines changed: 399 additions & 0 deletions

File tree

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')
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python3
2+
def FizzBuzz(n=100):
3+
'''
4+
'''
5+
for i in range(1, n + 1):
6+
if i % 15 == 0:
7+
print('FizzBuzz')
8+
elif i % 3 == 0:
9+
print('Fizz')
10+
elif i % 5 == 0:
11+
print('Buzz')
12+
else:
13+
print(i)
14+
15+
16+
def FizzBuzz_2(n=100):
17+
'''
18+
'''
19+
for i in range(1, n + 1):
20+
output = ''
21+
if i % 3 == 0:
22+
output += 'Fizz'
23+
if i % 5 == 0:
24+
output += 'Buzz'
25+
if not output:
26+
output = i
27+
print(output)
28+
29+
30+
if __name__ == '__main__':
31+
FizzBuzz()
32+
FizzBuzz_2()
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/env python3
2+
def example_grid():
3+
'''
4+
Print a simple grid
5+
'''
6+
7+
for _ in range(2):
8+
print('+', 4 * '-', '+', 4 * '-', '+')
9+
10+
for _ in range(4):
11+
print('|', 4 * ' ', '|', 4 * ' ', '|')
12+
13+
print('+', 4 * '-', '+', 4 * '-', '+')
14+
15+
16+
def print_grid(n):
17+
'''
18+
n is the length of the line minus the plus on the side
19+
'''
20+
if n < 3:
21+
n = 3
22+
if n % 2 == 0:
23+
n += 1
24+
25+
line_a = gene_line('+', '-', n)
26+
line_b = gene_line('|', ' ', n)
27+
28+
lines = [line_a] + [line_b for _ in range(n)] + [line_a]
29+
# middle line
30+
lines[(n + 1) // 2] = line_a
31+
32+
for line in lines:
33+
print(line)
34+
35+
36+
def print_grid2(m, n):
37+
# (m number of row / columns, n cell size)
38+
'''
39+
Print a grid with 2 parameters
40+
'''
41+
line_a = gene_line2('+', '-', m, n)
42+
line_b = gene_line2('|', ' ', m, n)
43+
44+
lines = [line_b for _ in range(m * n + m + 1)]
45+
for i in range(0, len(lines), n + 1):
46+
lines[i] = line_a
47+
48+
for line in lines:
49+
print(line)
50+
51+
52+
def gene_line(char_a, char_b, n):
53+
54+
line = char_a + ' ' + ((n - 1) // 2) * (char_b + ' ')
55+
line = line + char_a + line[::-1]
56+
return line
57+
58+
59+
def gene_line2(char_a, char_b, m, n):
60+
61+
sub_line = char_a + ' ' + n * ( char_b + ' ' )
62+
line = sub_line * m + char_a
63+
return line
64+
65+
66+
if __name__ == '__main__':
67+
print(repr(example_grid.__name__))
68+
print(repr(example_grid.__doc__))
69+
example_grid()
70+
print()
71+
72+
print(repr(print_grid.__name__))
73+
print(repr(print_grid.__doc__))
74+
print_grid(8)
75+
print()
76+
print_grid(20)
77+
print()
78+
79+
print(repr(print_grid2.__name__))
80+
print(repr(print_grid2.__doc__))
81+
print_grid2(1, 1)
82+
print()
83+
print_grid2(2, 3)
84+
print()
85+
print_grid2(3, 4)
86+
print()
87+
print_grid2(5, 3)
88+
89+
90+
91+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env python3
2+
3+
4+
def fibonnaci(n):
5+
'''
6+
Generate the nth item of the Fibonnaci serie
7+
Fibonnaci serie has n1 = 0 and n2 = 1 with
8+
s(n) = s(n - 2) - s(n - 1)
9+
'''
10+
return sum_series(n)
11+
12+
13+
def lucas(n):
14+
'''
15+
Return the nth item of the Lucas Serie
16+
Lucas serie has n1 = 2 and n2 = 1 with
17+
s(n) = s(n - 2) - s(n - 1)
18+
'''
19+
return sum_series(n, 2, 1)
20+
21+
22+
def sum_series(n, n_1=0, n_2=1):
23+
'''
24+
Generate the nth value of a serie of the type s(n) = s(n - 2) + s(n - 1)
25+
with n1 and n2 known. By default n1 = 0 and n2 = 2, the first 2 values of
26+
the Fibonnaci serie
27+
'''
28+
if n == 1:
29+
n_elem = n_1
30+
elif n == 2:
31+
n_elem = n_2
32+
else:
33+
n_elem = sum_series(n - 2, n_1, n_2) + sum_series(n - 1, n_1, n_2)
34+
35+
return n_elem
36+
37+
38+
if __name__ == '__main__':
39+
n = 15
40+
function_list = [fibonnaci, lucas]
41+
m = len(function_list)
42+
list_n = [i for i in range(1, n + 1)]
43+
44+
for j in range(m):
45+
print(repr(function_list[j].__name__))
46+
print(repr(function_list[j].__doc__))
47+
serie_lst = [function_list[j](i) for i in list_n]
48+
serie_str = ', '.join(map(str, serie_lst))
49+
print(serie_str)
50+
print()
51+
52+
# Assert
53+
assert fibonnaci(3) != fibonnaci(2) - fibonnaci(1), 'this is wrong'
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python
2+
#
3+
# creating and modifying a list
4+
#
5+
6+
zoo = ["monkey", "tiger", "eagle", "parrot"]
7+
8+
print "The zoo has the following", len(zoo), "animals:", zoo
9+
10+
print "The 1. animal is", zoo[0]
11+
print "The 2. animal is", zoo[1]
12+
print "The 1. and 2. animals are", zoo[:2]
13+
print "Animals 2. - 4. are", zoo[1:4]
14+
print "The animals after the 2. one are", zoo[2:]
15+
print "The last element is", zoo[-1]
16+
17+
new_animal = raw_input("Which animal would you like to add? ")
18+
zoo.append(new_animal)
19+
print "The zoo has the following", len(zoo), "animals:", zoo
20+
21+
new_animal = raw_input("Which animal should replace the monkey? ")
22+
zoo[0] = new_animal
23+
print "The zoo has the following", len(zoo), "animals:", zoo
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env python3
2+
3+
4+
def rev_str(str):
5+
return str[::-1]
6+
7+
8+
def fruit_lst():
9+
'''
10+
Playing with list and fruit
11+
'''
12+
fruits = ['Apple', 'Pears', 'Orange', 'Peaches']
13+
print(fruits)
14+
15+
new_fruit = input('Please, add a fruit to the list\n')
16+
fruits.append(new_fruit)
17+
print(fruits)
18+
19+
bool = True
20+
nb_fruits = len(fruits)
21+
while bool:
22+
number = input('Please give a number between 1 and {}\n'.format(nb_fruits))
23+
try:
24+
number = int(number)
25+
if number <= nb_fruits:
26+
number -= 1
27+
bool = False
28+
else:
29+
print('Not enough fruits!\n')
30+
except:
31+
print('Wrong input !\n')
32+
33+
print( '{} is fruit {}\n'.format(str(number + 1), fruits[number]) )
34+
35+
fruits = ['Raspberry'] + fruits
36+
print(fruits)
37+
38+
fruits.insert(0, 'Strawberry')
39+
print(fruits)
40+
41+
for fruit in fruits:
42+
if fruit[0] == 'P':
43+
print(fruit)
44+
45+
print(fruits)
46+
del fruits[-1]
47+
print(fruits)
48+
49+
del_fruit = input('Please remove a fruit from the list\n')
50+
fruits = 2 * fruits
51+
fruits = list(filter(lambda fruit: fruit != del_fruit, fruits))
52+
print(fruits)
53+
54+
new_fruits = list()
55+
for fruit in fruits:
56+
bool = True
57+
while bool:
58+
like_fruit = input('Do you like {}?\n'.format(fruit.lower()))
59+
if like_fruit == 'yes' or like_fruit == 'no':
60+
bool = False
61+
if like_fruit == 'yes':
62+
new_fruits.append(fruit)
63+
else:
64+
print('Please type yes or no')
65+
66+
fruits = new_fruits
67+
del new_fruits
68+
print(fruits)
69+
70+
71+
if __name__ == '__main__':
72+
fruit_lst()
73+
print(rev_str('test'))
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
from string import ascii_lowercase, ascii_uppercase
3+
4+
def rot(n):
5+
'''
6+
Generate subsitution cypher table of value n
7+
Outcome is a tuple with the std alphabet and the rotated one
8+
'''
9+
ascii_chars = [ascii_lowercase, ascii_uppercase]
10+
rot_chars = [alphabet[n:] + alphabet[:n] for alphabet in ascii_chars]
11+
12+
return ''.join(ascii_chars), ''.join(rot_chars)
13+
14+
def translation(string, n, boolean = True):
15+
'''
16+
Provide:
17+
- translation from natural to encoded by default
18+
- translation from encoded to natural when boolean == False
19+
'''
20+
from_t, to_t = rot(n)
21+
if boolean == False: from_t, to_t = to_t, from_t
22+
trans_table = str.maketrans(from_t, to_t)
23+
return string.translate(trans_table)
24+
25+
def rot13(string):
26+
'''
27+
Provide translation from natural to encoded
28+
'''
29+
return translation(string, 13)
30+
31+
def rot13_reverse(string):
32+
'''
33+
Provide translation from encoded to natural
34+
'''
35+
return translation(string, 13, False)
36+
37+
if __name__ == '__main__':
38+
print(rot(13))
39+
print(type(rot(13)))
40+
print(rot13('testo'))
41+
print(rot13_reverse('Zntargvp sebz bhgfvqr arne pbeare'))
42+

0 commit comments

Comments
 (0)