def main(): # Call function print(lis([-4, 10, 3, 7, 15], 0, -1)) # Save recursion tree to a file vs.make_animation("lis.gif", delay=2)
from visualiser.visualiser import Visualiser as vs st = [] @vs(show_argument_name=False, node_properties_kwargs={ "shape": "record", "color": "#f57542", "style": "filled", "fillcolor": "grey" }) def combi(prefix, s): if len(s) == 0: return " " else: st.append(prefix + s[0]) combi(prefix=prefix + s[0], s=s[1:]) combi(prefix=prefix, s=s[1:]) return st print(combi(prefix="", s='abc')) vs.make_animation("combinations.gif", delay=3)
def main(): # Call function print(fib(n=6)) # Save recursion tree to a file vs.make_animation("fibonacci.gif", delay=2)
from visualiser.visualiser import Visualiser as vs """ Problem Link: https://stackoverflow.com/questions/33808653/recursion-tree-with-fibonacci-python/60126306#60126306 """ @vs(node_properties_kwargs={ "shape": "record", "color": "#f57542", "style": "filled", "fillcolor": "grey" }) def binary(length, outstr=""): if len(outstr) == length: print(outstr) else: for i in ["0", "1"]: binary(length=length, outstr=outstr + i) binary(length=3, outstr="") vs.make_animation("binary_string.gif", delay=2)
def main(): # Call function print(max_revenue(5,prices)) # Save recursion tree to a file vs.make_animation("fibonacci.gif", delay=1)
def main(): # Call function print(merge_sort([5, 0, 1, 9])) # Save recursion tree to a file vs.make_animation("merge_sort.gif", delay=1.3)
if right_side_m > 0 and right_side_c > right_side_m: return False visited[(m, c, s)] = True if s == 1: op = -1 else: op = 1 solved = False for i in range(5): next_m, next_c, next_side = m + op * options[i][0], c + op * options[ i][1], int(not s) if is_valid(next_m, next_c): if (next_m, next_c, next_side) not in visited: solved = (solved or dfs( m=next_m, c=next_c, s=next_side, level=level + 1)) if solved: return True return solved if (dfs(m=3, c=3, s=1, level=0)): print("SOlution Found") # Save recursion tree to a file vs.make_animation("missionaries.gif", delay=2)
def main(): amount = 5 coins = [1, 2, 5] print(f(coins=coins, amount=amount, n=len(coins))) vs.make_animation("coin_change.gif", delay=3)
def main(): # Call function print(fact(n=6)) # Save recursion tree to a file vs.make_animation("factorial.gif", delay=2)
from visualiser.visualiser import Visualiser as vs """ Given an array of numbers, find all the subsets: eg: nums = [1, 2, 3] Output: [[], [1], [2], [2, 1], [3], [3, 1], [3, 2], [3, 2 , 1]] You can find my explanation here: https://qr.ae/TWHmsi """ subsets = [] @vs(ignore_args=["nums"], show_return_value=False, show_argument_name=False) def f(nums, i, current_subset): # If no more elements left if i == 0: subsets.append(current_subset) return # Exclude Current element f(nums=nums, i=i - 1, current_subset=current_subset) # Include current element f(nums=nums, i=i - 1, current_subset=current_subset + [nums[i - 1]]) if __name__ == "__main__": nums = [1, 2, 3] f(nums=nums, i=len(nums), current_subset=[]) # Save recursion tree to a file vs.make_animation("subset.gif", delay=3)