Questions tagged [recursion]

Recursion is a kind of function call in which a function calls itself. Such functions are also called recursive functions. Structural recursion is a method of problem solving where the solution to a problem depends on solutions to smaller instances of the same problem.

687
votes
20answers
418k views

Access / process (nested) objects, arrays or JSON

I have a nested data structure containing objects and arrays. How can I extract the information, i.e. access a specific or multiple values (or keys)?For example:var data={code: 42,...
479
votes
14answers
99k views

What is the most efficient/elegant way to parse a flat table into a tree?

Assume you have a flat table that stores an ordered tree hierarchy:Id Name ParentId Order1 'Node 1' 0 102 'Node 1.1' 1 103 'Node 2' 0 ...
1395
votes
23answers
353k views

What is tail recursion?

Whilst starting to learn lisp, I've come across the term tail-recursive. What does it mean exactly?
256
votes
13answers
284k views

What is the maximum recursion depth in Python, and how to increase it?

I have this tail recursive function here:def fib(n, sum):if n < 1:return sumelse:return fib(n-1, sum+n)c=998print(fib(c, 0))It works up to n=997, then it just ...
33
votes
4answers
60k views

Why does my function return None?

This may be an easy question to answer, but I can't get this simple program to work and it's driving me crazy. I have this piece of code:def Dat_Function():my_var=raw_input("Type \"a\" or \"...
210
votes
20answers
72k views

Understanding recursion [closed]

I'm having major trouble understanding recursion at school. Whenever the professor is talking about it, I seem to get it but as soon as I try it on my own it completely blows my brains. I was trying ...
289
votes
19answers
102k views

Way to go from recursion to iteration

I've used recursion quite a lot on my many years of programming to solve simple problems, but I'm fully aware that sometimes you need iteration due to memory/speed problems.So, sometime in the very ...
627
votes
8answers
120k views

What Is Tail Call Optimization?

Very simply, what is tail-call optimization? More specifically, Can anyone show some small code snippets where it could be applied, and where not, with an explanation of why?
137
votes
14answers
269k views

How to search by key=>value in a multidimensional array in PHP

Is there any fast way to get all subarrays where a key value pair was found in a multidimensional array? I can't say how deep the array will be.Simple example array:$arr=array(0=> array(id=&...
204
votes
20answers
241k views

Recursively list files in Java

How do I recursively list all files under a directory in Java? Does the framework provide any utility? I saw a lot of hacky implementations. But none from the framework or nio
164
votes
18answers
60k views

Can every recursion be converted into iteration?

A reddit thread brought up an apparently interesting question:Tail recursive functions can trivially be converted into iterative functions. Other ones, can be transformed by using an explicit ...
172
votes
9answers
62k views

Recursion in Angular directives

There are a couple of popular recursive angular directive Q&A's out there, which all come down to one of the following solutions:manually incrementally 'compile' HTML based on runtime scope ...
1
vote
2answers
5k views

Recursion, parsing xml file with attributes into treeview c#

I'm currently working on a project where an application will take on XML files and display it into a treeview in C#. I'm using Visual Studio 10 to write this code. I cannot limit number of times the ...
70
votes
16answers
108k views

What in layman's terms is a Recursive Function using PHP

Can anyone please explain a recursive function to me in PHP (without using Fibonacci) in layman language and using examples? i was looking at an example but the Fibonacci totally lost me!Thank you ...
91
votes
8answers
51k views

Convert a series of parent-child relationships into a hierarchical tree?

I have a bunch of name-parentname pairs, that I'd like to turn into as few heirarchical tree structures as possible. So for example, these could be the pairings:Child : ParentH : GF : G...
121
votes
40answers
161k views

What is recursion and when should I use it?

One of the topics that seems to come up regularly on mailing lists and online discussions is the merits (or lack thereof) of doing a Computer Science Degree. An argument that seems to come up time and ...
151
votes
6answers
53k views

Does Python optimize tail recursion?

I have the following piece of code which fails with the following error:RuntimeError: maximum recursion depth exceededI attempted to rewrite this to allow for tail recursion optimization (TCO). ...
-1
votes
2answers
286 views

Recursive function does not return specified value

I am trying to debug a recursive function used to validate user input and return a value when the input is OK. The function looks like this:double load_price(){double price;Goods * ...
70
votes
3answers
56k views

Recursive function to generate multidimensional array from database result

I'm looking to write a function that takes an array of pages/categories (from a flat database result) and generates an array of nested page/category items based on the parent ids. I would like to do ...
110
votes
18answers
29k views

Understanding how recursive functions work

As the title explains I have a very fundamental programming question which I have just not been able to grok yet. Filtering out all of the (extremely clever) "In order to understand recursion, you ...
113
votes
11answers
93k views

How to [recursively] Zip a directory in PHP?

Directory is something like:home/file1.htmlfile2.htmlAnother_Dir/file8.htmlSub_Dir/file19.htmlI am using the same PHP Zip class used in PHPMyAdmin http://trac....
109
votes
9answers
89k views

self referential struct definition?

I haven't been writing C for very long, and so I'm not sure about how I should go about doing these sorts of recursive things... I would like each cell to contain another cell, but I get an error ...
197
votes
28answers
133k views

Recursion or Iteration?

Is there a performance hit if we use loop instead of recursion or vice versa in algorithms where both can serve the same purpose? Eg : Check if given string is palindrome.I have seen many programmers ...
12
votes
1answer
3k views

Python recursion with list returns None [duplicate]

def foo(a):a.append(1)if len(a) > 10:print areturn aelse:foo(a)Why this recursive function returns None (see transcript below)? I can't quite understand ...
3
votes
1answer
437 views

Recursive code returns None [duplicate]

I really do not understand, why the codedef isIn(char, aStr): ms=len(aStr)/2if aStr[ms]==char:print 'i am here now'return Trueelif char>aStr[ms] and not ms==...
48
votes
5answers
12k views

Building a promise chain recursively in javascript - memory considerations

In this answer, a promise chain is built recursively.Simplified slightly, we have :function foo() {function doo() {// always return a promiseif (/* more to do */) {...
80
votes
4answers
24k views

defaultdict of defaultdict, nested

Is there a way to make a defaultdict also be the default for the defaultdict?IOW, if I do:x=defaultdict(...stuff...)x[0][1][0]{}That's what I want. I'll probably just end up using the bunch ...
117
votes
21answers
233k views

Solution for “Fatal error: Maximum function nesting level of '100' reached, aborting!” in PHP

I have made a function that finds all the URLs within an html file and repeats the same process for each html content linked to the discovered URLs. The function is recursive and can go on endlessly. ...
16
votes
4answers
34k views

Get list of sub-directories in VBA

I want to get a list of all sub-directories within a directory. If that works I want to expand it to a recursive function.However my initial approach to get the subdirs fails. It simply shows ...
107
votes
19answers
92k views

How do I recursively delete a directory and its entire contents (files + sub dirs) in PHP?

How do I delete a directory and its entire contents (files and subdirectories) in PHP?

153050per page
angop.ao, elkhabar.com, noa.al, afghanpaper.com, bbc.com, time.com, cdc.gov, nih.gov, xnxx.com, github.com,