Skip to content

Commit 182b9a4

Browse files
author
Dave Fugelso
committed
All of HW for last three classes
1 parent 5b16ced commit 182b9a4

File tree

6 files changed

+227
-7
lines changed

6 files changed

+227
-7
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
def y_xrange(start, stop, step=1):
2+
i = start
3+
while i < stop:
4+
yield i
5+
i += step
6+
7+
8+
9+
def intsum():
10+
'''
11+
keep adding the next integer
12+
13+
0 + 1 + 2 + 3 + 4 + 5 + ...
14+
15+
so the sequence is:
16+
17+
0, 1, 3, 6, 10, 15 .....
18+
'''
19+
i = 0
20+
sum = 0
21+
22+
while True:
23+
sum = i + sum
24+
yield sum
25+
i += 1
26+
27+
28+
def intsum2():
29+
'''
30+
test_generator has a intsum2... dunno why
31+
'''
32+
i = 0
33+
sum = 0
34+
35+
while True:
36+
sum = i + sum
37+
yield sum
38+
i += 1
39+
40+
def doubler():
41+
'''
42+
Doubler:
43+
Each value is double the previous value:
44+
45+
1, 2, 4, 8, 16, 32,
46+
'''
47+
sum = 1
48+
while True:
49+
yield sum
50+
sum += sum
51+
52+
def fib():
53+
'''
54+
Fibonacci sequence:
55+
The fibonacci sequence as a generator:
56+
57+
f(n) = f(n-1) + f(n-2)
58+
59+
1, 1, 2, 3, 5, 8, 13, 21, 34...
60+
'''
61+
yield 1
62+
x = 0
63+
y = 1
64+
while True:
65+
yield x+y
66+
x, y = y, x+y
67+
68+
69+
70+
71+
72+
def isprime(n):
73+
'''check if integer n is a prime'''
74+
# make sure n is a positive integer
75+
n = abs(int(n))
76+
# 0 and 1 are not primes
77+
if n < 2:
78+
return False
79+
# 2 is the only even prime number
80+
if n == 2:
81+
return True
82+
# all other even numbers are not primes
83+
if not n & 1:
84+
return False
85+
# range starts with 3 and only needs to go up the squareroot of n
86+
# for all odd numbers
87+
for x in range(3, int(n**0.5)+1, 2):
88+
if n % x == 0:
89+
return False
90+
return True
91+
92+
def prime():
93+
'''
94+
Prime numbers:
95+
Generate the prime numbers (numbers only divisible by them self and 1):
96+
97+
2, 3, 5, 7, 11, 13, 17, 19, 23...
98+
'''
99+
i = 2
100+
while True:
101+
while not isprime (i):
102+
i += 1
103+
yield i
104+
i += 1
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"""
2+
test_generator.py
3+
4+
tests the solution to the generator lab
5+
6+
can be run with py.test or nosetests
7+
"""
8+
9+
import generator_solution as gen
10+
11+
12+
def test_intsum():
13+
14+
g = gen.intsum()
15+
16+
assert g.next() == 0
17+
assert g.next() == 1
18+
assert g.next() == 3
19+
assert g.next() == 6
20+
assert g.next() == 10
21+
assert g.next() == 15
22+
23+
24+
def test_intsum2():
25+
26+
g = gen.intsum2()
27+
28+
assert g.next() == 0
29+
assert g.next() == 1
30+
assert g.next() == 3
31+
assert g.next() == 6
32+
assert g.next() == 10
33+
assert g.next() == 15
34+
35+
36+
def test_doubler():
37+
38+
g = gen.doubler()
39+
40+
assert g.next() == 1
41+
assert g.next() == 2
42+
assert g.next() == 4
43+
assert g.next() == 8
44+
assert g.next() == 16
45+
assert g.next() == 32
46+
47+
for i in range(10):
48+
j = g.next()
49+
50+
assert j == 2**15
51+
52+
53+
def test_fib():
54+
g = gen.fib()
55+
56+
assert g.next() == 1
57+
assert g.next() == 1
58+
assert g.next() == 2
59+
assert g.next() == 3
60+
assert g.next() == 5
61+
assert g.next() == 8
62+
assert g.next() == 13
63+
assert g.next() == 21
64+
65+
66+
def test_prime():
67+
g = gen.prime()
68+
69+
assert g.next() == 2
70+
assert g.next() == 3
71+
assert g.next() == 5
72+
assert g.next() == 7
73+
assert g.next() == 11
74+
assert g.next() == 13
75+
assert g.next() == 17
76+
assert g.next() == 19
77+
assert g.next() == 23
78+

Students/Dave Fugelso/Session 9/p_wrapper.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ def string_return (string):
1919
class tag_wrapper (object):
2020

2121
def __init__(self, tag):
22+
#print "Inside __init__()"
2223
self.tag = tag
2324

24-
@p_wrapper
2525
def __call__(self, string):
26-
def string_return (string):
27-
string = '<'+self.tag+'> '+string+' </'+self.tag+'p>'
28-
return string_return
26+
#print "Inside __call__()"
27+
def tagged_line (string):
28+
# print 'decorator'
29+
# print '<'+self.tag+'> '+string+' </'+self.tag+'>'
30+
return '<'+self.tag+'> '+string+' </'+self.tag+'>'
31+
return tagged_line
32+

Students/Dave Fugelso/Session 9/test_p_wrapper.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,3 @@ def return_a_string(string):
3030

3131
assert return_a_string("this is a string") == "<div> this is a string </div>"
3232

33-
test_p_wrapper()
34-
test_tag_wrapper()
35-
test_tag_wrapper2()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'''
2+
Test timer context manager.
3+
'''
4+
5+
from timer import *
6+
7+
def test_timer():
8+
print 'test'
9+
with Timer() as t:
10+
for i in range(100000):
11+
i = i ** 20
12+
print t.elapsed_time()
13+
assert t.elapsed_time() > 0
14+
15+
test_timer()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import time
2+
3+
class Timer (object):
4+
'''
5+
Context manager that prints elapsed time on exit.
6+
'''
7+
def __init__(self):
8+
print 'init'
9+
pass
10+
11+
12+
def __enter__(self):
13+
print 'enter'
14+
self.start = time.time()
15+
return self
16+
17+
def __exit__(self, exc_type, exc_val, exc_tb):
18+
print 'Exit'
19+
self.end = time.time()
20+
21+
def elapsed_time (self):
22+
return self.end - self.start

0 commit comments

Comments
 (0)