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.
3
votes
1answer
50 views
Filter Duplicate Elements in Haskell
I'm working on HackerRank to try to improve my Haskell skills along side with reading Haskell Programming from first principles. I wrote a program that works, but it seems to time out on large input ...
-4
votes
0answers
35 views
Solving a linear peg solitaire game
I'm trying to code a solution for Linear Peg Solitaire Game.
By linear, I mean that the board is a single dimensional array with 1 hole in it. And pieces only either move for left to right or right ...
1
vote
1answer
37 views
Add 1 to a number represented as an array of digits, using recursion
Given a non-negative number represented as an array of digits, plus one to the number.
The digits are stored such that the most significant digit is at the head of the list.
Explanation for using ...
1
vote
1answer
56 views
Cons List in Python
It make sense to not allow links to external code hosting, so I restated my question. This is classical cons list in python using Abstract Base Class interface. Constructor is in fact out of the Cons ...
3
votes
2answers
228 views
Java exhaustive solution
This is a take on Pebble Solitaire. The user inputs n number of games and after that inputs a strings of length 23 that contains a combinations of only 'o' (pebble) and '-' (empty space). If there are ...
2
votes
1answer
60 views
Groups/colors/removes cells
Based on the value of what is in column B (column A is hidden) it groups/colors/removes the corresponding node(s). "R - " values are nodes that need to be grouped. The rows are 6 cells apart before a ...
3
votes
1answer
71 views
Return path to a value in a nested object
Description:
Its not a programming challenge but I though to write a small utility which will return the path to a primitive value in a possibly nested object. The idea is not original.
Code:
...
3
votes
2answers
56 views
Get subsets of a set given as a list
This code is meant to create a list of subsets of a given set which is represented as a list.
I'm doing this to improve my style and to improve my knowledge of fundamental algorithms/data structures ...
7
votes
2answers
92 views
Recursive higher-order function
I have implemented a through function for myself in a project.
Since I am still quite new in python and want to learn it correctly, I would be happy about any ...
1
vote
2answers
40 views
Pascal's triangle in mit-scheme
As suggested by the name of source file (ex1.12.scm), I just started learning mit-scheme by reading SICP. In Exercise 1.12, I'm asked to "compute elements of Pascal'...
5
votes
1answer
60 views
Interleaving of two strings
I have written this recursive code to find all interleaves of two strings, where the order of strings are preserved. For example:
s1="abc" s2="1"
a1bc
ab1c
abc1
...
1
vote
0answers
68 views
Optimizing Python Code in terms of Time - Recursive Function [closed]
I wrote the following code but it takes a lot of time, about 20 seconds. The code has recursion and the recursion depth doesn't exceed 100 in any case. When it is run a sufficient amount of times ...
4
votes
0answers
127 views
Assembling and traversing a tree, given a list of items and parent pointers
I've written a function which takes input such as this:
...
2
votes
2answers
52 views
Binary Tree Inversion
I solved this problem.
Invert a binary tree.
4
/ \
2 7
/ \ / \
1 3 6 9
To this
...
4
votes
2answers
162 views
Migrating data using entity framework, linq and recursion
I have a piece of code to migrate data from an old app to a new app. The data represents the user's websites.
In the old app the user only had one section for websites, on which you could add any ...
1
vote
0answers
14 views
Greedy recursive search [closed]
So I am trying to implement a groupSize method and a helper functions.
Here is one idea: set up a local Set of Spots to hold spots you’ve found so far in the cluster. From the current spot-position, ...
4
votes
0answers
70 views
Split string to space-separated words based on dictionary
This program is based on as old retired Google interview question where you're supposed to create a program that will split a string into space-separated words, if the string is composed of valid ...
3
votes
3answers
140 views
Prime factoring function using recursion
As a programming exercise, I decided to make a prime factoring function using recursion:
...
4
votes
2answers
148 views
Square root function
As a programming exercise I made a square root function in C# using recursion, here is the function:
...
4
votes
1answer
47 views
Recursively generating flat XML elements from hierarchical data structure
My class has a method that does some stuff, and then wants to recursively operate on some data. Specifically, I'm generating an XML report whose output includes a flat list of elements derived from a ...
0
votes
2answers
457 views
An alternative to loop constructs in Java [closed]
Java allows methods to make recursive calls. This can be dangerous because an infinite recursive call will fill up the stack. It can be useful, however, for calculations that need iteration when the ...
3
votes
1answer
90 views
AngularJS recursive function call with $timeout
I have created a recursive function call in AngularJS and just wonder if is there a better way to solve this problem? The code works perfectly.
My code is the following:
...
2
votes
2answers
60 views
Project Euler #15 in Python
I've wrote the code for Project Euler #15 and it runs amazingly fast. I know I can write it using binomials but I wanted to calculate by brute force as if I do not know Binomials. But I think it must ...
0
votes
1answer
62 views
All permutations of integer vector
I have spent a decent amount of (waaay too much) time trying to write the below function and get my head around recursion. The aim was to return all permutations of an int vector (assuming no ...
6
votes
2answers
110 views
Console colorizer
To be able to apply various colors to the console I created a ConosoleColorizer. It's really simple. It just takes an XML and renders it to the console with the ...
6
votes
0answers
97 views
PLZ HELP MEH IMPROVA MEH LOLCAT CODEZ, WHUCH CALCULUTS FAWCTORIALS
SUNCE MEH BEEN WRUTEN CODEZ IN MUNY DIFFRNT PROGRAMMING LANGUAGES, MEH THOUGHTZ THAT ME TRY TO WRITE CODEZ IN AN ESOTERIC LANGUAGE. MEH CODEZ CALCULUTS A FAWCTORIAL OF UH USR INPUTTED NUMBAH IN ...
1
vote
0answers
62 views
JSON schema visualizer
I have the following backbone function to read a JSON schema and draw the tree structure (as in the screenshot below). I want to make sure if checking against the type as object/array is alright or if ...
3
votes
1answer
82 views
Code for dividing gifts equally
I recently came across this problem:
It is Lavanya's birthday and several families have been invited for
the birthday party. As is customary, all of them have brought gifts
for Lavanya as well ...
3
votes
2answers
89 views
Count numbers whose digits are only increasing or decreasing
The goal is described here: basically, it is to compute the number of numbers below \$10^n\$ such that its digits are either increasing or decreasing (like 112 or 211, but not like 121).
The original ...
3
votes
2answers
100 views
Longest repeating and non-overlapping substring
Given a string find the length of longest repeating non-overlapping substring in it. In other words, find 2 identical substrings of maximum length which do not overlap.
Examples:
Input : str = "...
3
votes
0answers
33 views
Recursively list files in provided directory in the style of `npm ls`
I've written a small CLI node program to list the contents of a directory with an output similar to that provided by npm ls but listing both files and directories, ...
1
vote
2answers
59 views
A double search function that looks in a list for one value and then the other alternatively
I'm writing in Python (3.4) a double search function that looks in a list for one value and then the other alternatively. That is, start searching the second value once you find first the first value (...
2
votes
1answer
115 views
Walk recursively through dictionaries and return result without using global variable
I need to walk through several nested dictionaries in a recursive way and return a value of the type property.
How my code could be improved? Especially, how to do ...
4
votes
3answers
70 views
Printing a table
I have this recursive function that I made which prints a table, but I really think my current solution is really ugly. Does anyone have an idea as to how I can improve my solution?
I'm trying to ...
2
votes
1answer
50 views
Building and querying an immutable phone book
I wanted to get your opinion on this immutable implementation of a Phone Book using recursion to process a stream. I am especially interested in ways to speed up the code execution.
Here is an ...
4
votes
1answer
126 views
String permutation iterative improvements?
I have written some code (below) which iterates over each character of a string and adds that character to every index of previous permutations.
For example, in case of abc: b is added to every index ...
4
votes
4answers
386 views
Recurrence, recursive function
Type a number \$n\$. Then calculate \$f(n)\$, where
$$ f(n) =
\begin{cases}
n-10 & \text{if}~ n > 100\\[1.5ex]
f(f(n + 11)) & \text{otherwise}
\end{cases}
$$
The output must show all the ...
2
votes
2answers
64 views
JavaScript function for to deep copy objects
I've made this function for to get a deep copy (recursive copy) of objects.
=> Sub-objects aren't references to the sub-objects of the original object. Instead they are objects on their own.
Here's ...
3
votes
1answer
53 views
Printing the hierarchical representation, and finding all ascendants for a node, in a self-referential table in Python with recursion
I have a self referential database table:
CREATE TABLE category (
id INT PRIMARY KEY,
name VARCHAR,
parent_id INT REFERENCES category (id)
);
And ...
5
votes
1answer
99 views
Recursion in Chess AI
I am currently in the process of completely rewriting an older Chess project and have written the main recurring function in the AI:
...
-2
votes
2answers
119 views
A JavaScript isEven() function, defined recursively
Can someone please take a look at these recursive functions, offer some advice on the code, or just critique my attempt into thinking recursively?
Problem:
Define a recursive function isEven ...
5
votes
1answer
113 views
Replacing characters in a Java string using iteration and recursion
I am new to the whole Data Structures thing and I want to get better at writing efficient code. So, I have been practicing some problem sets. The questions is - Replace characters in a string using ...
3
votes
2answers
347 views
Empty block in recursion base case
I wrote a recursive function to uncheck all menu nodes. It works well, but I need to know if it is right to leave empty block inside ...
6
votes
2answers
235 views
ASCII Paint Bucket
In MS Paint, if we choose paint bucket and fill click on a certain spot, it gets filled with a new chosen color along with its neighboring pixels that are the same color until it reaches certain ...
5
votes
2answers
121 views
Creating a minimax tree recursively
I am currently working on creating a tree (this is my learning process of creating a minimax tree and am starting towards minimax tree from here) with multiple nodes recursively.
Can anyone please ...
3
votes
3answers
209 views
(Re)Creating a tree from a flat collection (or “unflatten” a tree)
I need to create a tree from a flat collection. The order of elements is defined in a specification so it's rather impossible that it will ever change... but I want the whole algorithm to be ...
2
votes
1answer
56 views
Hand crafted longest common sub sequence
I know that the below solution is not the best one and I am in the way to learn that stuff till then I have applied the brute force to see how far I can go with my intuition.
...
1
vote
1answer
42 views
Simple Factor Class
I have a class which lazily factors numbers. It provides the user with these methods:
getFactors returns a ...
2
votes
1answer
60 views
Recursive Implementation of Merge Sort C++
I am currently reading an algorithms book and I read on the insertion and merge sorts. I implemented the merge sort in C++, The first function MERGE is responsible for the merging of the two subarrays ...
3
votes
2answers
82 views
Evaluating a completely parenthesized arithmetic expression
I tried to solve the following problem in a programming challenge, but the verdict was time limit exceeded.
Completely parenthesized expression
Write a program that reads a completely ...