def greatest(a: float, b: float, c: float, d: float) -> float:
"""
Returns the greatest value among a, b, c, and d.
"""
if a > b:
if a > c:
if a > d:
return a
else: # d >= a
return d
else: # c >= a
if c > d:
return c
else: # d >= c
return d
else: # b >= a
if b > c:
if b > d:
return b
else: # d >= b
return d
else: # c >= b
if c > d:
return c
else:
return d
import math
def greatest(a: float, b: float, c: float, d: float) -> float:
"""
Returns the greatest value among a, b, c, and d.
"""
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
print(greatest(math.sqrt(2), math.pi, 0, -math.sqrt(7)))
3.141592653589793
def isEven(v: int) -> bool:
"""
Returns True if v is even, otherwise returns False.
"""
print("isEven A")
return v%2==0
def describeEvenness(v: int) -> str:
"""
Returns "even" if v is even, otherwise returns "odd".
"""
print("describeEvenness A")
if isEven(v):
print("describeEvenness B")
return "even"
else:
print("describeEvenness C")
return "odd"
def printEvenness(v: int) -> None:
"""
Prints "v is even" if v is even, or "v is odd" otherwise.
"""
print("printEvenness A")
print(v, "is", describeEvenness(v))
print("printEvenness B")
printEvenness(2)
printEvenness A describeEvenness A isEven A describeEvenness B 2 is even printEvenness B
def isEven(v: int) -> bool:
"""
Returns True if v is even, otherwise returns False.
"""
return v%2==0
def describeEvenness(v: int) -> str:
"""
Returns "even" if v is even, otherwise returns "odd".
"""
if isEven(v):
return "even"
else:
return "odd"
def printEvenness(v: int) -> None:
"""
Prints "v is even" if v is even, or "v is odd" otherwise.
"""
print(v, "is", describeEvenness(v))
printEvenness(2)
2 is even
def numberCategory(v: float) -> str:
"""
Returns the category of the number v as a string, where the categories are:
"even": Even integers
"odd": Odd integers
"neg": Negative non-integers
"pos": Positive non-integers
"""
if int(v) == v:
return describeEvenness(int(v))
else:
if v > 0:
return "pos"
else:
return "neg"
print("0 is", numberCategory(0))
print("5.0 is", numberCategory(5.0))
print("pi is", numberCategory(math.pi))
print("-1/12 is", numberCategory(-1/12))
0 is even 5.0 is odd pi is pos -1/12 is neg
def numberCategory(v: float) -> str:
"""
Returns the category of the number v as a string, where the categories are:
"even": Even integers
"odd": Odd integers
"neg": Negative non-integers
"pos": Positive non-integers
"""
if int(v) == v and int(v)%2==0:
return "even"
elif int(v) == v:
return "odd"
elif v > 0:
return "pos"
else:
return "neg"
if describeEvenness(2) != "odd":
print("Whew, 2 isn't odd!")
print("foo" == "foo")
print("foo" != "bar")
print(False == False)
print(5 == "5")
print(True == 42)
print(True == 1)
Whew, 2 isn't odd! True True True False False True
print("aardvark" < "zebra")
print("Richards" > "Gregor")
print("Z" > "a")
True True False
1 / 0
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) Input In [28], in <cell line: 1>() ----> 1 1 / 0 ZeroDivisionError: division by zero
import math
print(math.inf)
inf
def safeDivide(num: float, den: float) -> float:
"""
Returns num divided by den. If den is zero, returns positive or negative infinity.
"""
if den == 0:
if num >= 0:
return math.inf
else: # den == 0 and num < 0
return -math.inf
else:
return num/den
print("2/3 is", safeDivide(2, 3))
print("pi/0 is", safeDivide(math.pi, 0))
print("0/0 is", safeDivide(0, 0))
print("-1/0 is", safeDivide(-1, 0))
2/3 is 0.6666666666666666 pi/0 is inf 0/0 is inf -1/0 is -inf
def xor(a: bool, b: bool) -> bool:
"""
Returns a xor b.
Returns True if either a or b is True, but not both. Returns False otherwise.
"""
if a:
#if b:
# return False
#else:
# return True
return not b
else: # a is False
return b
# return a != b
print(xor(1 > 2, 3 > -2))
print(xor(2 > 1, 3 > -2))
True False
def xor3(a: bool, b: bool, c: bool) -> bool:
"""
Returns True if exactly one of a, b, c is True. Otherwise, returns False.
"""
if a:
return not b and not c
else: # a is False
return xor(b, c)
print(xor3(1 > 2, 3 > -2, 0 != 0))
True