-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibonacci.py
executable file
·52 lines (36 loc) · 1.18 KB
/
fibonacci.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
"""
Compute Fibonacci numbers
"""
__author__ = "Daniele Raffo"
__version__ = "0.1"
__date__ = "10/12/2018"
import argparse
import itertools
import sys
def parse_command_line():
"""Parse command line arguments"""
parser = argparse.ArgumentParser(description = __doc__)
parser.add_argument('n', help = 'nth Fibonacci number to compute')
parser.add_argument('-u', '--upto', action = 'store_true', help = 'Print all Fibonacci numbers up to the nth')
return parser
def iter_fibonacci():
"""Generator for Fibonacci numbers"""
(a, b) = (1, 1)
while True:
yield a
a, b = b, a + b
def get_fibonacci(nth):
"""Get the nth Fibonacci number (from the iterator function)"""
return next(itertools.islice(iter_fibonacci(), nth - 1, None))
if __name__ == '__main__':
args = parse_command_line().parse_args()
number = int(args.n)
if number < 1:
print('Error: argument must be a positive integer')
sys.exit()
if args.upto:
for i, fibonacci in zip(range(1, number + 1), iter_fibonacci()):
print('{:>4}) {:>4}'.format(i, fibonacci))
else:
print(get_fibonacci(number))