Skip to content

Commit 91be44f

Browse files
author
sarangbishal
committed
Add examples
1 parent 3e18e18 commit 91be44f

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

examples/Visualiser/__init__.py

Whitespace-only changes.

examples/fibonacci.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Author: Bishal Sarang
2+
3+
# Import Visualiser class from module visualiser
4+
from visualiser import Visualiser as vs
5+
6+
# Add decorator
7+
# Decorator accepts arguments: ignore_args and show_argument_name
8+
@vs(ignore_args=['node_num'])
9+
def fib(n, node_num):
10+
if n <= 1:
11+
return n
12+
vs.node_count += 1
13+
left = fib(n=n - 1, node_num=vs.node_count)
14+
15+
vs.node_count += 1
16+
right = fib(n=n - 2, node_num=vs.node_count)
17+
return left + right
18+
19+
# Call function
20+
fib(n=6, node_num=0)
21+
22+
# Save recursion tree to a file
23+
vs.write_image("fibonacci")

examples/make_sum.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Author: Bishal Sarang
2+
from visualiser import Visualiser as vs
3+
4+
"""
5+
Problemm Link: https://qr.ae/TltTCV
6+
Find all permutations of 2, 3, and 7 that can add up to make 10. (Ex: 2,2,2,2,2; or 3,7)
7+
Output:
8+
[2, 2, 2, 2, 2]
9+
[2, 2, 3, 3]
10+
[2, 3, 2, 3]
11+
[2, 3, 3, 2]
12+
[3, 2, 2, 3]
13+
[3, 2, 3, 2]
14+
[3, 3, 2, 2]
15+
[3, 7]
16+
[7, 3]
17+
"""
18+
19+
@vs(ignore_args=['node_num'], show_argument_name=False)
20+
def f(sum, ans, node_num):
21+
# If sum becoms 0 we have found the required list
22+
if sum == 0:
23+
print(ans)
24+
25+
# Include every other element to make the sum
26+
# Number that is included also can be included
27+
for elem in nums:
28+
if sum - elem >= 0:
29+
30+
# Increment vs.node_count before each function call
31+
vs.node_count += 1
32+
f(sum=sum - elem, ans=ans + [elem], node_num=vs.node_count)
33+
34+
35+
# We want to make the sum from list nums
36+
nums = [2, 3, 7]
37+
sum = 10
38+
39+
# Call solve with sum and an empty list
40+
f(sum=sum, ans=[], node_num=0)
41+
vs.write_image("make_sum.png")

visualiser.py renamed to examples/visualiser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ def __init__(self, ignore_args, show_argument_name=True):
1111
self.ignore_args = ignore_args
1212

1313
@classmethod
14-
def write_image(self, filename="out"):
15-
self.graph.write_png(f"{filename}.png")
14+
def write_image(self, filename="out.png"):
15+
self.graph.write_png(f"{filename}")
1616

1717
def __call__(self, fn):
1818
@wraps(fn)
@@ -94,7 +94,7 @@ def wrapper(*args, **kwargs):
9494
u = None
9595

9696
if caller_function_name != '<module>':
97-
print(f"Called {current_function_signature} by {caller_func_signature}")
97+
print(f"Called {current_function_label} by {caller_func_label}")
9898
u = pydot.Node(name=caller_func_signature, label=caller_func_label)
9999
self.graph.add_node(u)
100100
edge = pydot.Edge(u, v)

0 commit comments

Comments
 (0)