# recursion

## Logic behind Recursive functions in Python

Logic behind Recursive functions in Python Question: I have the following recursive function below that returns the k to the s power of a certain integer. However I do not understand how it works. The base class is 1 when s becomes 0. How is it so that the returned value is actually k^s when …

## Graph DFS – Confused about the return value of recursion

Graph DFS – Confused about the return value of recursion Question: I have a graph where a path from top-left cell – (0,0) to the bottom-right cell – (m-1,n-1) is valid if the difference between consecutive cells are less than or equal to "val". For instance, this example should return true for a val=2 because …

## Unravel a tree structure stored as nested list

Unravel a tree structure stored as nested list Question: I’m working on a piece of code which simulates possible paths through a series of nodes. The nodes are assigned numbers and the connected nodes are listed in a lookup table. Since the path sometimes splits in two (or many) possibilities (and sometimes only one, i.e., …

## How to roll-out (NOT FLATTEN) a list of lists in Python

How to roll-out (NOT FLATTEN) a list of lists in Python Question: Say I have a list of lists: my_list = [["a"], ["b"], ["c", "d"], ["e", "f", "g"]] I would like a function that gives each possible combination of elements: output = [["a","b","c","e"],["a","b","d","e"],["a","b","c","f"],["a","b","d","f"],["a","b","c","g"],["a","b","d","g"]] Note that the number of elements in the initial list is the …

## How can I shorten a function which outputs the chain numbers of the Collatz conjecture?

How can I shorten a function which outputs the chain numbers of the Collatz conjecture? Question: This Python function outputs a list based on the collatz conjecture. This is an unsolved maths problem where the function will perform different operations on ‘n’ depending on if it is odd or even, outputting ‘n’ to a list …

## What are possible causes of my python script ending prematurely?

What are possible causes of my python script ending prematurely? Question: I have a python script that communicates with other software through TCP/IP protocol. It receives data in XML format from the software. This worked fine. However, since it is part of a larger project I packed the modules and installed them into my project …

## Passing an equation into a function in Python?

Passing an equation into a function in Python? Question: I thought I’d try to code a Euler’s method problem into a recursive function, but I’d like to improve the reusablity of it by being able to pass in any arrangement of the x and y values for the statement. For example, in my code the …

## How to iterate recursevly a df and calculate row value

How to iterate recursevly a df and calculate row value Question: I have the following df list_columns = [‘id’,’start’, ‘end’, ‘duration’] list_data = [ [1,’2023-01-01′, ‘2023-04-02′, 0], [2,’2023-01-10′, 0, 2],[3,’0′, 0, 3],[4,’0’, 0, 4]] df= pd.DataFrame(columns=list_columns, data=list_data) For a specific id, I want to calculate the start & end if they are 0 like this: …