Skip to content

Commit d7591c9

Browse files
committed
added first doctest test
1 parent e28eec4 commit d7591c9

1 file changed

Lines changed: 132 additions & 0 deletions

File tree

v3/tests/doctest-tests/lab1.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# some examples adapted from https://docs.python.org/2/library/doctest.html
2+
3+
LAB_NAME = "First doctest lab"
4+
5+
LAB_DESCRIPTION = '''
6+
This is my first **lab** in [markdown](https://daringfireball.net/projects/markdown/syntax) format
7+
8+
- shawn
9+
- is
10+
- cool
11+
12+
woohoo!
13+
'''
14+
15+
16+
def factorial(n):
17+
"""
18+
Lab part 1
19+
20+
Return the factorial of n, an exact integer >= 0.
21+
22+
If the result is small enough to fit in an int, return an int.
23+
Else return a long.
24+
25+
------
26+
27+
>>> [factorial(n) for n in range(6)]
28+
[1, 1, 2, 6, 24, 120]
29+
>>> [factorial(long(n)) for n in range(6)]
30+
[1, 1, 2, 6, 24, 120]
31+
>>> factorial(30)
32+
265252859812191058636308480000000L
33+
>>> factorial(30L)
34+
265252859812191058636308480000000L
35+
>>> factorial(-1)
36+
Traceback (most recent call last):
37+
...
38+
ValueError: n must be >= 0
39+
>>> factorial(30.1)
40+
Traceback (most recent call last):
41+
...
42+
ValueError: n must be exact integer
43+
>>> factorial(30.0)
44+
265252859812191058636308480000000L
45+
>>> factorial(1e100)
46+
Traceback (most recent call last):
47+
...
48+
OverflowError: n too large
49+
"""
50+
import math
51+
if not n >= 0:
52+
raise ValueError("n must be >= 0")
53+
if math.floor(n) != n:
54+
raise ValueError("n must be exact integer")
55+
if n+1 == n: # catch a value like 1e300
56+
raise OverflowError("n too large")
57+
result = 1
58+
factor = 2
59+
while factor <= n:
60+
result *= factor
61+
factor += 1
62+
return result
63+
64+
65+
# helper function written by student, not part of the lab
66+
def add(x, y):
67+
return x + y
68+
69+
70+
def slow_multiply(a, b):
71+
"""
72+
Lab part 2
73+
74+
Return the product of 'a' and 'b'
75+
76+
------
77+
78+
>>> slow_multiply(3, 5)
79+
15
80+
>>> slow_multiply(5, 3)
81+
15
82+
>>> slow_multiply(0, 1)
83+
0
84+
>>> slow_multiply(0, 100)
85+
0
86+
>>> slow_multiply(-1, 5)
87+
-5
88+
89+
"""
90+
91+
i = 0
92+
prod = 0
93+
for i in range(b):
94+
prod = add(prod, a)
95+
return prod
96+
97+
98+
GLOBAL_DATA = [{'name': 'John', 'age': 21},
99+
{'name': 'Jane', 'age': 35},
100+
{'name': 'Carol', 'age': 18}]
101+
102+
def find_age(person):
103+
"""
104+
Lab part 3
105+
106+
Fetch the age for a given person's name
107+
108+
------
109+
110+
>>> find_age('John')
111+
21
112+
>>> find_age('Carol')
113+
18
114+
>>> find_age('Jane')
115+
35
116+
>>> find_age('jane')
117+
Traceback (most recent call last):
118+
File "<stdin>", line 1, in <module>
119+
File "lab1.py", line 114, in find_age
120+
raise KeyError # not found!
121+
KeyError
122+
>>> find_age('bobby')
123+
Traceback (most recent call last):
124+
File "<stdin>", line 1, in <module>
125+
File "lab1.py", line 114, in find_age
126+
raise KeyError # not found!
127+
KeyError
128+
"""
129+
for e in GLOBAL_DATA:
130+
if e['name'] == person:
131+
return e['age']
132+
raise KeyError # not found!

0 commit comments

Comments
 (0)