recursion

I don't understand how this recursive function works

I don't understand how this recursive function works Question: So these are the examples I got: def bottom_up(n): if n == 0: pass else: bottom_up(n-1) print(n) and this one, which is fine: def top_down(n): if n == 0: pass else: print(n) top_down(n-1) I have an understanding of recursion, so that isn’t the problem. My problem …

Total answers: 2

Generate All Replacements for List of Lists

Generate All Replacements for List of Lists Question: I’m building an application in Python where I need to define the following sort of function: generate_replacements([‘a’, ‘b’, [‘c’, [‘e’, ‘f’]]], 1) The expected output is all possible versions of the input list where just one element has been replaced [ [1, ‘b’, [‘c’, [‘e’, ‘f’]]], [‘a’, …

Total answers: 1

Python: recursion func for nested list

Python: recursion func for nested list Question: I need to unpack a nested list of this type: lst = [5,2,3,[4,5, (6,7, [9])]] to this: [5, 2, 3, 4, 5, 6, 7, 9] What I did: def unpack_seq(sequence: List[Any]) -> List[Any]: final_lst = [] for el in sequence: if isinstance(el, list) or isinstance(el, tuple): res = …

Total answers: 2

Python convert repeating lists into sublists

Python convert repeating lists into sublists Question: I have multiple lists containing, e.g. a = [[‘server_1’, ‘abc’], [‘server_2’, ‘abc’], [‘server_1’, ‘def’]] b = [[‘server_3’, ‘abc’], [‘server_3’, ‘def’], [‘server_4’, ‘abc’]] c = [[‘server_5’, ‘abc’], [‘server_6’, ‘abc’], [‘server_5’, ‘def’]] I’d like to transform the data into sub-lists, for example like below: OUTPUT: a = [[‘server_1’, ‘abc’, ‘def’], …

Total answers: 4

Python Recursive Tree

Python Recursive Tree Question: I’m new to programming and I want to define a function that allows me to find an element in a non-binary tree, and keep track of all the parentals of that element on a list. The tree is coded as a tuple, where index 0 is the parent, and index 1 …

Total answers: 2

How to recursively add nodes to n-ary tree in Python

How to recursively add nodes to n-ary tree in Python Question: So I have an n-ary tree with a root node that I have manually added 5 child nodes too. Each of these children nodes represents a state of its parent that has had some calculations done to its data. For example: child1.value = root.value …

Total answers: 1

Why it seems contradictory when yield meet recursion in python?

Why it seems contradictory when yield meet recursion in python? Question: I tried to implement full permutation via python generator & recursion, but the output from ‘print’ is different from any other form of generator utility. def gen_perms(seq): seq_len = len(seq) def swap(i,j): x = seq[i] seq[i] = seq[j] seq[j] = x def perms(n): if …

Total answers: 1

Python: Recursively split strings when longer than max allowed characters, by the last occurrence of a delimiter found before max allowed characters

Python: Recursively split strings when longer than max allowed characters, by the last occurrence of a delimiter found before max allowed characters Question: I have a text transcript of dialogue, consisting of strings of variable length. The string lengths can be anywhere from a few characters to thousands of characters. I want Python to transform …

Total answers: 1

Python Dynamic Programming Problem – ( 2 dimension recursion stuck in infinite loop )

Python Dynamic Programming Problem – ( 2 dimension recursion stuck in infinite loop ) Question: In the book "A Practical Guide to Quantitative Finance Interview", there is a question called Dynamic Card Game, 5.3 Dynamic Programming) The solution according to the book is basically the following: E[f(b,r)] = max(b−r,(b/(b+r))∗E[f(b−1,r)]+(r/(b+r))∗E[f(b,r−1)]) with the following boundary conditions. f(0,r)=0, …

Total answers: 3