Skip to main content

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
3 votes
1 answer
126 views

Haskell tips/why doesnt this scale linearly?

My friend wrote a program which compares random arrangements of die faces to find the one with the most evenly distributed faces - especially when the faces are not a mere sequence. I translated his ...
Mike Fairhurst's user avatar
2 votes
2 answers
3k views

Print all interleavings of two given strings

Given two strings str1 and str2, write a function that prints all interleavings of the given two strings. You may assume that all characters in both strings are different Example: ...
Fihop's user avatar
  • 1,025
5 votes
4 answers
9k views

Optimizing a thread safe Java NIO / Serialization / FIFO Queue

I've written a thread safe, persistent FIFO for Serializable items. The reason for reinventing the wheel is that we simply can't afford any third party dependencies ...
trialcodr's user avatar
12 votes
2 answers
8k views

Print all permutations with repetition of characters

Given a string of length n, print all permutation of the given string. Repetition of characters is allowed. Print these permutations in lexicographically sorted order Examples: Input: AB ...
Fihop's user avatar
  • 1,025
5 votes
1 answer
3k views

Tail-recursive factorial

Is there anything what could be improved on this code? def factorial(n: Int, offset: Int = 1): Int = { if(n == 0) offset else factorial(n - 1, (offset * n)) } ...
Marek Sebera's user avatar
7 votes
2 answers
3k views

Pascal's triangle for an interview in PHP

I saw that one of the interview questions could be building a pascal triangle. Is there anything wrong with this particular solution I came up with? ...
Eugene's user avatar
  • 171
2 votes
2 answers
2k views

simple stupid F# async telnet client

Did I write this code to correctly be tail call optimized? Am I forcing computations more than I need with !? Did I generally write this asynchronously correctly to ...
Jimmy Hoffa's user avatar
  • 1,545
2 votes
2 answers
176 views

Finding if value exists in any column recursively

Our company uses an old version of JAXB so it does not allow generics. Other than that, I am using recursive calls because Rows can have subrows and I want to find ...
java_mouse's user avatar
3 votes
3 answers
5k views

Recursively find all objects of type System.Web.UI.Pair in an object array

I have an object array that contains various types of objects (primitives, strings, other object arrays) at arbitrary levels of nesting. I need to pull all objects of type ...
Mitch A's user avatar
  • 131
5 votes
1 answer
333 views

Kalaha Solver: finding the marbles

As with my other question this is regarding my Kalaha solver. Currently the way I find out the optimal progression of moves is with a command like this: ...
linduxed's user avatar
2 votes
4 answers
710 views

How can I remove unwanted combinations from my algorithm in C#?

I have written an algorithm in C#. It is recursive, but it is not optimal. Maybe you have suggestions for improvement? (I already posted this question on stackoverflow but the question was closed ...
Dick Bos's user avatar
  • 131
3 votes
1 answer
205 views

Haskell recursions, level zero

As a learning process I created 'reverse' function in a few different ways. Tinkering with 'reverse' made currying, folds and other stuff easier to understand. Almost natural! I know these are ...
CoR's user avatar
  • 333
1 vote
2 answers
19k views

Create a list of all possible combinations of elements into n-groups from a set of size k

I'm trying to write a Java program that, given a particular number of groups and number of total participants, creates a list of all possible ways to fill that number of groups evenly using all the ...
scae's user avatar
  • 111
4 votes
2 answers
4k views

Ruby Fibonacci(n) recursive computation optimized with reflection

The idea is to take the common-known (and awfully bad performing) Fibonacci(n) recursive method: ...
Darme's user avatar
  • 285
6 votes
2 answers
10k views

Reversing a linked list by iteration and recursion

From my original question on Stack Overflow: Is my implementation of reversing a linked list correct? I'm a beginner at C and I'd like to know about style, and correctness of the reverse algorithms. ...
Jared S's user avatar
  • 95
3 votes
2 answers
9k views

HttpClient error handling

I would be happy to hear feedback on my implementation of HttpClient. I am not so strong with error handling so I want to improve. My thought process behind this was that ...
jiduvah's user avatar
  • 433
3 votes
2 answers
148 views

Simple file recurser

I'm using C# for a project and thought I would get a feel for the language by making some simple programs. My current program is a simple file recurser that prints the names of files and directories. ...
user avatar
5 votes
3 answers
129 views

javascript property extractor optimisation

This is a very simple nested property extractor. I wonder if it can be optimised further? ...
ts01's user avatar
  • 228
2 votes
1 answer
3k views

Make a recursive method faster

How would you refactor this method to make it more performant? If you have refactored the code you need to prove the performance gain in numbers to get the solution :) ...
Pascal's user avatar
  • 131
8 votes
3 answers
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 ...
cimere's user avatar
  • 235
7 votes
1 answer
186 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 ...
Niklas Rosencrantz's user avatar
3 votes
3 answers
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 (...
jaresty's user avatar
  • 2,243
4 votes
1 answer
224 views

Searching an object tree structure

I have this recursive function that searches an object tree structure: ...
Matthew Nichols's user avatar
6 votes
2 answers
2k 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 ...
HunterBlack's user avatar
5 votes
3 answers
32k 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 ...
Eric Herlitz's user avatar
4 votes
2 answers
808 views

Recursive hashing function

This method is part of my login system. Could I improve it further? ...
Seralize's user avatar
  • 490
5 votes
2 answers
335 views

Optimize Recursive Fetching of information?

I wrote a small class that demonstrates my problem: ...
Jess's user avatar
  • 319
6 votes
4 answers
19k views

Getting the largest element in an array using recursion

Any suggestions on how to make this code more efficient? ...
NYC Canuck's user avatar
5 votes
1 answer
1k 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? ...
user avatar
21 votes
4 answers
25k 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? ...
Rafael Herscovici's user avatar
4 votes
1 answer
243 views

Simple recursive Summing program

Any tips on how to improve this simple recursion program? ...
mwmnj's user avatar
  • 143
17 votes
4 answers
2k views

Recursive and iterative Euclidean algorithms

Is this code too cryptic? Simple yes or no question. Feedback is optional. ...
Matthew Hoggan's user avatar
2 votes
1 answer
300 views

Recursive directory listing

I'm trying to optimize performance for recursive directory listing with some exceptions: ...
350D's user avatar
  • 21
11 votes
2 answers
40k 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: ...
yankee's user avatar
  • 508
4 votes
1 answer
6k 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? ...
Jakob Bowyer's user avatar
1 vote
1 answer
440 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: ...
jaresty's user avatar
  • 2,243
2 votes
2 answers
10k 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: ...
jaresty's user avatar
  • 2,243
0 votes
1 answer
2k 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)))) ...
jaresty's user avatar
  • 2,243
1 vote
1 answer
986 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 ...
jaresty's user avatar
  • 2,243
5 votes
2 answers
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? ...
Doron Yaacoby's user avatar

1
19 20 21 22
23