Skip to content
88 changes: 88 additions & 0 deletions students/MikeSchincariol/session8/circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import math

class Circle(object):

@classmethod
def from_diameter(cls, diameter):
return cls(diameter/2)


def __init__(self, radius):
self.r = radius


@property
def radius(self):
return self.r

@radius.setter
def radius(self, val):
self.r = val


@property
def diameter(self):
return 2 * self.r


@diameter.setter
def diameter(self, val):
self.r = val / 2


@property
def area(self):
return 2 * math.pi * self.r

def __repr__(self):
return 'Circle({})'.format(self.r)

def __str__(self):
return 'Circle with radius: {:6f}'.format(self.r)

def __add__(self, other):
if isinstance(other, Circle):
return Circle(self.r + other.r)
else:
raise ValueError("The other parameter must be a circle type")

def __iadd__(self, other):
if isinstance(other, Circle):
self.r += other.r
return self
else:
raise ValueError("The other parameter must be a circle type")

def __radd__(self, other):
return self.__add__(other)


def __mul__(self, other):
if isinstance(other, int):
return Circle(self.r * other)
else:
raise ValueError("The other parameter must be an int")

def __imul__(self, other):
if isinstance(other, int):
self.r *= other
return self
else:
raise ValueError("The other parameter must be an int")


def __rmul__(self, other):
return self.__mul__(other)


def __gt__(self, other):
if isinstance(other, Circle):
return self.r > other.r
else:
raise ValueError("The other parameter must be a circle type")

def __eq__(self, other):
if isinstance(other, Circle):
return self.r == other.r
else:
raise ValueError("The other parameter must be a circle type")
9 changes: 9 additions & 0 deletions students/MikeSchincariol/session8/quadratic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Quadratic(object):

def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c

def __call__(self, x):
return self.a*x**2 + self.b*x + self.c
102 changes: 102 additions & 0 deletions students/MikeSchincariol/session8/sparsearray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@


class Sparsearray(object):


def __init__(self, iterable):
self.max_idx = None # None means the array is empty
self.array = {} # The dict that holds the array data

# Iterate over the supplied list of values and put the non-zero
# items in the dict
for idx,val in enumerate(iterable):
if val > 0:
self.array[idx] = val

# Adjust the max index that the array knows about
# to account for the initial values
self.max_idx = len(iterable)-1


def __len__(self):
""" Returns the length of the sparse array
"""
return self.max_idx+1


def __contains__(self, x):
""" Returns True if the value 'x' is in the sparse array
"""
if self.max_idx is None:
return False
elif x == 0 and len(self) != len(self.array):
# Looking for a 0. Since this is a sparse array, we
# can't look in the array itself to see if the value '0'
# is present because it is not stored. The only way we
# can detect that a '0' is in the array is by a mismatch
# on the length of the array versus the last index of
# the array.
return True
else:
return x in self.array


def __setitem__(self, k, v):
""" Sets item 'k' equal to value 'v'
"""
# If the value is 0, we never store it and if item 'k' already
# existed as a non-zero value, we have to remove it. Finally,
# if the value is non-zero, store it in the array.
if v == 0 and k in self.array:
del self.array[k]
elif v > 0:
self.array[k] = v

# Update the last index of the array
if k > self.max_idx:
self.max_idx = k


def __getitem__(self, k):
""" Returns the value of item 'k', if 'k' is in the array
"""
# If the item 'k' is outside the bounds of the array, then
# it was never added to the array. If 'k' is inside the
# bounds of the array, the array will have it if the value
# associated with the item 'k' is non-zero.
if k > self.max_idx:
return None
elif k in self.array:
return self.array[k]
else:
return 0

def __delitem__(self, k):
""" Removes item 'k' from the array
"""
if k in self.array:
del self.array[k]



def __iter__(self):
""" Iterates over the list of items returning one at a time.
"""
for idx in range(len(self)):
if idx in self.array:
yield self.array[idx]
else:
yield 0

def __str__(self):
# Used the same string as returned by __repr__
self.__repr__(self)

def __repr__(self):
l = [x for x in self]
return "Sparsearray({})".format(l)





97 changes: 97 additions & 0 deletions students/MikeSchincariol/session8/test_circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
from circle import Circle
import math
import pytest

def test_get_radius():
c = Circle(5)
assert c.radius == 5

def test_get_diameter():
c = Circle(4)
assert c.diameter == 2*4

def test_set_diameter():
c = Circle(4)
c.diameter = 2
assert c.diameter == 2
assert c.radius == 1

def test_get_area():
c = Circle(2)
assert c.area == 2 * math.pi * 2

def test_set_area():
c = Circle(2)
with pytest.raises(AttributeError):
c.area = 42

def test_create_with_diameter():
c = Circle.from_diameter(8)
assert c.diameter == 8
assert c.radius == 4

def test_repr():
c = Circle(4)
assert c.__repr__() == 'Circle(4)'

def test_str():
c = Circle(4)
assert c.__str__() == "Circle with radius: 4.000000"

def test_add_circles():
c1 = Circle(2)
c2 = Circle(4)
c = c1 + c2
return c.radius == 4

def test_mult_circles0():
c = Circle(2)
c *= 3
return c.radius == Circle(12).radius

def test_mult_circles1():
c = Circle(2)
return (3 * c) == Circle(12)

def test_gt():
c1 = Circle(2)
c2 = Circle(4)
assert not (c1 > c2)

def test_lt():
c1 = Circle(2)
c2 = Circle(4)
assert (c1 < c2)

def test_eq0():
c1 = Circle(2)
c2 = Circle(4)
assert (c1 != c2)

def test_eq0():
c1 = Circle(2)
c2 = Circle(4)
c3 = Circle(4)
assert (c2 == c3)

def test_sort_circles():
circles = [Circle(6), Circle(7), Circle(8), Circle(4), Circle(0), Circle(2), Circle(3), Circle(5), Circle(9), Circle(1)]
circles.sort()
assert circles.__repr__() == "[Circle(0), Circle(1), Circle(2), Circle(3), Circle(4), Circle(5), Circle(6), Circle(7), Circle(8), Circle(9)]"

def test_reflected_numerics():
a_circle = Circle(2)
assert (a_circle * 3) == (3 * a_circle)

def test_augmented_add():
a_circle = Circle(2)
another_circle = Circle(4)
a_circle += another_circle
assert a_circle.r == 6

def test_augmented_mult():
a_circle = Circle(4)
a_circle *= 2
assert a_circle.r == 8


20 changes: 20 additions & 0 deletions students/MikeSchincariol/session8/test_quadratic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from quadratic import Quadratic as quad


def test_quad0():
my_quad = quad(a=2, b=3, c=1)
assert my_quad(0) == 1

def test_quad1():
my_quad = quad(a=2, b=3, c=1)
assert my_quad(1) == 6

def test_quad2():
my_quad = quad(a=2, b=3, c=1)
assert my_quad(2) == 15

def test_quad3():
my_quad = quad(a=2, b=3, c=1)
assert my_quad(3) == 28


50 changes: 50 additions & 0 deletions students/MikeSchincariol/session8/test_sparsearray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from sparsearray import Sparsearray


def test_sa_creation():
sa = Sparsearray([1,2,0,0,0,0,3,0,0,4])
assert sa.__repr__() == "Sparsearray([1, 2, 0, 0, 0, 0, 3, 0, 0, 4])"

def test_sa_len():
sa = Sparsearray([1,2,0,0,0,0,3,0,0,4])
assert len(sa) == 10

def test_sa_setitem0():
sa = Sparsearray([1,2,0,0,0,0,3,0,0,4])
sa[5] = 12
assert sa.__repr__() == "Sparsearray([1, 2, 0, 0, 0, 12, 3, 0, 0, 4])"

def test_sa_setitem1():
sa = Sparsearray([1,2,0,0,0,0,3,0,0,4])
sa[3] = 0
assert sa.__repr__() == "Sparsearray([1, 2, 0, 0, 0, 0, 3, 0, 0, 4])"

def test_sa_setitem2():
sa = Sparsearray([1,2,0,0,0,0,3,0,0,4])
sa[0] = 0
assert sa.__repr__() == "Sparsearray([0, 2, 0, 0, 0, 0, 3, 0, 0, 4])"

def test_sa_getitem0():
sa = Sparsearray([1,2,0,0,0,0,3,0,0,4])
assert sa[13] == None

def test_sa_getitem0():
sa = Sparsearray([1,2,0,0,0,0,3,0,0,4])
assert sa[0] == 1

def test_sa_getitem1():
sa = Sparsearray([1,2,0,0,0,0,3,0,0,4])
assert sa[1] == 2

def test_sa_getitem2():
sa = Sparsearray([1,2,0,0,0,0,3,0,0,4])
assert sa[2] == 0

def test_sa_getitem3():
sa = Sparsearray([1,2,0,0,0,0,3,0,0,4])
assert sa[9] == 4

def test_sa_delitem0():
sa = Sparsearray([1,2,0,0,0,0,3,0,0,4])
del sa[1]
assert sa.__repr__() == "Sparsearray([1, 0, 0, 0, 0, 0, 3, 0, 0, 4])"