# What is the difference between using a recursive function in Python and JS?

Advertisements Trying to use recursive function in JS, but code isn`t work: let arr = [6, 5, 3, 1, 1, 1, 1] function sum(ars, i) { if (ars.length == i) { return 0 } return ars[i] + sum(ars) } console.log(sum(arr, 0)) // Output: 6 But the same code works in python: arr = [2,5,3,1,1,1,1] def… Read More What is the difference between using a recursive function in Python and JS?

# Given an array of 2 numbers and a sum d find all the sequence of numbers that equal that sum

Advertisements We are given an array of 2 elements and we are required to find every sequence where their sum is equal to d using recursion For example given array v[]={2,6} and d=10 the function should print out 4 solutions {2,2,2,2,2} {2,2,6} {2,6,2} {6,2,2} Thing is i cant think of any idea on how i… Read More Given an array of 2 numbers and a sum d find all the sequence of numbers that equal that sum

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

Advertisements 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 same of the number of elements in the output sublists. The… Read More How to roll-out (NOT FLATTEN) a list of lists in Python

# See if value exists in Python nested dict which contains dicts, lists, and strings

Advertisements I’m not sure if there is some library that handles this, or if recursion is the way to go. I am trying to write a recursion function, when I call it it always ends up returning False. Any help appreciated. item starts out as a dict. text is a string to match. def match(item,… Read More See if value exists in Python nested dict which contains dicts, lists, and strings

# Recursive function of printing numbers in C++

Advertisements How to write a recursive function of printing numbers first in increasing order and then in decreasing order . Input: 5 Output: 1 2 3 4 5 5 4 3 2 1 I was trying to do it with single variable but it didn’t worked. #include<iostream> using namespace std; void print(int n){ //base case:… Read More Recursive function of printing numbers in C++

# Is n power of three – Python

Advertisements Leet-Code 326. Power of Three: Question Link: https://leetcode.com/problems/power-of-three/description/ My Code: class Solution: def isPowerOfThree(self, n: int) -> bool: print(n) if n % 3 == 0: if n == 3: return True return self.isPowerOfThree(n/3) else: return False I a getting the following error. Any help! RecursionError: maximum recursion depth exceeded while getting the str of… Read More Is n power of three – Python

# Fibonacci in recursive and normal fibonacci?

Advertisements So I have made 2 versions of Fibonacci calculator, one is in normal way and one is in recursion. public static int Fib(int n) { if (n == 0) { return 0; } if(n == 1) { return 1; } int a = 0; int b = 1; int res = 0; for (int… Read More Fibonacci in recursive and normal fibonacci?

# I don't understand how this recursive function works

Advertisements 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 is the result for bottom_down(): bottom_up(5) 1 2… Read More I don't understand how this recursive function works

# Generate All Replacements for List of Lists

Advertisements 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’, 1, [‘c’, [‘e’, ‘f’]]], [‘a’, ‘b’, 1],… Read More Generate All Replacements for List of Lists

# Does R store the values of recursive functions that it has obtained?

Advertisements I have used Mathematica for many years and have just started using R for programming. In both programs we can define recursive functions. In Mathematica there is a way to save values of functions. I am not sure if this is the default setting for R. Take the Fibonacci numbers for example. In Mathematica… Read More Does R store the values of recursive functions that it has obtained?