recursion

Cutting an array into consistent pieces of any size, with recursion

Cutting an array into consistent pieces of any size, with recursion Question: The problem is to, given an array, write a generator function that will yield all combinations of cutting the array into consistent pieces(arrays of elements that are consecutive in the given array) of any size and which together make up the whole given …

Total answers: 1

How do you count the number of negative items in a list using a recursive function?

How do you count the number of negative items in a list using a recursive function? Question: I have to make a recursive function that counts how many negative values there are in a given list, but I can’t figure out what I am supposed to return for each conditional. def countNegatives(list): """Takes in a …

Total answers: 6

Recursion on odd to be front, even in the back

Recursion on odd to be front, even in the back Question: I am new to python. I am writing a recusion to returns a COPY of the list with odds at front, evens in the back. For example: [3,4,5,6] returns [3,5,6,4]. How should I break the problem into small pieces. def oddsevens(thelist): if thelist == …

Total answers: 1

Python recursive generator breaks when using list() and append() keywords

Python recursive generator breaks when using list() and append() keywords Question: I have only recently learned about coroutines using generators and tried to implement the concept in the following recursive function: def _recursive_nWay_generator(input: list, output={}): ”’ Helper function; used to generate parameter-value pairs to submit to the model for the simulation. Parameters ———- input : …

Total answers: 1

Knapsack with SPECIFIC AMOUNT of items from different groups

Knapsack with SPECIFIC AMOUNT of items from different groups Question: So this is a variation of the Knapsack Problem I came with the other day. It is like a 0-1 Knapsack Problem where there are multiple groups and each item belongs to only one group. The goal is to maximize the profits subject to the …

Total answers: 1

Recursive function to go through list of references to indices in same list

Recursive function to go through list of references to indices in same list Question: I have this list: [[1, 2, 3, 4], [5], [6, 7], [8], [9], [10, 11], [12, 13, 14], [15, 16], [15, 16], [15, 16], [17], [18], [19], [20], [21], [20], [21], [], [], [], [], []] It could be described as …

Total answers: 2

How to get the list of children and grandchildren from a nested structure?

How to get the list of children and grandchildren from a nested structure? Question: Given this dictionary of parent-children relations, { 2: [8, 7], 8: [9, 10], 10: [11], 15: [16, 17], } I’d like to get the list of all children, grandchildren, great-grandchildren, etc. — e.g. given a parent with an ID 2 I …

Total answers: 1

How can I use tuples in recursive functions?

How can I use tuples in recursive functions? Question: I have to make a function where the input is a tuple consisting of 3 elements: elements 1 and 3 are numbers or other tuples with the same structure, and the second element is a string indicating an operation. An example would be (10, ‘-‘, (5, …

Total answers: 1

C programming recursion segmentation fault

C programming recursion segmentation fault Question: I am trying to using with recursion function. But I got failed which is segmentation fault. #include <stdio.h> int factorial( int x ); int main(){ factorial(4); return 0; } int factorial( int x ){ return x* factorial(x-1); } I have seen the same code in Python and C programming …

Total answers: 2

Recursion output isn't printing

Recursion output isn't printing Question: I’m new to python and trying to understand recursion. I’m trying to write a code where someone inputs 2 numbers (Num1, Num2). A calculation will take place until Num1 is greater than Num 2. The result of the calculation’s final value should then be outputted. This is my code: def …

Total answers: 1