Skip to content
Merged
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
106 changes: 106 additions & 0 deletions Students/A.Kramer/session08/generator_solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
'''
Created on Nov 26, 2014

@author: Alekesey Kramer
'''

# Generator to add numbers to predecessors
def intsum():
num = 0
count = 0
while num >= 0:
yield num
count += 1
num += count


def doubler():
num = 1
while num >= 0:
yield num
num = num*2

def prime():
# My code :-)
count = 2
while True:
if __is_prime(count):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally, with a generator, you generate as you go -- so you don't want to do the whole is_prime calculation for each number. That is there is some information you already know when generating the next prime number, having computed a bunch so far. But you've got the key structure for generators down, which is the point here.

yield count
count += 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you are going to increment count either way, why not put it outside the if block?

else:
count += 1

# Found this code on the web at
# https://www.daniweb.com/software-development/python/threads/70650/an-isprimen-function
# with combination from here: http://stackoverflow.com/questions/15285534/isprime-function-for-python-language
# and modified a bit to reject all the numbers lees than 2 (just in case I will use it later)
# quite useful and fast function, but limited by the have the size of int...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, python "int" is a "long int" -- i.e. it automatically rolls over to a unlimited precision integer -- so only limit is computation time.

But there is a bug -- it reports True for some even numbers::
In [8]: for i in range(10):
...: print i, __is_prime(i)
...:
0 False
1 False
2 True
3 True
4 True
5 True
6 True
7 True
8 True
9 False

def __is_prime(n):
if n < 2:
return False
if n == 2:
return True
# checking only odd numbers
for x in range(3, int(n**0.5)+1, 2):
if n % x == 0:
return False
return True

# fibonacci
def fib():
first = 1
second = 1
while True:
yield(first)
n = first + second
first = second
second = n


if __name__ == "__main__":
print "Sum of integers"
sum_of_ints = intsum()
print sum_of_ints.next()
print sum_of_ints.next()
print sum_of_ints.next()
print sum_of_ints.next()
print sum_of_ints.next()
print sum_of_ints.next()
print
print "Doubler"
doub = doubler()
print doub.next()
print doub.next()
print doub.next()
print doub.next()
print doub.next()
print doub.next()
print
print "Getting Primes"
g = prime()
print g.next()
print g.next()
print g.next()
print g.next()
print g.next()
print g.next()
print g.next()
print g.next()
print g.next()
print
print "Fibonacci"
f = fib()
print f.next()
print f.next()
print f.next()
print f.next()
print f.next()
print f.next()
print f.next()
print f.next()
print f.next()





21 changes: 21 additions & 0 deletions Students/A.Kramer/session08/quadratic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'''
Created on Nov 25, 2014

@author: Aleksey
'''

# Quadratic equation
class Quadratic(object):
def __init__(self, a, b, c):
self._a = a
self._b = b
self._c = c

def __call__(self, x):
self._x = x
return self._a * self._x**2 + self._b * self._x + self._c


if __name__ == "__main__":
q = Quadratic(1,2,3)
print q(3)
33 changes: 33 additions & 0 deletions Students/A.Kramer/session08/sparse_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class SparseArray(object):
def __init__(self, iterable):
self.values = {}
self.length = len(iterable)
for i, val in enumerate(iterable):
if val:
self.values[i] = val

def __getitem__(self, index):
if index >= self.length:
raise IndexError("Sparse array index out of range")
else:
return self.values.get(index, 0)

def __setitem__(self, index, value):
if index >= self.length:
raise IndexError("Sparse array index out of range")
else:
if value == 0:
self.values.pop(index, None)
else:
self.values[index] = value

def __delitem__(self, index):
if index >= self.length:
raise IndexError("Sparse array index out of range")
else:
self.values.pop(index, None)
self.length -= 1

def __len__(self):
return self.length

65 changes: 65 additions & 0 deletions Students/A.Kramer/session08/test_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""
test_generator.py

tests the solution to the generator lab

can be run with py.test or nosetests
"""

import generator_solution as gen


def test_intsum():

g = gen.intsum()

assert g.next() == 0
assert g.next() == 1
assert g.next() == 3
assert g.next() == 6
assert g.next() == 10
assert g.next() == 15


def test_doubler():

g = gen.doubler()

assert g.next() == 1
assert g.next() == 2
assert g.next() == 4
assert g.next() == 8
assert g.next() == 16
assert g.next() == 32

for i in range(10):
j = g.next()

assert j == 2**15


def test_fib():
g = gen.fib()

assert g.next() == 1
assert g.next() == 1
assert g.next() == 2
assert g.next() == 3
assert g.next() == 5
assert g.next() == 8
assert g.next() == 13
assert g.next() == 21


def test_prime():
g = gen.prime()

assert g.next() == 2
assert g.next() == 3
assert g.next() == 5
assert g.next() == 7
assert g.next() == 11
assert g.next() == 13
assert g.next() == 17
assert g.next() == 19
assert g.next() == 23
35 changes: 35 additions & 0 deletions Students/A.Kramer/session08/test_quadratic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env python

"""
test code for the quadratic function evaluator
"""

import pytest

from quadratic import Quadratic


def test_init():
q = Quadratic(1,2,3)


def test_evaluate():
q = Quadratic(1,2,3)

assert q(3) == 9 + 6 + 3
assert q(0) == 3


def test_evaluate2():
q = Quadratic(2,1,-3)

assert q(0) == -3
assert q(1) == 2 + 1 - 3
assert q(-2) == 8 - 2 - 3


def test_bad_input():

with pytest.raises(TypeError):
q = Quadratic(2,3)

82 changes: 82 additions & 0 deletions Students/A.Kramer/session08/test_sparse_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import pytest
from sparse_array import SparseArray


def set_up():
my_array = [2, 0, 0, 0, 3, 0, 0, 0, 4, 5, 6, 0, 2, 9]
my_sparse = SparseArray(my_array)
return (my_array, my_sparse)


def test_object_exists():
my_array, my_sparse = set_up()
assert isinstance(my_sparse, SparseArray)

def test_get_non_zero_number():
my_array, my_sparse = set_up()
assert my_sparse[4] == 3

def test_get_zero():
my_array, my_sparse = set_up()
assert my_sparse[1] == 0

def test_get_element_not_in_array():
my_array, my_sparse = set_up()
with pytest.raises(IndexError):
my_sparse[14]

def test_get_lenght():
my_array, my_sparse = set_up()
assert len(my_sparse) == 14

def test_change_number_in_array():
my_array, my_sparse = set_up()
my_sparse[0] = 3
assert my_sparse[0] == 3
# make sure others aren't changed
assert my_sparse[1] == 0
# make sure still same length
assert len(my_sparse) == 14

def test_change_number_in_array_to_zero():
my_array, my_sparse = set_up()
my_sparse[4] = 0
assert my_sparse[4] == 0
# make sure still same length
assert len(my_sparse) == 14

def test_change_number_in_array_from_zero():
my_array, my_sparse = set_up()
my_sparse[1] = 4
assert my_sparse[1] == 4
# make sure still same length
assert len(my_sparse) == 14

def test_delete_number():
my_array, my_sparse = set_up()
del(my_sparse[4])
# if we delete the 4 position, should now be zero
assert my_sparse[4] == 0
# should have smaller length
assert len(my_sparse) == 13


def test_delete_zero():
my_array, my_sparse = set_up()
del(my_sparse[5])
# should still be zero, but should have shorter length
assert my_sparse[5] == 0
assert len(my_sparse) == 13

def test_delete_last_number():
my_array, my_sparse = set_up()
del(my_sparse[13])
# should get an error?
print 'print some stuff damnit'
with pytest.raises(IndexError):
my_sparse[13]
assert len(my_sparse) == 13