Skip to content

Commit b3acd4e

Browse files
author
sarangbishal
committed
Version 1.0.0: Supports saving animation as gif
1 parent 94932da commit b3acd4e

File tree

14 files changed

+111
-22
lines changed

14 files changed

+111
-22
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
test*
22
.idea
33
.idea
4-
4+
img
5+
out*
6+
*frames*
57
# Byte-compiled / optimized / DLL files
68
__pycache__/
79
*.py[cod]

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ print(fib(6))
3636

3737
Now we want to draw the recursion tree for this function. It is as simple as adding a decorator
3838
```python
39+
# Author: Bishal Sarang
40+
3941
# Import Visualiser class from module visualiser
4042
from visualiser.visualiser import Visualiser as vs
4143

@@ -52,7 +54,7 @@ def main():
5254
# Call function
5355
print(fib(n=6))
5456
# Save recursion tree to a file
55-
vs.write_image("fibonacci.png")
57+
vs.make_animation("fibonacci.gif", delay=2)
5658

5759

5860
if __name__ == "__main__":
@@ -62,11 +64,16 @@ Here are the changes required:
6264

6365
- Add decorator Visualiser which accepts optional arguments `ignore_args`, `show_argument_name` and 'show_return_value'
6466
- Change every function calls to pass as keyword arguments.
65-
- Write the image
67+
- Make_animation
6668

69+
The output image are saved as "fibonacci.gif" and "fibonacci.png"
70+
6771
Here is how the recursion tree looks like:
68-
![enter image description here](https://github.com/sarangbishal/Recursion-Visualizer/blob/master/examples/fibonacci.png)
72+
Animation:
73+
![enter image description here](https://github.com/sarangbishal/Recursion-Visualizer/blob/master/examples/fibonacci.gif)
6974

75+
![enter image description here](https://github.com/sarangbishal/Recursion-Visualizer/blob/master/examples/fibonacci.png)
76+
7077
## 2. Make sum
7178
This is taken from one of my answers on quora where I had to manually draw recursion tree. Using Visualiser with very less changes I was able to draw the following tree. You can compare the tree [here](https://qr.ae/TltTCV)
7279
![enter image description here](https://github.com/sarangbishal/Recursion-Visualizer/blob/master/examples/make_sum.png)
@@ -76,6 +83,7 @@ This is taken from one of my answers on quora where I had to manually draw recu
7683
## TODO:
7784
- [x] Minimal working version
7885
- [x] Upload package to pypi
86+
- [x] Support animation
7987
- [ ] Refactor
8088
- [ ] Handle base cases
8189
- [ ] Make more beautiful trees

examples/fibonacci.gif

1.83 MB
Loading

examples/fibonacci.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def main():
1616
# Call function
1717
print(fib(n=6))
1818
# Save recursion tree to a file
19-
vs.write_image("fibonacci.png")
19+
vs.make_animation("fibonacci.gif", delay=2)
2020

2121

2222
if __name__ == "__main__":

examples/make_sum.gif

3.77 MB
Loading

examples/make_sum.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ def f(sum, ans):
3636

3737
# Call solve with sum and an empty list
3838
f(sum=sum, ans=[])
39-
vs.write_image("make_sum.png")
39+
# Save recursion tree to a file
40+
vs.make_animation("make.gif", delay=2)

examples/missionaries.gif

1.6 MB
Loading

examples/missionaries.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def is_valid(m, c):
1111

1212

1313
@vs(ignore_args=["node_num", "level"])
14-
def dfs(m, c, s, level, node_num):
14+
def dfs(m, c, s, level):
1515
if (m, c, s) == goal_state:
1616
return True
1717

@@ -37,14 +37,14 @@ def dfs(m, c, s, level, node_num):
3737
if is_valid(next_m, next_c):
3838

3939
if (next_m, next_c, next_side) not in visited:
40-
vs.node_count += 1
41-
solved = (solved or dfs(m=next_m, c=next_c, s=next_side, level=level + 1, node_num=vs.node_count))
40+
solved = (solved or dfs(m=next_m, c=next_c, s=next_side, level=level + 1))
4241

4342
if solved:
4443
return True
4544
return solved
4645

4746

48-
if (dfs(m=3, c=3, s=1, level=0, node_num=0)):
47+
if (dfs(m=3, c=3, s=1, level=0)):
4948
print("SOlution Found")
50-
vs.write_image("missionaries.png")
49+
# Save recursion tree to a file
50+
vs.make_animation("missionaries.gif", delay=2)

examples/subset.gif

709 KB
Loading

examples/subset.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ def f(nums, i, current_subset):
2727
if __name__ == "__main__":
2828
nums = [1, 2, 3]
2929
f(nums=nums, i = len(nums), current_subset=[])
30-
vs.write_image("subset.png")
30+
# Save recursion tree to a file
31+
vs.make_animation("subset.gif", delay=3)

0 commit comments

Comments
 (0)