Tagged Questions

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.

learn more… | top users | synonyms

4
votes
0answers
54 views

Brainfuck interpreter in Haskell

Okay, so I just started learning Haskell around a week ago and this is my first real program that I worked on all of yesterday with a lot of help from IRC. I know that using indicies and arrays is not ...
6
votes
2answers
150 views

Merge sort using sentinels

This is my implementation of recursive merge sort, using sentinels: ...
7
votes
3answers
528 views

Simplifying this factorial function

def factorial(t,n): if n == 1 : return t else: x = (t * (n-1)) n = n-1 return factorial(x,n) print factorial(6,6) I can't seem to figure out ...
10
votes
2answers
155 views

Validating a Binary Search Tree

This is an implementation of a function to check the property of a Binary Search Tree, that is: the key in any node is larger than the keys in all nodes in that node's left subtree and smaller ...
2
votes
0answers
64 views

GOF Composite Design Pattern Implementation Using Modern C++

After reading about the composite pattern from the GOF design pattern book, I thought to re-implement the code mentioned in the motivation section using the modern C++ concept/idioms. Below is the ...
4
votes
4answers
253 views

Get the environment variable's value

I've written a function which can get the environment variable's value. The input parameter would one of the following: ${machine}/bin ...
4
votes
2answers
47 views

Lopsided Trees and Recursion

The original problem is: Define the height of a binary tree to be the number of nodes in the longest path from the root to a leaf. The empty tree is considered to have height 0. A node is ...
6
votes
3answers
746 views

Reversing an array recursively

Implementation: ...
3
votes
3answers
134 views

Miller-Rabin Recursive Primality Test

I'm working on a primality test and have written a recursive function that returns the value of the function \$b^{q-1} \bmod q\$ where \$3<= q <= 32000\$ Is there any way to speed up ...
2
votes
1answer
146 views

Nsum problem: find all n elements in an array whose sum equals a given value

I am implementing the algorithm for the following problem with Python. I am pretty new to Python, so I want to listen to any advice that improve my coding style in a "pythonic" way, even about naming ...
4
votes
2answers
288 views

Simple factorial program using recursion

Two concepts I realized I needed to understand and use more are recursion and Exceptions. Thus, I combined both in the following program. Although it began with a focus on using recursion it became ...
4
votes
2answers
472 views

Building a simple tree structure

I'm trying to build this simple tree: ...
3
votes
3answers
202 views

Merge Sort - Efficiency

I've written this Java code to implement Merge Sort. ...
6
votes
3answers
250 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
43 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
36 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
616 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
217 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
75 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
77 views

Tail recursive sort

I've heard of this implementation of quick sort (in pseudocode): ...
1
vote
2answers
114 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
56 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
42 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 ...
5
votes
1answer
878 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
195 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: ...
8
votes
1answer
86 views

Queens placement

The code below is intended to be a learning material for high school intermediate programming class, as an introduction to recursion and backtracking. I was thinking of rook placement and found it too ...
4
votes
1answer
736 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
56 views
2
votes
3answers
100 views
4
votes
1answer
64 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
63 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
72 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
123 views
3
votes
2answers
219 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
430 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
45 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
40 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
44 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
191 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
86 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 ...
4
votes
0answers
56 views

Factorial using trampolines

I've translated this Java code into Delphi code: ...
5
votes
1answer
92 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
97 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
62 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
177 views

Trampoline Interceptor

Aim In C#, the following method will cause a StackOverFlowException if called with parameter 0: ...
6
votes
2answers
1k 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
179 views

Seeking improved Objective-C permutation algorithm

This algorithm seeks to find all of the possible permutations of a word: ...
3
votes
1answer
65 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
39 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 ...
5
votes
1answer
150 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 ...