Skip to content

Commit 53af994

Browse files
authored
Add Zeckendorf Tests (#5299)
1 parent e523c10 commit 53af994

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

.glotter.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,3 +1408,49 @@ projects:
14081408
expected: "Usage: please enter the dimension of the matrix and the serialized matrix"
14091409
transformations:
14101410
- "strip"
1411+
zeckendorf:
1412+
words:
1413+
- "zeckendorf"
1414+
requires_parameters: true
1415+
tests:
1416+
zeckendorf_valid:
1417+
params:
1418+
- name: "zero"
1419+
input: '"0"'
1420+
expected: ""
1421+
- name: "small fibonacci value"
1422+
input: '"55"'
1423+
expected: "55"
1424+
- name: "small non-fibonacci value"
1425+
input: '"67"'
1426+
expected: "55, 8, 3, 1"
1427+
- name: "large fibonacci value"
1428+
input: '"10946"'
1429+
expected: "10946"
1430+
- name: "large non-fibonacci value"
1431+
input: '"16383"'
1432+
expected: "10946, 4181, 987, 233, 34, 2"
1433+
transformations:
1434+
- remove:
1435+
- "["
1436+
- "]"
1437+
- "strip"
1438+
zeckendorf_invalid:
1439+
params:
1440+
- name: "no input"
1441+
input: null
1442+
expected: "Usage: please input a non-negative integer"
1443+
- name: "empty input"
1444+
input: '""'
1445+
expected: "Usage: please input a non-negative integer"
1446+
- name: "negative input"
1447+
input: '"-2"'
1448+
expected: "Usage: please input a non-negative integer"
1449+
- name: "floating point input"
1450+
input: '"2.6"'
1451+
expected: "Usage: please input a non-negative integer"
1452+
- name: "non-numeric input"
1453+
input: '"bad"'
1454+
expected: "Usage: please input a non-negative integer"
1455+
transformations:
1456+
- "strip"

archive/p/python/zeckendorf.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import sys
2+
from typing import NoReturn
3+
4+
5+
def usage() -> NoReturn:
6+
print("Usage: please input a non-negative integer")
7+
sys.exit(1)
8+
9+
10+
def fibonacci_up_to(n: int) -> list[int]:
11+
result: list[int] = []
12+
a = 1
13+
b = 2
14+
while a <= n:
15+
result.append(a)
16+
a, b = b, a + b
17+
18+
return result
19+
20+
21+
def zeckendorf(n: int) -> list[int]:
22+
result: list[int] = []
23+
fibs = fibonacci_up_to(n)
24+
idx = len(fibs) - 1
25+
while idx >= 0 and n > 0:
26+
fib = fibs[idx]
27+
if fib <= n:
28+
n -= fib
29+
result.append(fib)
30+
idx -= 2
31+
else:
32+
idx -= 1
33+
34+
return result
35+
36+
37+
def main() -> int:
38+
if len(sys.argv) < 2:
39+
usage()
40+
41+
try:
42+
n = int(sys.argv[1])
43+
except ValueError:
44+
usage()
45+
46+
if n < 0:
47+
usage()
48+
49+
result = zeckendorf(n)
50+
print(result)
51+
52+
53+
if __name__ == "__main__":
54+
main()

0 commit comments

Comments
 (0)