Skip to content

Commit 64ef498

Browse files
authored
Merge branch 'master' into master
2 parents 033665f + 0ee7a5b commit 64ef498

File tree

13 files changed

+129
-26
lines changed

13 files changed

+129
-26
lines changed

MANIFEST.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
include LICENSE
2+
include MANIFEST.in
3+
include README.rst
4+
include requirements.txt
5+
6+
recursive-include pygorithm *
7+
8+
recursive-exclude * __pycache__
9+
recursive-exclude * *.py[co]
10+
recursive-exclude * *.orig

docs/Data_Structure.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Stack
7272

7373
.. automodule:: pygorithm.data_structures.stack
7474

75-
Stack
76-
-----
77-
.. autoclass:: InfixToPostfix
78-
:members:
75+
Infix to Postfix
76+
----------------
77+
.. autoclass:: InfixToPostfix
78+
:members:

docs/Fibonacci.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ Features
1818
--------
1919

2020
* Fibonacci implementations available:
21+
- Generator
22+
- Golden ratio
23+
- Memorization (which saves some recursions to avoid computation of same series again and again)
2124
- Recursion
22-
- Cache (which saves some recursions to avoid computation of same series again and again)
2325

2426
* Get the code used for any of the implementation
2527

@@ -36,7 +38,7 @@ Features
3638
3739
>>> from pygorithm.fibonacci import modules
3840
>>> modules()
39-
['cache', 'recursion']
41+
['generator', 'goldenratio', 'memoization', 'recursion']
4042
4143
Implementations API
4244
-------------------

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
# -- General configuration ------------------------------------------------
1414
project = u'pygorithm'
15-
version = release = u'0.1.dev3'
15+
version = release = u'0.1'
1616

1717
# Add any Sphinx extension module names here, as strings. They can be
1818
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom

pygorithm/fibonacci/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
from pygorithm.fibonacci import cache
1+
from pygorithm.fibonacci import generator
2+
from pygorithm.fibonacci import goldenratio
3+
from pygorithm.fibonacci import memoization
4+
from pygorithm.fibonacci.modules import modules
25
from pygorithm.fibonacci import recursion
3-
from . modules import modules

pygorithm/fibonacci/generator.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Fibonacci implementation through generator.
3+
"""
4+
5+
import inspect
6+
7+
8+
def get_sequence(n):
9+
"""
10+
Return Fibonacci sequence from zero to specified number as list.
11+
"""
12+
def fib():
13+
"""
14+
Return Fibonacci value by specified number as integer.
15+
16+
Golden ratio — https://en.wikipedia.org/wiki/Golden_ratio
17+
Fibonacci's relation to the golden ratio — https://en.wikipedia.org/wiki/Fibonacci_number#Closed-form_expression
18+
"""
19+
a, b = 0, 1
20+
21+
while True:
22+
yield a
23+
24+
a, b = b, a + b
25+
26+
def sequence(n):
27+
"""
28+
Return sequence of Fibonacci values as list.
29+
"""
30+
f = fib()
31+
return [f.__next__() for _ in range(n + 1)]
32+
33+
return sequence(n)
34+
35+
36+
def get_code():
37+
"""
38+
Return source code of Fibonacci sequence logic's implementation.
39+
"""
40+
return inspect.getsource(get_sequence)

pygorithm/fibonacci/goldenratio.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
Fibonacci implementation through golden ratio (math formula).
3+
"""
4+
5+
import inspect
6+
import math
7+
8+
9+
def get_sequence(n):
10+
"""
11+
Return Fibonacci sequence from zero to specified number as list.
12+
"""
13+
def fib(n):
14+
"""
15+
Return Fibonacci value by specified number as integer.
16+
17+
Golden ratio — https://en.wikipedia.org/wiki/Golden_ratio
18+
Fibonacci's relation to the golden ratio — https://en.wikipedia.org/wiki/Fibonacci_number#Closed-form_expression
19+
"""
20+
golden_ratio = (1 + math.sqrt(5)) / 2
21+
22+
val = (golden_ratio ** n - (1 - golden_ratio) ** n) / math.sqrt(5)
23+
24+
return int(val)
25+
26+
def sequence(n):
27+
"""
28+
Return sequence of Fibonacci values as list.
29+
"""
30+
return [fib(value) for value in range(n + 1)]
31+
32+
return sequence(n)
33+
34+
35+
def get_code():
36+
"""
37+
Return source code of Fibonacci sequence logic's implementation.
38+
"""
39+
return inspect.getsource(get_sequence)

pygorithm/fibonacci/cache.py renamed to pygorithm/fibonacci/memoization.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ def fib(n):
2424

2525
def sequence(n):
2626
"""
27-
Return sequence if Fibonacci values as list.
27+
Return sequence of Fibonacci values as list.
2828
"""
2929
return [fib(value) for value in range(n + 1)]
3030

3131
return sequence(n)
3232

33+
3334
def get_code():
3435
"""
3536
Return source code of Fibonacci sequence logic's implementation.

pygorithm/fibonacci/modules.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1+
"""
2+
Find all modules in Fibonacci logic.
3+
"""
4+
15
import pkgutil
6+
7+
import pygorithm.fibonacci
8+
9+
210
def modules():
311
"""
4-
Find all functions in pygorithm.data_structures
12+
Find all functions in `pygorithm.fibonacci`.
513
"""
6-
import pygorithm.fibonacci
714
package = pygorithm.fibonacci
8-
modules = []
9-
for importer, modname, ispkg in pkgutil.iter_modules(package.__path__):
10-
modules.append(modname)
11-
modules.remove('modules')
12-
modules.sort()
15+
16+
modules = sorted([
17+
modname for _, modname, __ in pkgutil.iter_modules(package.__path__) if modname != 'modules'
18+
])
19+
1320
return modules

pygorithm/fibonacci/recursion.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,14 @@ def fib(n):
1313
"""
1414
Return Fibonacci value by specified number as integer.
1515
"""
16-
if n == 0:
17-
return 0
18-
19-
if n == 1:
20-
return 1
16+
if n <= 1:
17+
return n
2118

2219
return fib(n - 1) + fib(n - 2)
2320

2421
def sequence(n):
2522
"""
26-
Return sequence if Fibonacci values as list.
23+
Return sequence of Fibonacci values as list.
2724
"""
2825
return [fib(value) for value in range(n + 1)]
2926

0 commit comments

Comments
 (0)