diff --git a/.glotter.yml b/.glotter.yml index b5f21b8a9..67b9a5155 100644 --- a/.glotter.yml +++ b/.glotter.yml @@ -1408,3 +1408,49 @@ projects: expected: "Usage: please enter the dimension of the matrix and the serialized matrix" transformations: - "strip" + zeckendorf: + words: + - "zeckendorf" + requires_parameters: true + tests: + zeckendorf_valid: + params: + - name: "zero" + input: '"0"' + expected: "" + - name: "small fibonacci value" + input: '"55"' + expected: "55" + - name: "small non-fibonacci value" + input: '"67"' + expected: "55, 8, 3, 1" + - name: "large fibonacci value" + input: '"10946"' + expected: "10946" + - name: "large non-fibonacci value" + input: '"16383"' + expected: "10946, 4181, 987, 233, 34, 2" + transformations: + - remove: + - "[" + - "]" + - "strip" + zeckendorf_invalid: + params: + - name: "no input" + input: null + expected: "Usage: please input a non-negative integer" + - name: "empty input" + input: '""' + expected: "Usage: please input a non-negative integer" + - name: "negative input" + input: '"-2"' + expected: "Usage: please input a non-negative integer" + - name: "floating point input" + input: '"2.6"' + expected: "Usage: please input a non-negative integer" + - name: "non-numeric input" + input: '"bad"' + expected: "Usage: please input a non-negative integer" + transformations: + - "strip" diff --git a/archive/p/python/zeckendorf.py b/archive/p/python/zeckendorf.py new file mode 100644 index 000000000..158a17c9b --- /dev/null +++ b/archive/p/python/zeckendorf.py @@ -0,0 +1,54 @@ +import sys +from typing import NoReturn + + +def usage() -> NoReturn: + print("Usage: please input a non-negative integer") + sys.exit(1) + + +def fibonacci_up_to(n: int) -> list[int]: + result: list[int] = [] + a = 1 + b = 2 + while a <= n: + result.append(a) + a, b = b, a + b + + return result + + +def zeckendorf(n: int) -> list[int]: + result: list[int] = [] + fibs = fibonacci_up_to(n) + idx = len(fibs) - 1 + while idx >= 0 and n > 0: + fib = fibs[idx] + if fib <= n: + n -= fib + result.append(fib) + idx -= 2 + else: + idx -= 1 + + return result + + +def main() -> int: + if len(sys.argv) < 2: + usage() + + try: + n = int(sys.argv[1]) + except ValueError: + usage() + + if n < 0: + usage() + + result = zeckendorf(n) + print(result) + + +if __name__ == "__main__": + main()