Skip to content

Commit 61cdab8

Browse files
author
sarangbishal
committed
Add flag for return value
1 parent 3539452 commit 61cdab8

File tree

6 files changed

+59
-5
lines changed

6 files changed

+59
-5
lines changed

examples/fibonacci.png

26.1 KB
Loading

examples/fibonacci.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def fib(n, node_num):
1717
return left + right
1818

1919
# Call function
20-
fib(n=6, node_num=0)
20+
print(fib(n=6, node_num=0))
2121

2222
# Save recursion tree to a file
23-
vs.write_image("fibonacci")
23+
vs.write_image("fibonacci.png")

examples/make_sum.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
[7, 3]
1717
"""
1818

19-
@vs(ignore_args=['node_num'], show_argument_name=False)
19+
20+
@vs(ignore_args=['node_num'], show_argument_name=False, show_return_value=False)
2021
def f(sum, ans, node_num):
2122
# If sum becoms 0 we have found the required list
2223
if sum == 0:

examples/missionaries.png

117 KB
Loading

examples/missionaries.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from visualiser import Visualiser as vs
2+
start_state = (3, 3, 1)
3+
goal_state = (0, 0, 0)
4+
5+
options = [(2, 0), (1, 1), (0, 2), (1, 0), (0, 1)]
6+
visited = dict()
7+
8+
def is_valid(m, c):
9+
return m >= 0 and m <= 3 and c >= 0 and c <= 3
10+
11+
12+
@vs(ignore_args=["node_num", "level"])
13+
def dfs(m, c, s, level, node_num):
14+
if (m, c, s) == goal_state:
15+
return True
16+
17+
if m > 0 and c > m:
18+
return False
19+
20+
right_side_m = 3 - m
21+
right_side_c = 3 - c
22+
if right_side_m > 0 and right_side_c > right_side_m:
23+
return False
24+
25+
visited[(m, c, s)] = True
26+
27+
if s == 1:
28+
op = -1
29+
else:
30+
op = 1
31+
32+
solved = False
33+
for i in range(5):
34+
next_m, next_c, next_side = m + op * options[i][0], c + op * options[i][1], int(not s)
35+
36+
if is_valid(next_m, next_c):
37+
38+
if (next_m, next_c, next_side) not in visited:
39+
vs.node_count += 1
40+
solved = (solved or dfs(m=next_m, c=next_c, s=next_side, level=level + 1, node_num=vs.node_count))
41+
42+
if solved:
43+
return True
44+
return solved
45+
46+
47+
if (dfs(m=3, c=3, s=1, level=0, node_num=0)):
48+
print("SOlution Found")
49+
vs.write_image("missionaries.png")

examples/visualiser.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
from collections import OrderedDict
44
import pydot
55

6+
67
class Visualiser(object):
78
node_count = 0
89
graph = pydot.Dot(graph_type="digraph")
9-
def __init__(self, ignore_args, show_argument_name=True):
10+
11+
def __init__(self, ignore_args, show_argument_name=True, show_return_value=True):
1012
self.show_argument_name = show_argument_name
13+
self.show_return_value = show_return_value
1114
self.ignore_args = ignore_args
1215

1316
@classmethod
@@ -85,7 +88,8 @@ def wrapper(*args, **kwargs):
8588
result = fn(*args, **kwargs)
8689

8790
# #Child Node
88-
child_label = current_function_label
91+
92+
child_label = current_function_label + f" => {result}"
8993
child_name = current_function_signature
9094
v = pydot.Node(name=child_name, label=child_label)
9195
self.graph.add_node(v)

0 commit comments

Comments
 (0)