Repository navigation
session 02 #29
session 02 #29
Changes from all commits
bd31bbb
e7c9508
e3c1464
44fc6f1
8bdc517
3d07bfe
5f93a6a
1131df4
9115eb7
b4102a3
b5abde1
5f85d14
4093825
f756b97
e3d1612
2611ae1
fc8ef9c
20b3773
fb05a45
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| #!/usr/bin/env python3 | ||
| def name_e(): | ||
| #name error | ||
| return alpha | ||
|
|
||
| def syntax_e(): | ||
| #syntax error | ||
| a = 't | ||
|
|
||
| def type_e(): | ||
| #type error | ||
| a = list() | ||
| b = 1 + a | ||
|
|
||
| def attribute_error(): | ||
| #attribute error | ||
| alpha ='' | ||
| alpha.append('t') |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| #!/usr/bin/env python3 | ||
| def FizzBuzz(n=100): | ||
| ''' | ||
| ''' | ||
| for i in range(1, n + 1): | ||
| if i % 15 == 0: | ||
| print('FizzBuzz') | ||
| elif i % 3 == 0: | ||
| print('Fizz') | ||
| elif i % 5 == 0: | ||
| print('Buzz') | ||
| else: | ||
| print(i) | ||
|
|
||
|
|
||
| def FizzBuzz_2(n=100): | ||
| ''' | ||
| ''' | ||
| for i in range(1, n + 1): | ||
| output = '' | ||
| if i % 3 == 0: | ||
| output += 'Fizz' | ||
| if i % 5 == 0: | ||
| output += 'Buzz' | ||
| if not output: | ||
| output = i | ||
| print(output) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| FizzBuzz() | ||
| FizzBuzz_2() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| #!/usr/bin/env python3 | ||
| def example_grid(): | ||
| ''' | ||
| Print a simple grid | ||
| ''' | ||
|
|
||
| for _ in range(2): | ||
| print('+', 4 * '-', '+', 4 * '-', '+') | ||
|
|
||
| for _ in range(4): | ||
| print('|', 4 * ' ', '|', 4 * ' ', '|') | ||
|
|
||
| print('+', 4 * '-', '+', 4 * '-', '+') | ||
|
|
||
|
|
||
| def print_grid(n): | ||
| ''' | ||
| n is the length of the line minus the plus on the side | ||
| ''' | ||
| if n < 3: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could also do this with: n = max(n, 3) |
||
| n = 3 | ||
| if n % 2 == 0: | ||
| n += 1 | ||
|
|
||
| line_a = gene_line('+', '-', n) | ||
| line_b = gene_line('|', ' ', n) | ||
|
|
||
| lines = [line_a] + [line_b for _ in range(n)] + [line_a] | ||
| # middle line | ||
| lines[(n + 1) // 2] = line_a | ||
|
|
||
| for line in lines: | ||
| print(line) | ||
|
|
||
|
|
||
| def print_grid2(m, n): | ||
| # (m number of row / columns, n cell size) | ||
| ''' | ||
| Print a grid with 2 parameters | ||
| ''' | ||
| line_a = gene_line2('+', '-', m, n) | ||
| line_b = gene_line2('|', ' ', m, n) | ||
|
|
||
| lines = [line_b for _ in range(m * n + m + 1)] | ||
| for i in range(0, len(lines), n + 1): | ||
| lines[i] = line_a | ||
|
|
||
| for line in lines: | ||
| print(line) | ||
|
|
||
|
|
||
| def gene_line(char_a, char_b, n): | ||
|
|
||
| line = char_a + ' ' + ((n - 1) // 2) * (char_b + ' ') | ||
| line = line + char_a + line[::-1] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can use += line += char_a + line[::-1] And very nice use of slicing to reverse the string! |
||
| return line | ||
|
|
||
|
|
||
| def gene_line2(char_a, char_b, m, n): | ||
|
|
||
| sub_line = char_a + ' ' + n * ( char_b + ' ' ) | ||
| line = sub_line * m + char_a | ||
| return line | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| print(repr(example_grid.__name__)) | ||
| print(repr(example_grid.__doc__)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why the repr? |
||
| example_grid() | ||
| print() | ||
|
|
||
| print(repr(print_grid.__name__)) | ||
| print(repr(print_grid.__doc__)) | ||
| print_grid(8) | ||
| print() | ||
| print_grid(20) | ||
| print() | ||
|
|
||
| print(repr(print_grid2.__name__)) | ||
| print(repr(print_grid2.__doc__)) | ||
| print_grid2(1, 1) | ||
| print() | ||
| print_grid2(2, 3) | ||
| print() | ||
| print_grid2(3, 4) | ||
| print() | ||
| print_grid2(5, 3) | ||
|
|
||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
|
|
||
| def fibonnaci(n): | ||
| ''' | ||
| Generate the nth item of the Fibonnaci serie | ||
| Fibonnaci serie has n1 = 0 and n2 = 1 with | ||
| s(n) = s(n - 2) - s(n - 1) | ||
| ''' | ||
| return sum_series(n) | ||
|
|
||
|
|
||
| def lucas(n): | ||
| ''' | ||
| Return the nth item of the Lucas Serie | ||
| Lucas serie has n1 = 2 and n2 = 1 with | ||
| s(n) = s(n - 2) - s(n - 1) | ||
| ''' | ||
| return sum_series(n, 2, 1) | ||
|
|
||
|
|
||
| def sum_series(n, n_1=0, n_2=1): | ||
| ''' | ||
| Generate the nth value of a serie of the type s(n) = s(n - 2) + s(n - 1) | ||
| with n1 and n2 known. By default n1 = 0 and n2 = 2, the first 2 values of | ||
| the Fibonnaci serie | ||
| ''' | ||
| if n == 1: | ||
| n_elem = n_1 | ||
| elif n == 2: | ||
| n_elem = n_2 | ||
| else: | ||
| n_elem = sum_series(n - 2, n_1, n_2) + sum_series(n - 1, n_1, n_2) | ||
|
|
||
| return n_elem | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| n = 15 | ||
| function_list = [fibonnaci, lucas] | ||
| m = len(function_list) | ||
| list_n = [i for i in range(1, n + 1)] | ||
|
|
||
| for j in range(m): | ||
| print(repr(function_list[j].__name__)) | ||
| print(repr(function_list[j].__doc__)) | ||
| serie_lst = [function_list[j](i) for i in list_n] | ||
| serie_str = ', '.join(map(str, serie_lst)) | ||
| print(serie_str) | ||
| print() | ||
|
|
||
| # Assert | ||
| assert fibonnaci(3) != fibonnaci(2) - fibonnaci(1), 'this is wrong' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice use of assert message. very nice job on the whole thing. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| #!/usr/bin/env python | ||
| # | ||
| # creating and modifying a list | ||
| # | ||
|
|
||
| zoo = ["monkey", "tiger", "eagle", "parrot"] | ||
|
|
||
| print "The zoo has the following", len(zoo), "animals:", zoo | ||
|
|
||
| print "The 1. animal is", zoo[0] | ||
| print "The 2. animal is", zoo[1] | ||
| print "The 1. and 2. animals are", zoo[:2] | ||
| print "Animals 2. - 4. are", zoo[1:4] | ||
| print "The animals after the 2. one are", zoo[2:] | ||
| print "The last element is", zoo[-1] | ||
|
|
||
| new_animal = raw_input("Which animal would you like to add? ") | ||
| zoo.append(new_animal) | ||
| print "The zoo has the following", len(zoo), "animals:", zoo | ||
|
|
||
| new_animal = raw_input("Which animal should replace the monkey? ") | ||
| zoo[0] = new_animal | ||
| print "The zoo has the following", len(zoo), "animals:", zoo |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| #!/usr/bin/env python3 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you are getting quite ahead of yourself (or the rest of us, anyway) nothing wrong with that, but as this is a session 3 exercise -- we will review it then. (any maybe put it in a "session 3" dir to keep things straight. I do apologize -- the first few sessions are going to feel slow for folks that have some Python experience. But not everyone is the class does have much experience with python, so we spend the first few classes on the basics to get everyone on the same page. |
||
|
|
||
|
|
||
| def rev_str(str): | ||
| return str[::-1] | ||
|
|
||
|
|
||
| def fruit_lst(): | ||
| ''' | ||
| Playing with list and fruit | ||
| ''' | ||
| fruits = ['Apple', 'Pears', 'Orange', 'Peaches'] | ||
| print(fruits) | ||
|
|
||
| new_fruit = input('Please, add a fruit to the list\n') | ||
| fruits.append(new_fruit) | ||
| print(fruits) | ||
|
|
||
| bool = True | ||
| nb_fruits = len(fruits) | ||
| while bool: | ||
| number = input('Please give a number between 1 and {}\n'.format(nb_fruits)) | ||
| try: | ||
| number = int(number) | ||
| if number <= nb_fruits: | ||
| number -= 1 | ||
| bool = False | ||
| else: | ||
| print('Not enough fruits!\n') | ||
| except: | ||
| print('Wrong input !\n') | ||
|
|
||
| print( '{} is fruit {}\n'.format(str(number + 1), fruits[number]) ) | ||
|
|
||
| fruits = ['Raspberry'] + fruits | ||
| print(fruits) | ||
|
|
||
| fruits.insert(0, 'Strawberry') | ||
| print(fruits) | ||
|
|
||
| for fruit in fruits: | ||
| if fruit[0] == 'P': | ||
| print(fruit) | ||
|
|
||
| print(fruits) | ||
| del fruits[-1] | ||
| print(fruits) | ||
|
|
||
| del_fruit = input('Please remove a fruit from the list\n') | ||
| fruits = 2 * fruits | ||
| fruits = list(filter(lambda fruit: fruit != del_fruit, fruits)) | ||
| print(fruits) | ||
|
|
||
| new_fruits = list() | ||
| for fruit in fruits: | ||
| bool = True | ||
| while bool: | ||
| like_fruit = input('Do you like {}?\n'.format(fruit.lower())) | ||
| if like_fruit == 'yes' or like_fruit == 'no': | ||
| bool = False | ||
| if like_fruit == 'yes': | ||
| new_fruits.append(fruit) | ||
| else: | ||
| print('Please type yes or no') | ||
|
|
||
| fruits = new_fruits | ||
| del new_fruits | ||
| print(fruits) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| fruit_lst() | ||
| print(rev_str('test')) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
|
|
||
| from string import ascii_lowercase, ascii_uppercase | ||
|
|
||
| def rot(n): | ||
| ''' | ||
| Generate subsitution cypher table of value n | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. very nice! I don't think I've seen this solution before. |
||
| Outcome is a tuple with the std alphabet and the rotated one | ||
| ''' | ||
| ascii_chars = [ascii_lowercase, ascii_uppercase] | ||
| rot_chars = [alphabet[n:] + alphabet[:n] for alphabet in ascii_chars] | ||
|
|
||
| return ''.join(ascii_chars), ''.join(rot_chars) | ||
|
|
||
| def translation(string, n, boolean = True): | ||
| ''' | ||
| Provide: | ||
| - translation from natural to encoded by default | ||
| - translation from encoded to natural when boolean == False | ||
| ''' | ||
| from_t, to_t = rot(n) | ||
| if boolean == False: from_t, to_t = to_t, from_t | ||
| trans_table = str.maketrans(from_t, to_t) | ||
| return string.translate(trans_table) | ||
|
|
||
| def rot13(string): | ||
| ''' | ||
| Provide translation from natural to encoded | ||
| ''' | ||
| return translation(string, 13) | ||
|
|
||
| def rot13_reverse(string): | ||
| ''' | ||
| Provide translation from encoded to natural | ||
| ''' | ||
| return translation(string, 13, False) | ||
|
|
||
| if __name__ == '__main__': | ||
| print(rot(13)) | ||
| print(type(rot(13))) | ||
| print(rot13('testo')) | ||
| print(rot13_reverse('Zntargvp sebz bhgfvqr arne pbeare')) | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a very nice solution. But do make sure you understand what ord() and chr() do as well. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice use of the underscore.