Questions tagged [recursion]

Recursion in computer science is a method of problem solving where the solution to a problem depends on solutions to smaller instances of the same problem.

Filter by
Sorted by
Tagged with
3
votes
1answer
143 views

Find the maximum possible summation of differences of consecutive elements

Array A contains the elements, \$A_1,A_2, \ldots, A_N\$. And array B contains the elements, \$B_1,B_2, \ldots, B_N\$. There is a relationship between \$A_i\$ and \$B_i\$: any element \$A_i\$ ...
1
vote
2answers
119 views

Binary Tree Maximum Turns

Given any binary tree, what is the maximum number of turns possible in any path from root to any leaf? A turn is when the path involves moving from left branch to right or vice-versa; i.e. the number ...
4
votes
1answer
3k views

Find maximum number in a nested array using recursion

Here is my code: ...
2
votes
1answer
66 views

dynamic programming solution for a string ending with 0, 1 or 2

A string that contains only 0s, 1s, and 2s is called a ternary string. Find a total ternary ...
2
votes
1answer
67 views

Recursion function subtracting accumulated percentage with <<-

I have a file with percentage of presence of micro-organisms but each category has the accumulation of percentages of the child categories. I designed a recursive function to subtract the proportion ...
2
votes
1answer
3k views

Searching JSON recursively for an item with a “selected” attribute

I have the following function, which I believe could be improved with functional-programming. ...
3
votes
1answer
41 views

Generating a list of powers in Elm up to some limit

I'm looking to generate a list of powers in Elm, up to a certain limit. I've written the following tests: ...
0
votes
2answers
237 views

Print every line of file, last to first, with recursion

Had a discussion with a friend to solve this extra credit problem, and we came up with two separate solutions. What are the differences between these two? Is one better than the other? How can I make ...
3
votes
0answers
78 views

Minimum number of basic operations to convert str1 into str2

I am to convert a given string into another using only three basic operations, that is by deleting, inserting or replacing a character. Changes can only be made to string 1 and it is given that the ...
12
votes
1answer
1k views

Flatten an array of integers in Python

The goal is to flatten an array of nested arrays of integers. For example [1, [2], [3, [4]]] should return [1, 2, 3, 4] I'm ...
2
votes
1answer
1k views

Given a binary tree, find the maximum path sum

This is a leetcode.com problem. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must ...
2
votes
1answer
6k views

Computing the powerset of a list

I've written the following code for computing the powerset of a list (we consider the list has all distinct elements, although it doesn't really matter). Can this function be further optimized? <...
9
votes
4answers
6k views

Format an integer with thousands separator (recursive implementation)

The function accepts a number n and displays it recursively with commas. As well as a general code review, I'm interested in suggestions for alternatives to ...
2
votes
3answers
10k views

Generate all permutations of a list in Python

This is my solution to the "Permutations" problem from Leetcode: Given a collection of distinct numbers, return all possible permutations. I know this is a common routine that can be done much ...
10
votes
2answers
3k views

xml is XML is xml is XML

I had to write some code for an interview, to print out the name attribute of each node. I started off going in the wrong direction, so I didn't get to finish. I wanted to finish writing the code, so ...
3
votes
2answers
310 views

Finding the nth Fibonacci number using recursive technique

EDIT So I've been working on this according to the challenge @PeterTaylor gave me. I'm not very good at math, so I've been doing a lot of reading and found this link on the Binet form. Fibonacci ...
1
vote
0answers
126 views

x as the sum of unique integers raised to power n

I've done this problem using recursion. But I'm not sure that could this be optimized for bigger values of x using memoization. ...
3
votes
2answers
748 views

Algorithm that generate all possible strings from N Tuples. Found Brute Force version, Recursive Version uses a Tree?

I am trying to figure out a solution for a problem. Consider the following tuples (it could be any number of tuples, I am using four in this example): {A, B, C, D}...
5
votes
1answer
381 views

Compute all valid configuration of eight queens on a 8x8 chess board

Write an algorithm to print all valid ways of arranging eight queens on an 8x8 chess board so that none of them attack any other. Following is my code for this. Do you see any performance or coding ...
1
vote
1answer
560 views

Compute the number of ways a given amount (cents) can be changed

Given an infinite number of different coin types (such as pennies, nickels, dimes, quarters) find out how many ways n cents can be represented. My code appears to work (although I am curious to ...
8
votes
2answers
2k views

Generate all valid combinations of n pair of parentheses

Implement an algorithm to print all valid (e.g. properly opened and closed) combinations of n pair of parentheses. Following is my implementation and I think it works. Is the algorithm efficient? ...
5
votes
1answer
121 views

Haskell limit number of recursions

I want to create a monad which will prevent more than 10 instances of recursion (which I am implementing by preventing more than 10 instances of >>=). My <...
3
votes
1answer
681 views

Word Squares Generator

I've solved the Word Squares problem: Given a set of words (without duplicates), find all word squares you can build from them. A sequence of words forms a valid word square if the kth row and ...
2
votes
1answer
318 views

A recursive web-crawler created using vba

I had a desire to make a recursive web crawler in vba. As I don't have much knowledge on vba programming, so it took me a while to understand how the pattern might be. Finally, I've created one. The ...
1
vote
1answer
55 views

Recursive max int in array

I would like a review for this simple recursive algorithm which returns the largest int in an array of integers. I would like feedback on the transfer of state via the third parameter specifically. ...
5
votes
2answers
1k views

Two approaches to print all permutations - returning versus passing through the “result” list

I have noticed many of the backtracking problems have two ways of solving. One is to return "whatever's the required list", vs passing-through the "result" to every call and appending to it. What is ...
7
votes
1answer
430 views

Solving the game WordBrain using brute force

I've been working on a program to solve the game known as WordBrain. In this game, you have to swipe your finger across a square grid of letters and trace out words of given lengths in the given order....
3
votes
0answers
190 views

Recursive JavaScript Permutations Function

I have a recursive function that calculates the permutations of a given list/array list. Although a similar implementation works great in Python, this JavaScript ...
5
votes
2answers
695 views

Permutation of objects, increase maximum limit

I am using this function to find all permutation of objects of a given length. However, right now, the maximum length I can reach before it throws an "Out of memory exception" is 9 (also, it gets too ...
1
vote
1answer
82 views

Base 256 array addition Recursively - Follow up

This is a follow up code review request from Base 256 array addition Recursively I have made the changes that were suggested for the RecursiveAdd method. Please ...
2
votes
5answers
2k views

Determining if a palindrome exists in a linked list using recursion

Summary of code: This code recurses through half of the LinkedList, "adding" the left half node's to the call stack. Then, once at the middle, the code returns the ...
12
votes
4answers
364 views

Deletable primes

I came across this problem and I was wondering if you could help me to improve my solution. Problem: Count the number of ways you can reduce a deletable prime to a one digit number. A "...
10
votes
2answers
1k views

Lovely Lucky LAMBs

I am trying to solve this question from google's foobar. However, my code exceeds the time limit, even though when I tested it I got correct answers. How can I make my code more efficient? Lovely ...
-1
votes
3answers
148 views

Base 256 array addition Recursively [closed]

Is there any way to improve private byte[] RecursiveAdd(byte[] f, byte[] s, int index, byte[] result, bool carry = false) method? The method signature is too long ...
5
votes
3answers
7k views

Javascript of property path in object

I am trying to find the path of property in an object in JavaScript. For example, given var myObj = { 'a': {'b': {'c': {'x': 1, 'y': 2 }}} } the path of 'x' in <...
2
votes
1answer
354 views

Typescript: Recursion file-system lookup

Previously I asked a question to find the best practice of recursively running a function to retrieve all import/export statements of files in NodeJs here. After some discussion I'm not satisfied yet,...
1
vote
0answers
55 views

Recursive time formatter

This is small utility class I made format.utility.ts. There are some other one off utility methods in here I removed for this. The one in question is ...
3
votes
1answer
354 views

Recursively find TypeScript files imported/exported from an entry point

I'd like to know what's the best practice as functional programming in JavaScript to work with arrays and passing them as an argument to a recursive function that may run itself within its branch. In ...
2
votes
1answer
174 views

Pairwise grouping of elements of a list

I want to group the elements in an arbitrary long list into pairs and implemented the following so far: ...
5
votes
2answers
137 views

Python program to check substring match locations with a lot of permutations of the substring

This is my first code here and also my first program of this size. I don't really know what is expected from a programmer who writes "good, readable" code. This is my first program which will be used ...
2
votes
1answer
75 views

Algorithm optimisation to get first parent with specific kind of class

I am writing an extension for UIViewController to search all the parents and return a specific parent which is a kind of specific class. here is my try : ...
1
vote
0answers
95 views

Recursive function, traversing grid

I am building game where a player must build a path from the bottom of the grid to the top. When a grid square is changed I check to see if they have completed the path (won). Because the check ...
2
votes
2answers
155 views

Recursive and Non-recursive SelectionSort

This is a naive attempt of writing a recursive and non-recursive versions for SelectionSort(). My goal is mainly to present an elegant, easy-to-understand, ...
0
votes
4answers
4k views

Array decomposing recursive function in JavaScript

What I need is to remove first word from the string again and again until only one word is left, and put it all into array. For example, I have this string as an input: ...
4
votes
1answer
167 views

Matching a type T to a list of case statements

I have a compile-time switch_ template that matches a given type T to a list of case_ ...
15
votes
3answers
2k views

Project Euler Problem #8: Largest product in a series

The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832. ...
2
votes
2answers
2k views

All possible groups of combinations of array

Let's assume I have a one-dimensional array of integers of size n. My problem is to generate all the combination of all possible groups of size 1 to n, such as each combination has exactly one ...
1
vote
1answer
97 views

Large switch statement bucketing system

I've finally got a solution for a "weekly streak" bucketing system I've been working on but unfortunately it's resulted in a very large switch statement. I'm running through a handful of dates and ...
2
votes
1answer
751 views

Regular Expression Matching: Recursive and DP-based implementation

Problem Statement: Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching ...
2
votes
1answer
107 views

Time complexity of common ancestor algorithm

I have an algorithm here to find the common ancestor of two nodes in a binary tree. ...