def greatest(a: float, b: float, c: float, d: float) -> float:
if a > b:
if a > c:
if a > d:
return a
else: # a <= d
return d
else: # a <= c
if c > d:
return c
# This version would take a LONG TIME!
def greatest(a: float, b: float, c: float, d: float) -> float:
if a > b and a > c and a > d:
return a
elif b > c and b > d:
return b
elif c > d:
return c
else:
return d
greatest(7, -8, 1, 2)
7
def greatest(a: float, b: float, c: float, d: float) -> float:
r = a
if b > r:
r = b
if c > r:
r = c
if d > r:
r = d
return r
def isEven(v: int) -> bool:
print("In isEven")
return v%2 == 0
print(isEven(0))
print(isEven(1))
print(isEven(2))
In isEven True In isEven False In isEven True
def printEvenness(v: int) -> None:
if isEven(v):
print(v, "is even")
else:
print(v, "is odd")
printEvenness(1)
1 is odd
def describeEvenness(v: int) -> str:
print("In describeEvenness")
if isEven(v):
print("In describeEvenness even branch")
return "even"
else:
print("In describeEvenness odd branch")
return "odd"
def printEvenness(v: int) -> None:
print("In printEvenness")
print(v, "is", describeEvenness(v))
print("At end of printEvenness")
printEvenness(1)
In printEvenness In describeEvenness In isEven In describeEvenness odd branch 1 is odd At end of printEvenness
def admission(isAfter5: bool, age: int) -> int:
assert age >= 0, "Age cannot be negative"
#if age <= 12:
# if isAfter5:
# return 0
# else:
# return 900
#else:
# if isAfter5:
# return 0
# else:
# return 1600
if isAfter5:
return 0
else:
if age <= 12:
return 900
else:
return 1600
assert admission(False, 12) == 900, "$9 for 12-year-olds"
assert admission(False, 3) == 900, "$9 for less than 12"
assert admission(False, 6500) == 1600, "$16 for the ageless"
assert admission(False, 13) == 1600, "$16 for 13-year-olds"
assert admission(True, 12) == 0, "$0 after 5 for 12-year-olds"
assert admission(True, 3) == 0, "$0 after 5 for less than 12"
assert admission(True, 6500) == 0, "$0 after 5 for the ageless"
assert admission(True, 13) == 0, "$0 after 5 for 13-year-olds"
import math
def numberCategory(n: float) -> str:
if int(n) == n: # Is integer
return describeEvenness(int(n))
else: # Is non-integer
if n > 0:
return "pos"
else: # n must be less than 0 because 0 is an integer
return "neg"
assert numberCategory(math.pi) == "pos", "Pi is positive"
assert numberCategory(round(math.pi)) == "odd", "3 (rounded pi) is odd"
assert numberCategory(-1/12) == "neg", "-1/12 is negative"
assert numberCategory(-2) == "even", "-2 is even"
assert numberCategory(0) == "even", "0 is even"
In describeEvenness In isEven In describeEvenness odd branch In describeEvenness In isEven In describeEvenness even branch In describeEvenness In isEven In describeEvenness even branch
import math
def numberCategory(n: float) -> str:
if int(n) == n and n%2 == 0:
return "even"
elif int(n) == n and n%2 != 0:
return "odd"
elif n>0:
return "pos"
else:
return "neg"
assert numberCategory(math.pi) == "pos", "Pi is positive"
assert numberCategory(round(math.pi)) == "odd", "3 (rounded pi) is odd"
assert numberCategory(-1/12) == "neg", "-1/12 is negative"
assert numberCategory(-2) == "even", "-2 is even"
assert numberCategory(0) == "even", "0 is even"
print("foo" == "foo")
print("foo" != "bar")
print(False == False)
print(5 == "5")
print(True == 1)
True True True False True
print("aardvark" < "zebra")
print("Richards" > "Gregor")
print("Z" < "a")
True True True
def safeDiv(num: float, den: float) -> float:
if den == 0:
return math.inf
else:
return num / den
safeDiv(1, 0)
inf
def xor(a: bool, b: bool) -> bool:
return (a and not b) or (b and not a)
# Also works but confusing: return a != b
print(xor(False, False))
print(xor(False, True))
print(xor(True, False))
print(xor(True, True))
False True True False
def xor3(a: bool, b: bool, c: bool) -> bool:
# WRONG WRONG WRONG
#return xor(xor(a, b), c)
if a:
return not b and not c
else:
return xor(b, c)
print(xor3(True, True, True))
False
def majorityVote(a: str, b: str, c: str) -> str:
if a == b:
return a
elif a == c:
return a
elif b == c:
return b
else:
return "no winner"