Last active
October 21, 2023 00:27
-
-
Save TG-Techie/a97d605385a44a3808f6741d7255afa3 to your computer and use it in GitHub Desktop.
CS250 2023Fall HW3 Problem 4.1.6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
CS250 2023Fall HW3 Problem 4.1.6 | |
Author: Jonah Y-M | |
Copyright: (CC BY-SA 4.0) Attribution-ShareAlike 4.0 International | |
""" | |
import sys | |
assert sys.version_info >= (3, 12), "Python 3.12+ required." | |
# --- setup --- | |
TEST_RANGE = 16 | |
type natural = int | |
# --- the natual minus function --- | |
def minus(x: natural, y: natural) -> natural: | |
""" x - y for natural numbers """ | |
assert x >= 0 | |
assert y >= 0 | |
if y > x: | |
return 0 | |
if y > 0: | |
x = minus(x - 1, y - 1) | |
return x | |
# --- test --- | |
if __name__ == "__main__": | |
res: dict[tuple[int, int], tuple[int, int]] = { | |
(x, y): ( | |
x - y if x >= y else 0, # python result for `minus(x, y)` | |
minus(x, y), # test target, my result for `minus(x, y)` | |
) | |
for x in range(TEST_RANGE) | |
for y in range(TEST_RANGE) | |
} | |
for (x, y), (py_res, my_res) in res.items(): | |
print(f"minus({x}, {y}) = {my_res} (expected {py_res})") | |
assert py_res == my_res | |
else: | |
print("All tests passed!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment