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
8
votes
3answers
4k views

Recursive string reverse function

I'm studying C on K&R and now I'm facing a recursion exercise that states: Write a recursive version of the function reverse(s), which reverses the string s ...
7
votes
1answer
175 views

Calculate highscores based on sales data

The background is my question is SO How to create a list of totals for durations? where I found a recursive solution that I want to review. I could not test it for higher levels than 1 but to the best ...
3
votes
3answers
2k views

RPN-Stack-Based Recursive Calculator Needs Tuneup

This function takes an array of strings and numbers and recursively processes it as a kind of "calculating program". The structure is based on Reverse Polish Notation, so the top object in the stack (...
4
votes
1answer
211 views

Searching an object tree structure

I have this recursive function that searches an object tree structure: ...
6
votes
2answers
1k views

Integer to String recursive method

I was experimenting with lists, sets, and finally maps when I spotted a pattern in my code to make it recursive. Now I haven't used recursion much in the past or at work and I was very excited to have ...
5
votes
3answers
26k views

Recursive method turning flat structure to recursive

I often end up working with systems where the data is delivered with some Id property and possibly a parentId prop, but never do ...
4
votes
2answers
704 views

Recursive hashing function

This method is part of my login system. Could I improve it further? ...
5
votes
2answers
327 views

Optimize Recursive Fetching of information?

I wrote a small class that demonstrates my problem: ...
7
votes
4answers
15k views

Getting the largest element in an array using recursion

Any suggestions on how to make this code more efficient? ...
5
votes
1answer
957 views

PHP - Optimizing Recursive Method

Here is a recursive function that I wrote to search a file-system to a child depth defined by $depth. Could it be optimized more? ...
19
votes
4answers
20k views

Any way to make this recursive function better/faster?

Is there anything that can be done differently for this function? Any way to make it faster? ...
4
votes
1answer
233 views

Simple recursive Summing program

Any tips on how to improve this simple recursion program? ...
17
votes
4answers
1k views

Recursive and iterative Euclidean algorithms

Is this code too cryptic? Simple yes or no question. Feedback is optional. ...
2
votes
1answer
286 views

Recursive directory listing

I'm trying to optimize performance for recursive directory listing with some exceptions: ...
9
votes
2answers
25k views

How can I avoid unchecked cast warning in my generic recursive Iterator?

It's somewhat odd that Java's collection framework has no iterator for recursive data structures. Since I needed something like this, I wrote my own. First off, I need recursive elements: ...
4
votes
1answer
5k views

Selection sort using recursion

How does this recursive selection sort look to everyone here? Am I missing anything 'pythonic' about the way I have done it? ...
1
vote
1answer
363 views

Square-tree using maps and recursion

Define a procedure square-tree analogous to the square-list procedure of exercise 2.21. That is, square-list should behave as follows: ...
1
vote
2answers
6k views

Design a procedure to reverse a list

SICP exercise 2.18 asks the following: Exercise 2.18. Define a procedure reverse that takes a list as argument and returns a list of the same elements in reverse order: ...
0
votes
1answer
1k views

Iterative sum using recursion

Given the following recursive definition of sum: (define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b)))) ...
1
vote
1answer
661 views

Both an Iterative and a Recursive f(x)

A function f is defined by the rule that f(n) = n if n < 3 and f(n) = f(n-1) + 2f(n-2) + 3f(n-3) if n>=3. Write a recursive and an iterative process for computing f(n). I wrote the ...
4
votes
2answers
2k views

Is this Recursion + Linq example inefficient?

I have a hierarchy of of groups, and I want to get a collection of all the lowest level groups (or leaves if we'll look at this as a tree). I wrote the following code. Is it inefficient? ...