diff options
author | Braxton Hall | 2022-10-27 06:35:15 +0000 |
---|---|---|
committer | GitHub | 2022-10-27 06:35:15 +0000 |
commit | ed167a009dba7e79f52f1bbe89f8fee80a4d29a2 (patch) | |
tree | 37c64b5d5a73156866813ebdcb2e443423f600c3 | |
parent | 27b827cae18ec64e22de14379758fdf33f703fcd (diff) | |
parent | 443cccd5a996964c61c634ce239de62db0ecedcf (diff) |
Merge pull request #54 from zgrannan/main
Add human in the loop fib
-rwxr-xr-x | entries/zgrannan/fib.py | 44 | ||||
-rw-r--r-- | people.json | 4 |
2 files changed, 48 insertions, 0 deletions
diff --git a/entries/zgrannan/fib.py b/entries/zgrannan/fib.py new file mode 100755 index 0000000..ee5c678 --- /dev/null +++ b/entries/zgrannan/fib.py @@ -0,0 +1,44 @@ +# Human in the loop fibonacci + +from functools import cache +import sys + +req = 0 +cur = 0 + +def compute(query, check): + global cur, req + while True: + try: + result = int(input(f"Please compute {query}: ")) + if check(result): + break + except ValueError: + pass + print("Hmm, that didn't seem right...") + cur += 1 + print(f"Computation {cur / req:2.2%} complete.") + return result + +@cache +def add(x, y): + return compute(f"{x} + {y}", lambda r : r - x == y) + +@cache +def sub(x, y): + return compute(f"{x} - {y}", lambda r : r + y == x) + +def fib(n): + if n <= 1: + return n + else: + return add(fib(sub(n, 1)), fib(sub(n, 2))) + +try: + n = int(sys.argv[-1]) +except ValueError: + n = 10 + +req = 3 * (n - 1) +result = fib(n) +print(f"fib({n}) = {result}") diff --git a/people.json b/people.json index f1d8014..79747a8 100644 --- a/people.json +++ b/people.json @@ -381,6 +381,10 @@ { "name": "haskell", "link": "./entries/zgrannan/Fib.hs" + }, + { + "name": "human in the loop", + "link": "./entries/zgrannan/fib.py" } ] }, |