Orchard classes, Paint class, and Polynomial class
# Add the classes required so that all the following
# test functions work properly. Do not hardcode anything!
def testOrchardClasses():
print("Testing Fruit, Citrus and Tree classes...", end="")
rA = Fruit("apple", "Malus", "red")
gA = Fruit("apple", "Malus", "green")
gP = Fruit("pear", "Pyrus", "green")
lL = Citrus("lemon", "yellow")
gL = Citrus("lime", "green")
oO = Citrus("orange", "orange")
assert(rA.name == "apple")
assert(gA.genus == "Malus")
assert(gP.color == "green")
# NOTE: an apple equals an apple, regardless of color
assert(rA == gA)
assert(gA != gP)
assert(rA != lL)
assert(rA != "Don't Crash Here")
# NOTE: All Citrus have genus Rutaceae
assert(lL == Fruit("lemon", "Rutaceae", "yellow"))
assert(str([rA, oO]) == "[<APPLE: red Malus>, (ORANGE: orange Rutaceae)]")
assert(str({gP}) == "{<PEAR: green Pyrus>}")
# NOTE: Tree takes in a list of fruits as a parameter
assert(Tree.howManyInOrchard(rA) == 0)
assert(Tree.howManyInOrchard(gP) == 0)
assert(Tree.howManyInOrchard(oO) == 0)
assert(Tree.howManyInOrchard(gL) == 0)
T1 = Tree("Oak", [rA, rA, gP, gA, rA])
# NOTE: this string has the fruits sorted by count
assert(str(T1) == "Oak Tree(1 of pear, 4 of apple)")
assert(Tree.howManyInOrchard(rA) == 4)
assert(Tree.howManyInOrchard(gP) == 1)
assert(Tree.howManyInOrchard(oO) == 0)
assert(Tree.howManyInOrchard(gL) == 0)
T2 = Tree("Willow", [gA, gL, gL, gL, gA, gL, gL, gP])
assert(str(T2) == "Willow Tree(1 of pear, 2 of apple, 5 of lime)")
# NOTE: T1 has 4 apples, T2 has 2 apples, so the entire orchard has 6
assert(Tree.howManyInOrchard(rA) == 6)
assert(Tree.howManyInOrchard(gP) == 2)
assert(Tree.howManyInOrchard(oO) == 0)
assert(Tree.howManyInOrchard(gL) == 5)
print("Passed!")
def testPaint():
print("Testing Paint class...", end="")
CandyApple = Paint(255, 28, 16)
Burgundy = Paint(151, 16, 51)
Sapphire = Paint(16, 82, 186)
Turkish = Paint(79, 151, 163)
# NOTE: You can use the hex() function to convert each of the r, g, and
# b base-10 numbers into hexedecimal. Assume all the colors have r, b and b
# numbers between 16 and 255 for convenience.
assert(str([CandyApple, Burgundy]) == "[#ff1c10, #971033]")
assert(CandyApple != Burgundy != Sapphire != Turkish)
assert(CandyApple != [42]) # Don't crash here!
assert(CandyApple == Paint(255, 28, 16))
assert(CandyApple == "#ff1c10") # Careful here...
assert(CandyApple != "#971033")
TurkishCandyApple = CandyApple.mixPaints(Turkish)
assert(TurkishCandyApple == Paint(167, 89, 89)) # (255 + 79) // 2 == 167
# NOTE: this should work for arbitrary color catagories, not just red
# green and blue. A dictionary somewhere may be useful...
assert(Paint.examineCatalog("red") == set())
assert(Paint.examineCatalog("green") == set())
assert(Paint.examineCatalog("blue") == set())
CandyApple.addToCatalog("red")
assert(Paint.examineCatalog("red") == {CandyApple})
assert(Paint.examineCatalog("green") == set())
assert(Paint.examineCatalog("blue") == set())
Burgundy.addToCatalog("red")
Sapphire.addToCatalog("blue")
assert(Paint.examineCatalog("red") == {CandyApple, Burgundy})
assert(Paint.examineCatalog("green") == set())
assert(Paint.examineCatalog("blue") == {Sapphire})
print("Passed!")
def testPolynomial():
print("Testing Polynomial class...", end="")
func0 = Polynomial({1:1})
func1 = Polynomial({2:1, 0:-1})
func2 = Polynomial({2:1, 1:0, 0:-1})
func3 = Polynomial({2:-5, 1:1, 3:2, 0:1})
# NOTE: think about where you could get these letters from...
assert(str(func0) == "f(x) = 1x")
assert(str(func1) == "g(x) = 1x^2 + -1")
assert(str(func2) == "h(x) = 1x^2 + -1")
assert(str(func3) == "i(x) = 2x^3 + -5x^2 + 1x + 1")
# Careful with equality here, consider why func1 and func2 are equal
assert(func1 == func2)
assert(func2 != func3)
assert(func0 != "Don't crash here!")
# Think about what getLog is, and where we can store this information
assert(func1.getLog() == [])
assert(func1.callWithInput(0) == -1)
assert(func1.callWithInput(1) == 0)
assert(func1.callWithInput(2) == 3)
assert(func1.callWithInput(3) == 8)
assert(Polynomial.getLog() == [
'g(0) = 1(0)^2 + -1 = -1',
'g(1) = 1(1)^2 + -1 = 0',
'g(2) = 1(2)^2 + -1 = 3',
'g(3) = 1(3)^2 + -1 = 8'])
func4 = func1.multiplyPolynomials(func2)
assert(func4 == Polynomial({4:1, 2:-2, 0:1}))
assert(func4.callWithInput(-2) == 9)
assert(Polynomial.getLog() == [
'g(0) = 1(0)^2 + -1 = -1',
'g(1) = 1(1)^2 + -1 = 0',
'g(2) = 1(2)^2 + -1 = 3',
'g(3) = 1(3)^2 + -1 = 8',
'j(-2) = 1(-2)^4 + -2(-2)^2 + 1 = 9'])
print("Passed!")
def testAll():
testOrchardClasses()
testPaint()
testPolynomial()
testAll()