Skip to content

Commit c7eaaa7

Browse files
committed
Added session01 and sessio
1 parent 8c93ca5 commit c7eaaa7

5 files changed

Lines changed: 198 additions & 0 deletions

File tree

‎Students/michel/session01/grid.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Spyder Editor
4+
5+
This is a temporary script file.
6+
"""
7+
8+
def grid(nbCol, nbRow, size):
9+
10+
''' Draws a grid nbCol wide and nbRow tall
11+
nbCol and nbRow are integers > 0 and < 10
12+
size is the width of a cell
13+
size is an integer > 2
14+
the function returns nothing'''
15+
16+
horLine = ''
17+
verLine = ''
18+
19+
horLine = '+' + '-' * (size - 2) + '+'
20+
verLine = '|' + ' ' * (size - 2) + '|'
21+
22+
for i in range(nbRow):
23+
print horLine + (nbCol - 1) * horLine[1:]
24+
for j in range(size - 2):
25+
print verLine + (nbCol - 1) * verLine[1:]
26+
27+
print horLine + (nbCol - 1) * horLine[1:]
28+
return
29+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Mon Oct 13 19:43:16 2014
4+
5+
@author: Michel
6+
"""
7+
8+
def ackermann (m, n):
9+
"""
10+
computes ackerman recursive function
11+
12+
returns a positive integer value
13+
m and n are nonnegative integers
14+
"""
15+
if m < 0 or n < 0:
16+
return None
17+
elif m == 0:
18+
return n + 1
19+
20+
elif n == 0:
21+
return ackermann(m - 1, 1)
22+
else:
23+
return ackermann(m - 1, ackermann(m, n - 1))
24+
25+
if __name__ == '__main__':
26+
assert ackermann(-1,-1) == None
27+
assert ackermann(-1,0) == None
28+
assert ackermann(0,-1) == None
29+
assert ackermann(0,0) == 1
30+
assert ackermann(0,1) == 2
31+
assert ackermann(1,0) == 2
32+
assert ackermann(0,2) == 3
33+
assert ackermann(0,3) == 4
34+
assert ackermann(0,4) == 5
35+
assert ackermann(1,2) == 4
36+
assert ackermann(1,3) == 5
37+
assert ackermann(1,4) == 6
38+
assert ackermann(2,1) == 5
39+
assert ackermann(2,2) == 7
40+
assert ackermann(2,3) == 9
41+
assert ackermann(2,4) == 11
42+
assert ackermann(3,0) == 5
43+
assert ackermann(3,1) == 13
44+
assert ackermann(3,2) == 29
45+
assert ackermann(3,3) == 61
46+
assert ackermann(3,4) == 125
47+
assert ackermann(4,0) == 13
48+
assert ackermann(4,1) == 65533
49+
# assert ackermann(4,2) == 61
50+
# assert ackermann(4,3) == 61
51+
# assert ackermann(4,4) == 61
52+
print 'All tests passed'
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Tue Oct 07 20:25:04 2014
4+
5+
@author: Michel
6+
"""
7+
8+
def distance(x1, y1, x2, y2):
9+
""" Return distance between 2 points in 2D cartesian coordinates
10+
x1, y1, x2, y2 are float
11+
"""
12+
13+
distance = ((x2 - x1)**2 + (y2 - y1)**2)**0.5
14+
15+
return distance
16+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Tue Oct 07 20:33:17 2014
4+
5+
@author: Michel
6+
"""
7+
8+
for number in range(100):
9+
10+
if not number % 3 == 0 and not number % 5 == 0:
11+
print number
12+
13+
elif number % 3 == 0 and not number % 5 == 0:
14+
print 'Fizz'
15+
16+
elif not number % 3 == 0 and number % 5 == 0:
17+
print 'Buzz'
18+
19+
else:
20+
print 'FizzBuzz'
21+
22+
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Mon Oct 13 21:10:43 2014
4+
5+
@author: Michel
6+
"""
7+
8+
def fibonacci(n):
9+
"""
10+
Computes Fibonacci series
11+
12+
Returns Fibonacci of n
13+
n is a positive integer > 0
14+
"""
15+
if n < 0:
16+
return None
17+
elif n == 0 or n == 1:
18+
return 1
19+
else:
20+
return fibonacci(n-1) + fibonacci(n-2)
21+
22+
23+
def lucas(n):
24+
"""
25+
Computes Lucas series
26+
27+
Returns Lucas of n
28+
n is a positive integer > 0
29+
"""
30+
if n < 0:
31+
return None
32+
elif n == 0:
33+
return 2
34+
elif n == 1:
35+
return 1
36+
else:
37+
return lucas(n-1) + lucas(n-2)
38+
39+
40+
def sum_series(n, val1 = 0, val2 = 1):
41+
"""
42+
Computes Fibonacci, Lucas or other series depending on val1 and val2
43+
44+
45+
n is a positve integer
46+
val1 and val2 are optional parameters and are positve integers
47+
Returns Lucas of n if val1 ==2 and val2 == 1
48+
Returns Fibonacci of n if no optional parameter val1 and val2 specified
49+
Otherwise returns other series
50+
"""
51+
if n < 0:
52+
return None
53+
elif val1 == 0 and val2 == 1:
54+
return fibonacci(n)
55+
elif val1 == 2 and val2 == 1:
56+
return lucas(n)
57+
else:
58+
return n
59+
60+
61+
if __name__ == '__main__':
62+
# test all functions for n < 0
63+
assert fibonacci(-1) == None
64+
assert lucas(-1) == None
65+
assert sum_series(-1) == None
66+
# test fibonacci and lucas functions for base cases
67+
assert fibonacci(0) == 1
68+
assert fibonacci(1) == 1
69+
assert lucas(0) == 2
70+
assert lucas(1) == 1
71+
# test fibonacci and lucas functions for one general case
72+
assert fibonacci(5) == 8
73+
assert lucas(5) == 11
74+
# test sum_series function for parameter values
75+
assert sum_series(5) == fibonacci(5)
76+
assert sum_series(5, 0, 1) == fibonacci(5)
77+
assert sum_series(5, 2, 1) == lucas(5)
78+
assert sum_series(5, 23, 12) == 5
79+
print 'All tests passed'

0 commit comments

Comments
 (0)