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
2
votes
0answers
22 views

File and folder filter for a “last-updated explorer”

Sometimes I save a file somewhere but can't find it after or I install something and want to see what files were added. For this reason, I created a winforms app which only displayes files created/...
3
votes
1answer
35 views

User defined function call on a stack based interpreter for concatenative language

I would like to receive some advice on how to improve a small concatenative stack-based interpreter, executing a joy-like programming language. It is really minimal, and is in fact a subset of a ...
7
votes
1answer
90 views

Recursive function to get unique properties

I just wanted to share my newly created JavaScript code. This code is responsible for passing back only the unique elements for a given array. This one is quite useful when you have a huge array and ...
7
votes
1answer
56 views

Apply arbitrary action during recursive generator function

I've got this recursive generator function. It will traverse an object looking for "leafs", any keys that don't point to sub-objects. Then it applies one of two actions on the leaf and yields it. Any ...
4
votes
2answers
104 views

Get list of all combinations from a set

Given a tuple of elements, in elementTupleParam and the number in each combination r, we compute all the possible combinations ...
1
vote
1answer
45 views

Tower of Hanoi, ASCII animation with ANSI escape codes

A few days ago I saw a nice video on YouTube with prof Thorsten Altenkirch about recursion and the Tower of Hanoi puzzle. Today I tried to reproduce the code from the video and came to this: ...
7
votes
2answers
111 views

Letter Combinations of a phone number using a Dictionary

Problem statement (online source) Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone ...
5
votes
2answers
323 views

Selection Sort Algorithm (Node.js)

I wanted to implement a selection sort and wanted to make sure that I'm doing it correctly. I wanted to do it in a way that's efficient and use recursion. Please let me know if I am doing this ...
11
votes
1answer
106 views

Recursive code to generate a parametric sweep of input values

I generate a set of data based off every combination of inputs. The source range that's inputs requires headers to identify the parameters. In its present state I'm not content as it's not as ...
4
votes
2answers
111 views

Build a flat List from a Tree structure in Java

I have a Tree structure represented by a List, which I want to get it flattened. I wrote the function below: ...
5
votes
3answers
2k views

Insertion sort via recursion

I'm learning about algorithms and have been recently experimenting with insertion sort via recursion in Python. My code works fine, but I was wondering if there are any possible improvements that ...
3
votes
1answer
35 views

Batch rename files recursively, and number the directory index

When dealing with iOS app bundle, It is not convenient to get the file by its path, but loading it by its name. Need to batch rename images recursively, with Mac Bash. And to make the name unique, ...
1
vote
0answers
62 views

Recursion to dynamic programming [closed]

I was trying to solve a problem on de-arrangements (Number of partial derangement such that exactly prime number discs are found away from their natural positions? (Any number of non-prime K disks ...
5
votes
0answers
70 views

Mahjong hand completeness checking algorithm

As a summer project, I have been working on a small-scale, console-based version of Mahjong (the Rummy-like hand completion game, and not the solitaire version). While having prior knowledge of the ...
9
votes
1answer
116 views

Tower of Hanoi with helper function

Here is my solution to the Tower of Hanoi problem using Python ...
3
votes
0answers
58 views

Shortest Cell Path In a given grid

I was given this problem in a mock interview. I would appreciate a review of my implementation. Shortest Cell Path In a given grid of 0s and 1s, we have some starting row and column sr, sc and a ...
8
votes
1answer
139 views

AoC Day 24 2017: Electromagnetic Moat

This year's AoC has a puzzle for day 24 that was (supposedly) intended to be solved with recursion. Here is the description: --- Day 24: Electromagnetic Moat --- The CPU itself is a large, ...
2
votes
0answers
34 views

Leetcode Finding Maximum Area of Island Solution

Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are ...
3
votes
1answer
68 views

Adding an item to a hierarchical data structure

What I need to do is create a new object, modify it using a recursive function, then merge it with another object, and do this all in such a way that the values of the new object take precedence over ...
8
votes
4answers
3k views

“What is the maximum that Player 1 can win?”

This is a question that I encountered in one of the competitive coding tests. The question goes as follows: A 2-player game is being played. There is a single pile of stones. Every stone has an ...
3
votes
1answer
350 views

Ternary tree preoder traversal (recursive implementation)

Problem statement: Print out to console the ternary tree preorder traversal. Suppose that the tree can have at most 3 children, left, middle and right. Preorder traversal is to visit the middle ...
4
votes
1answer
97 views

Change tracking POCO entities

Description In a time where thin clients, web browsers, web services and micro services are prevalent, we tend to forget we still have to deal with thick and smart clients. Many applications in B2B ...
6
votes
1answer
2k views

C# recursive backtracker labyrinth generation & game

So for over a weekend I decided to write something rather simple. For maze generation I've followed this algorithm and aside from that I've made a little game, to go from A to B. Everything works fine ...
1
vote
1answer
276 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
0answers
37 views

JS deep merging of objects- removing empty keys

I've written a helper for combining objects (deep merge). It works, and is flexible to add options (like uniq and replace). ...
5
votes
3answers
117 views

javascript property extractor optimisation

This is a very simple nested property extractor. I wonder if it can be optimised further? ...
2
votes
1answer
38 views

Convert Sorted Array to Height Balanced Binary Search Tree

Algorithm This is my code to convert a sorted vector of integers to a balanced binary tree of the same content. The binary tree is made of these nodes: ...
19
votes
3answers
4k views

Loading military units into ships optimally, using backtracking

I solved the following problem using backtracking: We are given a list of military units, their weight and their (numerical) strength. We have a number of ships with a limited carrying capacity....
5
votes
2answers
152 views

Code Challenge: basic arithmic operations using only incrementation operator

Challenge Write a function that takes 2 integer operands left and right (both >= 0) and evaluates one of the following ...
4
votes
1answer
682 views

Parent-child relationship

I am implementing a parent-child relationship, represented by hashes that look like this: ...
3
votes
2answers
59 views

Fixed Content Necklace Generator

I wrote a class that generates combinatorial necklaces with fixed content as per Sawada: A fast algorithm to generate necklaces with fixed content. The class is instantiated with an input list that ...
7
votes
3answers
248 views

Sierpinski turtle triangle

I am working through Programming Fundamentals with Python on Udacity. The assignment was to create a recursive triangle. I am happy with the output, but I am hoping for feedback on the actual ...
7
votes
0answers
137 views

Modeling tabular structure in MSSQL to store data like Excel sheet applying Column inheritance

I've created this database structure to store tabular information (it's a simplification of my real structure but enough to show the point). In it I have Sheets and ...
3
votes
1answer
109 views

Find A Series of Numbers Who when Squared and Summed are equal to a given Square

I am working on a CodeWars titled 'Square into Squares. Protect trees!' it can be found here: https://www.codewars.com/kata/54eb33e5bc1a25440d000891/train/javascript I have a working solution, the ...
3
votes
2answers
216 views

Counting the number of ways to decode a string

I am working on problem where I need to decode a string: A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... ...
4
votes
1answer
47 views

Find the total number of emails in a folder (with all of the subfolders)

The Folder struct definition below is given: type Folder struct { ID int emailCount int childFolderIDs []int } Description of the fields are: ...
2
votes
1answer
48 views

Traversing two trees until a common value has been found

I am traversing two objects that may share a common ancestor. To make the code small enough to show here, I have changed it into two dictionaries with values. As you can see, ...
9
votes
2answers
765 views

Recursive search on Node Tree with Linq and Queue

This code is now maintained on GitHub I've created a Node class which contains two important properties: ...
2
votes
2answers
46 views

Listing ordered components, recursively

I am trying to get ordered components of an interface properly without having a local variable orderedComponents outside, and here is my current code. Reason is because orderedComponents will be in ...
12
votes
4answers
30k views

Get value from dictionary given a list of nested keys

I would like to get a deeply-nested value, for example {"a":{"b":{"c":"myValue"}} by providing the keys to traverse. I tried chaining together .get() but that didn'...
3
votes
1answer
128 views

React.js Nested Nav Bar

This was written for a coding challenge for a company I recently starting working for. I'm looking for any suggestions on how to clean up the code, as well as any issues anyone thinks may occur as it ...
2
votes
1answer
842 views

Insect combinatorics challenge

Below is the problem taken from Berkeley's Cs61A page here Question 9: Insect Combinatorics* Consider an insect in an M by N grid. The insect starts at the bottom left corner, (0, 0), and ...
5
votes
0answers
166 views

Recursive conversion from ExpandoObject to Dictionary<string, object> #2

Because my original question was lacking many details, I have been advised to ask a new question. I will repeat the important parts of the original question and add examples etc to hopefully make it ...
26
votes
4answers
9k views

Repeatedly multiplying digits until a single digit is obtained

I've written code that solves a problem from codewars.com. My code passes all the tests on the codewars site. It's a simple enough problem to solve naïvely, but I would love to improve the structure. ...
4
votes
4answers
713 views

Recursive conversion from ExpandoObject to Dictionary<string, object>

For my blazor library which is a modification of this awesome library I have to convert an ExpandoObject into a ...
1
vote
2answers
83 views

Combined looping and recursion when implementing Quicksort, using proper Clojure style

In "The Joy of Clojure", 2nd edition, code for quicksort is introduced on page 133 that I found hard to digest. I have rewritten it to make it clearer (at least to myself). Here is the original: <...
2
votes
1answer
33 views

Performing an action which may return other actions to be performed, concurrently

I have a program which, at its core, involves repeatedly performing an action on some value, and based on that action also returning a list of new values to perform that action on. The pure version of ...
7
votes
3answers
533 views

Compiling all Exception messages into a string

Below is a recursive function I've written that's responsible for compiling a string consisting of a Exception's message, and also each of that ...
0
votes
1answer
25 views

Uncompressing a list with some run-length-encoded elements

Regarding the following code ...