Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

2
votes
1answer
18 views

C Recursive Opendir Wrapper to Sort Directories First (ascending/descending)

A recent post on StackOverflow about a recursive directory listing which produced an unsorted mixed file/directory list, sparked the stray thought of "What would it take to write a wrapper for the ...
2
votes
1answer
61 views

Write a function that adds two numbers stored in a linked list and returns the sum

Just for fun I did the following exercise from "Cracking the coding interview. 4th edition": You have two numbers represented by a linked list, where each node contains a single digit. The digits ...
0
votes
0answers
28 views

Swift Knapsack problem using bottom-down (memoization) approach

I'm solving the "unbounded" variant of the knapsack problem, meaning the repetition of items is allowed. As in the Hackerrank version of the knapsack problem, I am returning the sum nearest to, not ...
3
votes
2answers
74 views

Find the longest sub string of a word after concatenation of given words array

An Array of N words is given. Each word consists of small letters ('a'-'z'). Our goal is to concatenate the words in such a way as to obtain a single word with longest possible substring composed of ...
1
vote
1answer
28 views

Recursive react.js navigation menu

I'm very new to React.js and have to start converting an entire website at my work. It's fun, but I'm hoping to get some feedback about how I tackled building this navigation component as I don't ...
1
vote
2answers
54 views

Recursive Transversal (Python)

For a programming challenge I was doing, I needed to traverse a list of lists, and while writing the standard 2D traversal function, I thought why not generalise it, so I did. Here's my best effort: ...
1
vote
0answers
38 views

Bin-packing-problem solution using recursion

I needed to make a freight calculator and had to solve a bin packing problem. I utilized a github package. Basically with the help of my stack overflow question I made a class Method that Splits an ...
2
votes
1answer
48 views

Most efficient way to find spatial order from a list of tuples (Python)

I have a circle-growth algorithm (line-growth with closed links) where new points are added between existing points at each iteration. The linkage information of each point is stored as a tuple in a ...
4
votes
2answers
63 views

Computing bezier curves of degree n with recursive functions

I have been looking at homemade CNC and have been wondering how curves are drawn, so I looked into it and found this cool article. I then decided to try coming up with a bezier curve algorithm in C. ...
0
votes
1answer
60 views

Find first repeating Char in String

Given a string, find the first repeating character in it. Examples: firstUnique("Vikrant")None ...
0
votes
2answers
63 views

Get property value from object tree in a recursive way

Consider the following structure, let condition = { and: [ { id: 3 }, { pageId: '1' } ] } I want to get the id property value ...
2
votes
1answer
42 views

Finding peak point in an array by using divide and conquer approach

It is to find the maximum element in an array which is first increasing and then decreasing. I've tried to write my idea by using divide and conquer approach. Is there any improvable or missing point? ...
1
vote
1answer
64 views

Rectangle packing in smallest possible area

I have written a Java Program to place a bunch of rectangles into the smallest possible rectangluar area(no overlapping and rotating). Here is a short summary: I calculate minArea and maxArea: ...
4
votes
3answers
113 views

Java Magic square program

Here is my improved version of my Magic square program from following this earlier version. I also added a few comments. Any help for improvements would be really appreciated. ...
4
votes
2answers
35 views

Replace array element with multiplication of neighbors in Scala

Given an array of integers, update the index with multiplication of previous and next integers, Input: 2 , 3, 4, 5, 6 Output: 2*3, 2*4, 3*5, 4*6, 5*6 Following ...
3
votes
2answers
141 views

Knapsack problem - recursive approach with memoization

This post is based on the 0-1 Knapsack problem. I came across this problem in Assignment #4 of Professor Tim Roughgarden's course Greedy Algorithms, Minimum Spanning Trees, and Dynamic Programming on ...
2
votes
1answer
43 views

Java n-ary Tree class with custom made methods and nested Node class

I'm a beginner and I wrote this (working) code for n-ary tree. The specifics: Each node is to be built with an integer value. Each node has a variable number of children. The nodes should have an ...
2
votes
0answers
24 views

Finding first duplicated value from “circular” list with a recursive function

Background This year I've taken on the challenge of Advent of Code. The second challenge of the first day is to calculate a frequency for a special device based on a series of numbers. Here's a quote ...
0
votes
2answers
38 views

Wrapping certain elements in an array with an array with PHP

I have a function that's doing this correctly, but I feel as though this can be done (possibly) with minimal code, with recursion, but I'm just wracking my brain. Basically I have JSON that gets ...
2
votes
2answers
89 views

A recursive algorithm to find the number of distinct quantities of money from a pile of coins

Problem: find the number of distinct quantities of money that a pile of coins can make. The vector coins contains the values of the coins, and the corresponding ...
-1
votes
1answer
40 views

Recursive loop which returns a value according to a single condition

I have a method that takes as parameter an array of several depth (group) and a name as a string (currentFilterName) ...
3
votes
1answer
133 views

Speed up Magic square program

I have written a Java program to calculate magic squares with recursion and backtracking. A 3*3 Magic square is calculated in about 1 sec, but a 4*4 needs about 50 minutes on my laptop with Intel i5. ...
1
vote
2answers
58 views

Recursive range iteration

I decided to do Advent of Code this year in D, and the following is my solution to day 8. Algorithm aside, I was specifically wondering if there was a better way to use ranges in a recursive manner ...
0
votes
1answer
52 views

Binary Reflected Gray Code generation recursively

I tried to write BRGC recursively. I've really recognized that intrinsic of recursion helps for some problems in which I think BRGC is included. is the way I wrote it recursively adequate solution? ...
2
votes
3answers
216 views

Leetcode #91 - Number of ways to decode a string

I'm working on problem 91 on Leetcode.com called Decode Ways and I've successfully managed to get a working recursive solution but it results in Time Limited ...
2
votes
1answer
42 views

Search a list recursively and return the index of the smallest value

I was asked to define a function that takes a list as an argument and returns the index of the smallest value. This is what I came up with: ...
3
votes
1answer
81 views

Checking whether given array is sorted by divide-and-conquer

I've written a code that I try to use divide and conquer approach to determine if the given array is sorted. I wonder whether I apply the approach accurately. ...
1
vote
2answers
165 views

Getting Pascal's triangle recursively in Python

I've been practicing recursion lately and wrote this code to get Pascal's triangle recursively. How would you guys improve it ? I don't really like the fact that the recursion goes for ...
1
vote
1answer
62 views

Recursive trim() function to handle arrays and objects

I have come up with a recursive function which applies trim() recursively to all string members of an array/object. I don't have any specific quandaries about my ...
3
votes
2answers
672 views

Insert a number in a sorted list and return the list with the number at the correct index

I feel like the title is a bit wordy. What I have defined here is a function that takes two parameters: a list that contains valued sorted from smallest to biggest a number to insert at the right ...
3
votes
1answer
104 views

Pouring water between two jugs to get a certain amount in one of the jugs (2)

I have been practicing recursion lately and I came up with this code to solve the water jug problem, given two jugs of volume jug1 and ...
5
votes
3answers
222 views

Picking the smallest value in a list using recursion

This is the code I wrote in order to find the smallest value in a list using recursion.However I feel like there would be a better way to do this? ...
1
vote
1answer
52 views

Check if list contains a pair which adds up to a given sum

Kindly review this scala code for given problem and suggest improvements. Problem - Given an array of integers and a target sum, check if array contains a pair which adds up to sum. Example - <...
4
votes
3answers
265 views

Password generator with multiple charset

This is just straight bruteforce with the ability to select multiple charset for one word, being able to mash up the linearity with a modulo. This code is also posted on GitHub. Generateur.h ...
-1
votes
2answers
106 views

Fibonacci Nth term using tail recursion [closed]

I have created a program that prints the Nth term of the Fibonacci sequence. The fib function needs to use tail recursion. If what I have coded isn't tail recursion, I would like to know how to change ...
3
votes
2answers
118 views

Simple recursive transformation solution in Python

This simple Python code I wrote for a problem asked during an interview. The problem statement is: Given a tuple (a, b), transform this into (c, d) by applying any of the two below operations ...
2
votes
0answers
33 views

Flatten Hierarchical Categories

I have an XLSX file that contains a hierarchical list of categories. Each category has a basic query associated with it. I need to flatten these categories which involves combining the categories. I ...
2
votes
4answers
88 views

K&R Exercise 1-19. Reverse program input one line at a time

Intro I'm going through the K&R book (2nd edition, ANSI C ver.) and want to get the most from it: learn (outdated) C and practice problem-solving at the same time. For that reason, I'm trying to ...
2
votes
2answers
60 views

Collect members from custom tree nodes

In my program, I have a tree of Group nodes, each identified by their ImportId. Each group has zero or more members, that I ...
12
votes
2answers
2k views

Tower of Hanoi Recursive Implementation using Python with OOP

I have just finished a small program for Tower of Hanoi using recursion. I did it to practice OOP which I recently learnt. It took me about 2 hours to research about the recursive algorithm and write ...
1
vote
1answer
101 views

C++ heap CPS: Continuation Passing Style with Recursion

I am willing to see how to write readable and efficient continuations on the heap in c++. I am not quite satisfied. The following is just an exercise: it is coded for fun. I post the code here ...
6
votes
1answer
218 views

Tower of Hanoi in NASM Win64 Assembly

This program is an implementation of the classic puzzle Tower of Hanoi! This is my first mostly recursive Assembly program. Any advice and all topical comments on code optimization and conventions is ...
1
vote
0answers
88 views

Recursive object/array filter by key

I have some code that takes some JSON, parses it, and given an optional filter, will attempt to find any key (or nested key) that includes the filter (partial matching - it does not match values). The ...
5
votes
1answer
177 views

Recursive Fibonacci in Rust with memoization

I'm trying to come up with an "elegant" way of calculating Fibonacci for number in Rust, using recursion and memoization (self-imposed requirements). This is what I have so far: ...
1
vote
2answers
62 views

When given 7 letters, find all possible words with length 3 to 7

I am writing a program in JavaScript to find all possible words given 7 letters. The words have to be 3 or more characters, and for right now I am limiting it to 7 characters long. The code below ...
1
vote
1answer
113 views

Scheme function to duplicate every element of a list a specified number of times

This code takes a list and a integer and duplicates every element in the list the specified number of times. ...
5
votes
2answers
185 views

Finding all possible letter combinations from an inputted phone number

Prompt: Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the ...
3
votes
1answer
283 views

Find the kth-to-last element of a singly linked list

I wrote this code for finding the \$k\$th-to-last element in a singly linked list. ...
1
vote
0answers
265 views

Filling values for nested dictionaries

I have a text file where any name followed with sequence word is considered a dictionary. The task is to go through the keys and values of a particular dictionary, which is already identified, and see ...
1
vote
2answers
31 views

Extract and divide algorithm implementation in Elixir

I just started learning Elixir and stumbled upon this challenge over on Programming Puzzles & Code Golf. It is a well-suited task for beginners, so I chose to give it a go (to be clear, give it a ...