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
3answers
125 views
3
votes
3answers
198 views
Recursive linear search - branch style and layout
Is my layout of the if/else statement reasonable?
It feels clumsy to me to spread the termination condition over the first three lines of the function. Can I ...
2
votes
1answer
28 views
Time complexity of 0/1 Knapsack challenge
The code gives correct output. How do I calculate time complexity for code like this?
The program finds all the combinations of items and sees if the combination gives the max profit. If the last ...
1
vote
1answer
29 views
Decoding an element list
I have a function which I am calling an infinite number of times (or until a condition is met). The problem with this recursive function is that on a higher level, it is called by a worker thread ...
4
votes
1answer
122 views
Implement a string in reverse using pointers and recursion
I'm trying to reverse a string using pointers and recursion, and I send it a char array. Is this the best way, or is there a better one?
...
4
votes
3answers
109 views
Merge Sort Implementation in Java (that seems slow…)
I decided to write my own implementation of merge sort. Here is the code:
...
2
votes
1answer
63 views
Cutting stock recursion
This problem is identical to the one here:
Suppose that you have been assigned the job of buying the plumbing
pipes for a construction project. Your foreman gives you a list of the
varying ...
2
votes
1answer
55 views
1
vote
2answers
78 views
Using brute force to find an energy optimal path for a given amount of time
The code below implements a brute force method to find an energy optimal path for a given amount of time t_aim using recursion (the recursion function is ...
2
votes
1answer
50 views
Process a line in chunks via Regular Expressions with Ruby
I'm writing a Ruby script to reverse engineering message log files. They come from an external system with the following characteristics:
Each line of the log file has at least one message.
Each ...
2
votes
1answer
32 views
Efficiently creating a menu consisting of multi-level structure
I was asked to modify existing code to allow multi-level page structure. Each page can have one parent, and one page can have multiple children.
I will present following code. My question is is there ...
3
votes
1answer
83 views
Print all possible combinations of size r, from an array of size n
This is my working solution for the following problem: given an array of integers of size n, print all possible combinations of size r.
Before I proceed to the solution, I have the following ...
11
votes
3answers
116 views
Recursive, nested list traversal
Given a nested list of strings (which may contain other nested lists), print the contents of the list and the corresponding depth.
Here is my solution:
...
4
votes
1answer
119 views
Recursive function that generates the permutations of a string
I am looking for a review of my recursive function that generates the permutations of a string. Are there better ways to do this?
...
4
votes
1answer
54 views
2
votes
3answers
83 views
4
votes
1answer
54 views
Comparison of Fibonacci (Multinacci) Functions in Python3
After coming up with a formula on this Stack Overflow question, defeating recursion limits and finding a few new ways of calculating these numbers (as per this Stack Overflow question), I decided that ...
4
votes
0answers
56 views
Refactoring while-do array comparison function into tail recursion with F#
As part 2 of my other question concerning this long running project I've been inquiring about on CR, I reimplemented my generic function for determining if an array of arrays is a sub-array or ...
2
votes
1answer
56 views
Refactoring while-do into tail recursion with F#
I have a while-do loop nested inside a while-do loop in my program, due to it needing to iterate in two dimensions through an array of arrays, and originally my loop looked like this:
...
4
votes
3answers
106 views
3
votes
2answers
88 views
Alternate method of iterating than os.walk
Recently, I became tired of how slowly os.walk seems to run (really, it should be called os.crawl), and made something recursive ...
8
votes
3answers
389 views
Stock Pipe Cutting using Recursion
This problem is from the Stanford Open Course CS106B Programming Abstractions Assignment #3
Problem number 6 here.
"You are charged with buying the plumbing pipes for a construction project. Your ...
4
votes
1answer
32 views
Recursive Django filter to output a list from user hierarchy
I need to create an HTML unordered list (to display with jstree) to display the hierarchy of users, along with a list underneath the user's "subusers" of their comments. Each user could have many ...
2
votes
1answer
33 views
Slow calculation of trigonometric sine function
Background
Creating a client-side pie chart in XSLT 1.0, without extensions or external dependencies (i.e., cannot use FXSLT, EXSLT, JavaScript, etc.).
Problem
The code is slow; at least fifteen ...
3
votes
1answer
34 views
Erlang pattern matching excersise
I recently started my first project with Erlang. I've been skimming documentation and various books to get the information I need, and it's going well, but now that I have something that works I want ...
11
votes
3answers
160 views
Algorithm to calculate semi perfect integers, lack of efficiency
I'm working on an algorithm to calculate weird numbers, and to do so there are several properties that needs to be calculated, one of them being, if it is NOT a semi-perfect/pseudoperfect number.
My ...
3
votes
2answers
67 views
More efficient conversion of tab-formatted nested data structure to json
I have data that looks like the "Input" below and need to convert it into JSON. My solution works by parsing the text to find a level for each data point. Then I use a recursive structure to build a ...
2
votes
0answers
38 views
5
votes
1answer
81 views
Algorithm to Iterate All Possible Strings in Clojure
I'm fairly new to Clojure and I struggled a bit coming up with a nice Clojure-y solution for implementing an algorithm for one of my applications. The real issue I'm trying to solve would require a ...
2
votes
1answer
50 views
Optimization of recursive jQuery images swap function
There's this website I'm working on that needs to swap images every few seconds, like a slideshow or image fade swap in a way, except that you need to target 5 IMG tag elements. I was able to make a ...
2
votes
2answers
46 views
Replacing an F# loop over a mutable variable by an immutable approach
Consider:
let mutable m' = m
while m' % i = 0L do
m' <- m' / i
I've refactored it into:
...
11
votes
1answer
148 views
Trampoline Interceptor
Aim
In C#, the following method will cause a StackOverFlowException if called with parameter 0:
...
6
votes
2answers
198 views
Infix to postfix conversion
I have written a C++ program to convert an infix expression to postfix expression using recursion. I would like to know if it can be improved if possible. Can we improve it by not using a stack? I am ...
4
votes
1answer
107 views
Seeking improved Objective-C permutation algorithm
This algorithm seeks to find all of the possible permutations of a word:
...
3
votes
1answer
55 views
Generating all keypad possibilities
I ran into this question on Stack Overflow. It looked like a really cool thing to try myself, being that I haven't done any recursion for ages (read at least 2 years).
...
1
vote
0answers
29 views
Optimizing Recursive Quadtree
I have a written a quadtree program in Python 2.7 to cross-correlate large catalogs with each other i.e. find the common objects in the catalogs based on their position. The problem is that it's still ...
3
votes
1answer
100 views
Finding if there is a subset that sums to a target value from an array of integers
I came across this problem.
Problem Statement:
Given an array of ints, is it possible to choose a group of some of
the ...
1
vote
1answer
70 views
improve performance of recursive function in c# method
I'm trying to write a function which will cache objects in memory.
The function below works, but performance is unbearable. What would you recommend to improve performance?
...
-2
votes
1answer
235 views
hailstone sequence using recursion in python
Douglas Hofstadter's Pulitzer-prize-winning book, Gödel, Escher, Bach, poses the following mathematical puzzle:
Pick a positive integer n as the start.
If n is even, divide it by 2.
If n is ...
10
votes
3answers
531 views
List all possible numbers from a given array that sums up to a given number
I have to solve the following problem: Given an array of integers and given an integer value, list all possible numbers frm the array that sums up to the given value.
Example:
Input: array = {1, 2, ...
4
votes
0answers
67 views
Fibonacci number function: convert to tail-recursion?
I've been implementing a function for calculating the nth Fibonacci number in F#. So far the best implementation I could come up with is this:
...
2
votes
1answer
79 views
Solving the coin change algorithm using 10 coins
I'm solving the coin change problem having target of n and upper bound as n.
Also the maximum number of coins is 10.
For example. if the target is 11. then the possible outcomes are -
...
2
votes
1answer
98 views
Modifying nested dictionaries in Python
My task was to take a configuration specified as a dictionary and run some function with it (the actual function is not relevant here). After implementing that, I was asked to allow some "sweeping" of ...
5
votes
3answers
364 views
Making this Tic Tac Toe implementation more recursive
Here is a Tic Tac Toe game in C++ that I have been working on. My questions are as follows:
I tested it for bugs, but if you find any, please post the solution!
I want to make the code more ...
10
votes
4answers
657 views
Recursive implementation of palindrome string checker
I've tried to refactor my program to single return statement at the end of the program, however, it ruins the end statements of the recursion because I want to return from function in specific line ...
1
vote
1answer
48 views
8
votes
3answers
192 views
Travelling Salesman in Qt
I am writing a recursive function based on the Travelling Salesman Problem.
Is this a correct way of doing things, or am I creating memory leaks?
Prerequisites are:
a two dimensional matrix ...
2
votes
1answer
40 views
Optimizing string replacement program that uses recursion
I have the following string replacement program for a string of size 256:
I am replacing the string
"\'"
with the string
'
Please suggest any modifications if needed or any ...
5
votes
6answers
864 views
Get rid of recursion in fruit picker
I have this simple code and it is working fine it is self explanatory: the user will choose a random number 10 times and based on the random number the array will be filled by fruits.
...
0
votes
0answers
13 views
performance in javascript recursion [duplicate]
I have this simple code and it is working fine It is self explanatory:
the user will choose a random number 10 times and based on therandom number the array will be filled by fruits.
...