Skip to content

Commit de1f54a

Browse files
author
sarangbishal
committed
Add coin change example; Update README
1 parent b3acd4e commit de1f54a

File tree

5 files changed

+38
-3
lines changed

5 files changed

+38
-3
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Recursion Visualiser
2-
Recursion visualiser is a python tool that draws recursion tree for recursive function with very less code changes.
2+
Recursion visualiser is a python tool that visualizes recursion tree with animation and draws recursion tree for recursive function.
3+
It works with almost any type of recursive function.
4+
Just add the recursion-visualiser decorator to your function and let it do the rest of the work.
5+
36

47
## Installation
58
The only dependency for recursion visualiser is Graphviz which you can download from [here](https://www.graphviz.org/download/)
@@ -84,6 +87,9 @@ This is taken from one of my answers on quora where I had to manually draw recu
8487
- [x] Minimal working version
8588
- [x] Upload package to pypi
8689
- [x] Support animation
90+
- [ ] Support aliasing for function name
91+
- [ ] Show repeated states
92+
- [ ] Support node_color, backgroundcolor etc
8793
- [ ] Refactor
8894
- [ ] Handle base cases
8995
- [ ] Make more beautiful trees

examples/coin_change.gif

1.67 MB
Loading

examples/coin_change.png

94.6 KB
Loading

examples/coin_change.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Author: Bishal Sarang
2+
from visualiser.visualiser import Visualiser as vs
3+
4+
"""
5+
Number of ways to make change:
6+
"""
7+
@vs(ignore_args=["coins"], show_argument_name=False)
8+
def f(coins, amount, n):
9+
if amount == 0:
10+
return 1
11+
12+
if amount < 0:
13+
return 0
14+
15+
if n <= 0 and amount >= 1:
16+
return 0
17+
18+
include = f(coins=coins, amount=amount - coins[n - 1], n=n)
19+
exclude = f(coins=coins, amount=amount, n=n-1)
20+
21+
return include + exclude
22+
23+
def main():
24+
amount = 5
25+
coins = [1, 2, 5]
26+
print(f(coins=coins, amount=amount, n=len(coins)))
27+
vs.make_animation("coin_change.gif", delay=3)
28+
29+
if __name__ == "__main__":
30+
main()

examples/make_sum.py

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

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

0 commit comments

Comments
 (0)