Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions fib-fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
last_2 = 1

def next():
global last_1, last_2
global last_1, last_2 #use the global last_1 and last_2

next_fib = last_1 + last_2
last_1, last_2 = last_2, next_fib

next_fib = last_1 + last_2 #store the sum of last_1 and last_2
last_1, last_2 = last_2, next_fib #re-assignment of the variables.
# last_1 = last_2 && last_2 = next_fib
return next_fib

print next()
Expand Down
2 changes: 2 additions & 0 deletions fib_gen/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
for n, i in zip(range(3), fib.fib()):
print i

print type(fib.fib())

# additional questions to address:
# - what the heck do 'zip' and 'range' do, and why are they there?
13 changes: 9 additions & 4 deletions fib_gen/fib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@
# a generator.

def fib():
last_1 = 1
last_1 = 1 # stored locally in this function
last_2 = 1

while 1:
next_fib = last_1 + last_2
last_1, last_2 = last_2, next_fib
yield next_fib
# next_fib = last_1 + last_2

# yield next_fib
yield (last_1 + last_2)
# last_1, last_2 = last_2, next_fib
last_1, last_2 = last_2, (last_1+last_2)

# additional questions to address:
# - could you swap the last two lines of the while statement? what are the
# plusses and minuses of doing so?


# - is there a way to condense the 'while' loop into two statements? try
# getting rid of the next_fib variable.
Binary file added fib_gen/fib.pyc
Binary file not shown.
8 changes: 6 additions & 2 deletions fib_iter/example.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import fib

for n, i in zip(range(3), fib.fib()):
print i
for n, i in zip(range(5), fib.fib()): # Gets the returned value of __iter__
print n, i

# additional questions to address:
# - what the heck do 'zip' and 'range' do, and why are they there?
''' 'zip' returns a list of tuples, where the ith tuple contains the
ith element for each argument sequences of iterables.
'range' is a function that contains iterator progressions. In this
example the iterator starts at 0 and ends at 2. '''
Binary file added fib_iter/fib.pyc
Binary file not shown.
Binary file added fib_mod/fib.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion iter_bug/fib.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ def __iter__(self):

def next(self):
next_fib = self.last_1 + self.last_2
self.last_1, self.last2 = self.last_2, next_fib
self.last_1, self.last_2 = self.last_2, next_fib
return next_fib
Binary file added iter_bug/fib.pyc
Binary file not shown.
3 changes: 2 additions & 1 deletion iter_bug/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ def test2():
i.next()
assert i.next() == 3

def test2():
def test3():
f = fib.fib()
i = iter(f)
i.next()
i.next()
assert i.next() == 5

# Tests
test1()
test2()
test3()
Expand Down
2 changes: 1 addition & 1 deletion series-fn.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
n = 0
n = 0 # 1. n is set to 0.

def add_one():
global n
Expand Down
6 changes: 3 additions & 3 deletions series_gen/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

def adder():
n = 0
while 1:
n += 1
yield n
while 1: # while true
n += 1
yield n # returns generated n
Binary file added series_gen/series.pyc
Binary file not shown.
Binary file added series_iter/series.pyc
Binary file not shown.
10 changes: 7 additions & 3 deletions series_mod/series.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# this is an implementation of the 'series' functionality using a module.

n = 0
n = 0 # Sets n to 0

def add_one():
global n
n = n + 1
global n # Sets a global n, init value 0.
n = n + 1 # increment n by 1
return n

# additional questions to address:
# - what does 'global' do, above?
# Uses the n thats defined locally and uses it as a global variable.

# - what naming limitations are there on series.py? Could we name it
# series_mod.py or series-mod.py, and still have it work as a module?

# As long as the correct
Binary file added series_mod/series.pyc
Binary file not shown.
6 changes: 6 additions & 0 deletions sieve_gen/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import sieve

for n, i in zip(range(10), sieve.next()):
print i


17 changes: 17 additions & 0 deletions sieve_gen/sieve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
_primeslist = [2]

def _is_prime(primes, n):
for i in primes:
if n % i == 0:
return False
return True

def next():
start = _primeslist[-1] + 1
while 1:
if _is_prime(_primeslist, start):
_primeslist.append(start)
yield start
start+=1


Binary file added sieve_gen/sieve.pyc
Binary file not shown.
8 changes: 8 additions & 0 deletions sieve_gen/test1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import sieve

def test1():
s = sieve.next()
i = iter(s)
assert i.next() == 3

test1()
12 changes: 12 additions & 0 deletions sieve_gen/test2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import sieve

def test2():
s = sieve.next()
i = iter(s)

for x in range(4):
i.next()

assert i.next() == 13

test2()
12 changes: 12 additions & 0 deletions sieve_gen/test3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import sieve

def test3():
s = sieve.next()
i = iter(s)

for x in range(8):
i.next()
assert i.next() == 29


test3()
7 changes: 7 additions & 0 deletions sieve_iter/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import sieve

for n, i in zip(range(10),sieve.sieve()):
print i



28 changes: 28 additions & 0 deletions sieve_iter/sieve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Implementation using iter

class sieve(object):
def __init__(self):
self._primeslist = [2]

def __iter__(self):
return self

def _is_prime(self, n):
for i in self._primeslist:
if(n % i == 0):
return False
return True


def next(self):
start = self._primeslist[-1] + 1
while 1:
if self._is_prime(start):
self._primeslist.append(start)
return start

start+=1




Binary file added sieve_iter/sieve.pyc
Binary file not shown.
8 changes: 8 additions & 0 deletions sieve_iter/test1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import sieve

def test1():
s = sieve.sieve()
i = iter(s)
assert i.next() == 3

test1()
11 changes: 11 additions & 0 deletions sieve_iter/test2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import sieve

def test2():
s = sieve.sieve()
i = iter(s)

for x in range(4):
i.next()
assert i.next() == 13

test2()
11 changes: 11 additions & 0 deletions sieve_iter/test3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import sieve

def test3():
s = sieve.sieve()
i = iter(s)

for x in range(8):
i.next()
assert i.next() == 29

test3()
7 changes: 7 additions & 0 deletions sieve_mod/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import sieve

for n in range(10):
print sieve.next()



18 changes: 18 additions & 0 deletions sieve_mod/sieve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
_primeslist = [2]

def _is_prime(primes, n):
for i in primes:
if n % i == 0:
return False
return True

def next():
start = _primeslist[-1] + 1
while 1:
if _is_prime(_primeslist, start):
_primeslist.append(start)
return start

start += 1


Binary file added sieve_mod/sieve.pyc
Binary file not shown.