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

-1
votes
0answers
34 views

Euclid's Algorithm in Python [closed]

I am learning about primes and modulus arithmetic in math class so I decided to try to implement Euclid's algorithm in Python. I can't figure out why this doesn't work, but the Python interpreter does ...
3
votes
1answer
49 views

java recursive method to print a descending then ascending integer sequence

From a programming assignment: Write a method writeSequence that accepts an integer n as a parameter and prints a symmetric sequence of n numbers with descending integers ending in 1 followed by ...
1
vote
1answer
28 views

Assign value of nested array as array key

I have a multi dimensional array like this: [0] => ['batch_id'] => '1' ['some_stuff'] => 'values' [0] => ['unit_id'] => '12' ['some_stuff'] => 'values' ...
-1
votes
0answers
38 views

What is so specific about reentrancy? [closed]

(the question was closed at Quality Assurance though I do not understand how it may be no related to that) Developers are obsessed with ensuring (or avoiding) the reentrancy when it regards to ...
1
vote
1answer
46 views

Recursive Determinant

The following code I devised to compute a determinant: module MatrixOps where determinant :: (Num a, Fractional a) => [[a]] -> a determinant [[x]] = x determinant mat = sum [s*x*(determinant ...
1
vote
1answer
45 views

Pascal's Triangle in Javascript

Here's the problem statement: Given an integer value, print out Pascal's Triangle to the corresponding depth in row-major format: Input Sample: 6 Output Sample: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 ...
1
vote
1answer
38 views

Improve file-path finding method

I am using a recursive algorithm to find all of the file-paths in a given directory: it returns a dictionary like this: {'Tkinter.py': 'C:\Python27\Lib\lib-tk\Tkinter.py', ...}. I am using this in a ...
2
votes
0answers
36 views

functional javascript - recursively walking a tree to build up a new one; could this be better factored as a reduce?

/** * recursively build a nested Backbone.Model or Backbone.Collection * from a deep JSON structure. * * undefined for regex values */ exports.modelify = function modelify (data) { if ...
1
vote
2answers
42 views

Recursively print in scala

I have just picked up Scala like 2 hours ago, and I am thinking of printing a series like 1 to 10 but in a recursive fashion. I do not understand what is wrong with this: def printSeries(x:Int):Int = ...
2
votes
2answers
101 views

How can I optimize this recursive function?

I have a function that I need to optimize: def search(graph, node, maxdepth = 10, depth = 0): nodes = [] for neighbor in graph.neighbors_iter(node): if ...
-2
votes
1answer
60 views

whats wrong with my recursion? [closed]

I am working on trying to create a tree in a recursive fashion. I have gotten constructive feedback on my previous questions so I try once more. I dont want to use malloc, and please dont post ...
2
votes
1answer
60 views

Iterative Collatz with memoization

I'm trying to write efficient code for calculating the chain-length of each number. For example, 13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1. It took 13 iterations ...
2
votes
2answers
57 views

clojure - finding first match in recursive search

I am searching a recursive solution space and would like to return the first match I find. (I am using this to solve sudoku, but I guess this would apply to any recursively defined problem or ...
1
vote
1answer
45 views

Recursive java.util.Properties references

Background A subclass of java.util.Properties attempts to recursively resolve configured properties: prop_a=Prop A is ${prop_b} prop_b=Prop B is ${prop_c} prop_c=Prop C. After reading the file, ...
1
vote
1answer
68 views

List-flattening function in erlang feels too wordy

I wrote a tail-recursive list-flattening function, but I'm not too happy with it. a) Is tail-recursion necessary here, or will the function be optimized without it? b) It's ugly how many clauses ...
0
votes
1answer
49 views

Creating an Array of Linked Lists from a BST

Question Given a binary search tree, design an algorithm which creates a linked list of all the nodes at each depth (eg, if you have a tree with depth D, you’ll have D linked lists) Here is my ...
4
votes
2answers
63 views

Recursive XML2JSON parser

I made a Python function that takes an XML file as a parameter and returns a JSON. My goal is to convert some XML get from an old API to convert into Restful API. This function works, but it is ...
2
votes
1answer
54 views

Efficiency of Recursive Checkers Legal Move Generator

I'm implementing a checkers engine for a scientific experiment. I found out through profiling that this is one of the functions that takes up a lot of time. I'm not looking for an in-depth analysis, ...
5
votes
1answer
111 views

Is there a better way to walk a directory tree?

I just started to learn Haskell, and i wrote a function that walks the directory tree recursively and pass the content of each directory to a callback function: The content of the directory is a ...
2
votes
2answers
75 views

multi-recursive replacement function

I wrote a function for creating all possible translations of a source string, based on a multiple translation map. It works, but generates a lot of intermediate maps (see line marked with *). Is there ...
1
vote
0answers
68 views

PostgreSQL: Get all recursive dependencies of a single database object

I've written a SELECT statement that creates a List of all objects that depend on a single object, so that if I wanted to DROP that object I could DROP all referenced objects first without using ...
2
votes
1answer
53 views

Avoiding use of an initialized variable that will either be changed or ignored

Follow on from this question This is a follow on question from one I asked a few days ago. I am making (or rather have made) a recursive method that takes a list of ArrayList objects as its ...
3
votes
3answers
206 views

Trying to make recursive code designed to find objects with even references tidier

As the title says, I'm trying to make my code less horrible looking! For a university practical, I've to use recursive methods to return the even references in a collection of Integer objects stored ...
2
votes
1answer
91 views

Javascript “recursion” via setTimeout

I understand that because of Javascript's single-threaded execution, long loops or recursive calls could make the browser unresponsive while they execute. I thought about simulating recursion using ...
1
vote
3answers
70 views

How to improve this functional python fast exponentiation routine?

I have the following python functions for exponentiation by squaring : def rep_square(b,exp): return reduce(lambda sq,i: sq + [sq[-1]*sq[-1]],xrange(len(radix(exp,2))),[b]) def ...
2
votes
1answer
165 views

Recursion and iteration how can i optimize this code

I have written some code which will fetch contents from a resource which is actually stored in a tree format. Since it is in a tree format, there will be a parent-child relation and hence recursion. ...
1
vote
1answer
135 views

Code review for recursion to map phone number to strings

I am trying to solve the following via recursion: In a phone we have each digit mapped to a number. Example: 1 2(ABC) 3(DEF) 4(GHI) 5(JKL) 6(MNO) 7(PRS) 8(TUV) 9(XYZ) * 0 ...
3
votes
2answers
101 views

JQuery typed wrapper in C#; possible recursion

I am writing an object in C# to mimic jQuery's various functions. That is, calling the constructor is akin to calling the $() function of jQuery. For example, var a = new JQuery("div").eq(0); ...
2
votes
1answer
78 views

How to rewrite recursion in a more ruby way

def get_all_friends(uid, client) friends = client.friendships.friends(uid: uid) total = friends.total_number get_friends(total, 0, uid, client) end def get_friends(total, cursor, uid, client) ...
2
votes
1answer
76 views

Which of these two paren-matching functions is better?

I am currently trying to learn Haskell (after taking a Scala course on Coursera); but while I can write functions that do what I want, I worry that I am not learning to write idomatic/clean/performant ...
4
votes
4answers
247 views

What do you think of my Recursive FizzBuzz?

I wanted to see if I fully understood recursion so I attempted the FizzBuzz challenge and applied recursion to it. Did I do it correctly? Is this good code? Is there a more efficient way of doing ...
3
votes
1answer
92 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 ...
-1
votes
1answer
74 views

recursive function [closed]

I want to generate a random number then to check if it is in the database. If not then redo it. My pseudo code: PIN = GenerateRandomNumber().ToString(); ...
1
vote
0answers
135 views

Print all interleavings of given two 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: Input: ...
4
votes
0answers
504 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 ...
2
votes
1answer
153 views

Scala: 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)) } The idea is to have tail-recursive ...
5
votes
2answers
660 views

pascal triangle in php (anything wrong with this solution?)

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? function pascal_r($r){ $local = array(); ...
0
votes
1answer
119 views

Codechef: Byte Landian Gold Coin - Java [closed]

This is the link to the question. http://www.codechef.com/problems/COINS Not sure what is wrong with this code. These are the input/output that I get 12 --> 13 13 --> 16 When I submit it to ...
1
vote
2answers
234 views

Confusing program for reversing link List using recursion? [closed]

I was trying to reverse the link list using recursion and somehow I did it? But one think is bothering me how the head in the last line finally points to the element 4 (i.e. the first element after ...
2
votes
2answers
210 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 allow sends and receives to occur ...
1
vote
1answer
352 views

shortest path recursive algorithm [closed]

This is the first time I come to this site, even though I tried to post in Stack Overflow but was told that this would be a better place to ask. So I hope this is the right place for my question. ...
2
votes
2answers
106 views

Finding if value exists in any column recursively

Could you please review if I've done this correctly? Our company uses 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 ...
1
vote
2answers
204 views

Need a method that recursively finds 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 System.Web.UI.Pair from ...
0
votes
1answer
218 views

recursion using pipes

So hi, my goal with my code is to have a program similar to pipes in unix like $printenv | sort | less using recursion. I'm pretty new to pipes and file descriptor manipulation so I don't know ...
0
votes
0answers
197 views

Code Improvement on generating Tree Structure

I'm using actionscript/flex 4.5 I have a Class (DEF) with three main properties COD_CLASSE is the ID of the object COD_CLASSE_PADRE is the ID of the parent of the object children is an ...
3
votes
0answers
191 views

Building an ASP.NET Menu Recursively - Needs Refactoring and Readability Improving

The following code builds an ASP.NET menu recursively from data in a database: Imports Microsoft.VisualBasic Public Class MenuBarHelper Public Shared Function GetMainMenu() As Menu Dim ...
2
votes
4answers
381 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 ...
0
votes
0answers
119 views

Ruby Trie Iterative to_a

I've been benchmarking recursive and iterative ruby trie functions. Unsurprisingly the iterative functions seem perform better then my recursive implementations. The only exception to this is my to_a. ...
3
votes
2answers
151 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 ...
0
votes
2answers
5k 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 ...

1 2