Assertions
This page is under construction. Please come back later.
#!/usr/bin/env python3; import Utils TEST_BASE = 0.9 def exponentiation(base, powerArg) : assert powerArg >= 0, "powerArg >= 0" power = powerArg result = 1 while (power > 0) : result *= base power -= 1 return result # Begin Main for p in range(10, -1 + -1, -1) : print("{0:f} ^ {1:d} = {2:f}".format(TEST_BASE, p, exponentiation(TEST_BASE, p)))
Output
$ python3 Assertions1.py
0.900000 ^ 10 = 0.348678
0.900000 ^ 9 = 0.387420
0.900000 ^ 8 = 0.430467
0.900000 ^ 7 = 0.478297
0.900000 ^ 6 = 0.531441
0.900000 ^ 5 = 0.590490
0.900000 ^ 4 = 0.656100
0.900000 ^ 3 = 0.729000
0.900000 ^ 2 = 0.810000
0.900000 ^ 1 = 0.900000
0.900000 ^ 0 = 1.000000
Traceback (most recent call last):
File "/Users/rich/Desktop/Resources/pureprogrammer/py/examples/Assertions1.py", line 17, in <module>
print("{0:f} ^ {1:d} = {2:f}".format(TEST_BASE, p, exponentiation(TEST_BASE, p)))
File "/Users/rich/Desktop/Resources/pureprogrammer/py/examples/Assertions1.py", line 7, in exponentiation
assert powerArg >= 0, "powerArg >= 0"
AssertionError: powerArg >= 0
#!/usr/bin/env python3; import Utils TEST_BASE = 0.9 def exponentiation(base, powerArg) : assert powerArg >= 0, "exponentiation() power must be non-negative, was " + str(powerArg) power = powerArg result = 1 while (power > 0) : result *= base power -= 1 return result # Begin Main for p in range(10, -1 + -1, -1) : print("{0:f} ^ {1:d} = {2:f}".format(TEST_BASE, p, exponentiation(TEST_BASE, p)))
Output
$ python3 Assertions2.py
0.900000 ^ 10 = 0.348678
0.900000 ^ 9 = 0.387420
0.900000 ^ 8 = 0.430467
0.900000 ^ 7 = 0.478297
0.900000 ^ 6 = 0.531441
0.900000 ^ 5 = 0.590490
0.900000 ^ 4 = 0.656100
0.900000 ^ 3 = 0.729000
0.900000 ^ 2 = 0.810000
0.900000 ^ 1 = 0.900000
0.900000 ^ 0 = 1.000000
Traceback (most recent call last):
File "/Users/rich/Desktop/Resources/pureprogrammer/py/examples/Assertions2.py", line 17, in <module>
print("{0:f} ^ {1:d} = {2:f}".format(TEST_BASE, p, exponentiation(TEST_BASE, p)))
File "/Users/rich/Desktop/Resources/pureprogrammer/py/examples/Assertions2.py", line 7, in exponentiation
assert powerArg >= 0, "exponentiation() power must be non-negative, was " + str(powerArg)
AssertionError: exponentiation() power must be non-negative, was -1
#!/usr/bin/env python3; import Utils TEST_BASE = 0.9 def exponentiation(base, powerArg) : assert powerArg >= 0, "exponentiation() power must be non-negative, was " + str(powerArg) power = powerArg result = 1 while (power > 0) : result *= base power -= 1 # This line will make the result incorrect. result = -result # If power is odd and base is negative, then result should be negative. # Otherwise result should be positive. assert result < 0 if (Utils.bitwiseAnd32(power, 1)) == 1 and (base < 0) else result >= 0, "Postcondition Failed: Result is wrong sign" return result # Begin Main for p in range(10, -1 + -1, -1) : print("{0:f} ^ {1:d} = {2:f}".format(TEST_BASE, p, exponentiation(TEST_BASE, p)))
Output
$ python3 Assertions3.py
Traceback (most recent call last):
File "/Users/rich/Desktop/Resources/pureprogrammer/py/examples/Assertions3.py", line 22, in <module>
print("{0:f} ^ {1:d} = {2:f}".format(TEST_BASE, p, exponentiation(TEST_BASE, p)))
File "/Users/rich/Desktop/Resources/pureprogrammer/py/examples/Assertions3.py", line 17, in exponentiation
assert result < 0 if (Utils.bitwiseAnd32(power, 1)) == 1 and (base < 0) else result >= 0, "Postcondition Failed: Result is wrong sign"
AssertionError: Postcondition Failed: Result is wrong sign
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
- Base Conversion
- Fast Exponentiation
- Fibonacci Numbers with Memoization
- Football Passer Rating (with Assertions)
- Julian Day Number
- Number of Primes (Estimate)
- Ordinal Date
- Ordinal Date (Revisited)
- Shuffle Deck
- Significant Digits
References
- [[Python Language Reference]]
- [[Python Standard Library]]
- [[Python at TutorialsPoint]]