-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfibonacci_module.py
50 lines (41 loc) · 1.6 KB
/
fibonacci_module.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
"""Fibonacci numbers module.
@see: https://docs.python.org/3/tutorial/modules.html
A module is a file containing Python definitions and statements. The file name is the module name
with the suffix .py appended. Within a module, the module’s name (as a string) is available as the
value of the global variable __name__.
"""
def fibonacci_at_position(position):
"""Return Fibonacci number at specified position"""
current_position = 0
previous_number, current_number = 0, 1
while current_position < position:
current_position += 1
previous_number, current_number = (
current_number,
previous_number + current_number,
)
return previous_number
def fibonacci_smaller_than(limit):
"""Return Fibonacci series up to limit"""
result = []
previous_number, current_number = 0, 1
while previous_number < limit:
result.append(previous_number)
previous_number, current_number = (
current_number,
previous_number + current_number,
)
return result
# When you run a Python module with:
#
# >>> python fibonacci.py <arguments>
#
# the code in the module will be executed, just as if you imported it, but with
# the __name__ set to "__main__". That means that by adding this code at the end of your module
# you can make the file usable as a script as well as an importable module, because the code that
# parses the command line only runs if the module is executed as the “main” file:
#
# >>> python fibonacci.py 50
if __name__ == "__main__":
import sys
print(fibonacci_smaller_than(int(sys.argv[1])))