recursion

rate my (C++) code: a recursive strstr sans any standard library string functions :)

So, the idea was a write a recursive function that compares two strings to see if string 'prefix' is contained in string 'other', without using any standard string functions, and using pointer arithmetic. below is what i came up with. i think it works, but was curious - how elegant is this, scale 1-10, any obvious funky moves you would h...

returning and using multiple tree structures in a recursive function

I have a function get_trees() that operates on a complex tree structure T and returns two component tree structures A and B. The only way I have been able tot get this to work is to create a new structure C with pointers to A and B, which is then passed as a parameter to the function and is also a return value: typedef struct Composite ...

Remove all nested blocks, whilst leaving non-nested blocks alone via python

Source: [This] is some text with [some [blocks that are nested [in a [variety] of ways]]] Resultant text: [This] is some text with I don't think you can do a regex for this, from looking at the threads at stack overflow. Is there a simple way to to do this -> or must one reach for pyparsing (or other parsing library)? ...

Simple PHP Recursion Test Failing

Hello, I am attempting a basic recursion to create multi-dimensional arrays based on the values of an inputed array. The recursion works by checking for a value we shall call it "recursion" to start the loop and looks for another value we'll call it "stop_recursion" to end. Basically taking this array array('One', 'Two', 'recursion',...

access php array children through parameters?

I have a unique case where I have an array like so: $a = array('a' => array('b' => array('c' => 'woohoo!'))); I want to access values of the array in a manner like this: some_function($a, array('a')) which would return the array for position a some_function($a, array('a', 'b', 'c')) which would return the word 'woohoo' So basicall...

cramming for the exams - what am i missing (C++ string manipulation)

cramming for a c++ exam, on string manip with some example questions (to which i dont have solutions) - below is today's handiwork. although it works fine - would be awesome if any obvious lapses / better way of doing things occur to you, just drop me a quick note, i need to learn me some C++ fast :) thanks! #include <iostream> #includ...

dynamic array key additions (sucky title, i know)

precode: $keys = array('a', 'b', 'c', 'd'); $number = 10; code: eval('$array[\''.implode('\'][\'',$keys).'\'] = $number;'); result: Array ( [a] => Array ( [b] => Array ( [c] => Array ( [d] => 10 ) ) ) ) problem...

how would you rewrite this recursive function to remove the recursion?

I got that little function(I changed the name of variables) Private Function everythingLinked(ByRef myClass As cls, ByVal found As Boolean) As Boolean If Not found AndAlso myClass.checked = False Then myClass.checked = True For i = 0 To myClass.numLink If Not found Then found = everything...

Convert function from recursion to iteration

Hello, I have this function that I wrote that is abysmally slow since php does not handle recursion well. I am trying to convert it to a while loop, but am having trouble wrapping my head around how to do it. Can anyone give me some tips? public function findRoute($curLoc, $distanceSoFar, $expectedValue) { $this->locationsVis...

Finding the largest positive int in an array by recursion

I decided to implement a very simple program recursively, to see how well Java handles recursion*, and came up a bit short. This is what I ended up writing: public class largestInIntArray { public static void main(String[] args) { // These three lines just set up an array of ints: int[] ints = new int[100]; java.util.Ran...

Path stack in a Java recursive search

Hi! I wrote a simple Depth-First search algorithm, which works, but is failing to build the patch right. Im having a tough time trying to understand, why - so, basically, need your help, guys :) Here is the code: public void Search(String from, String to) { String depart = from; String destin = to; if ( (NoChildren(depart) == fa...

Improve this recursive function for hash traversal in Ruby

I've written a method to turn a hash (nested if necessary) of values into a chain that can be used with eval to dynamically return values from an object. E.g. passed a hash like { :user => { :club => :title }}, it will return "user.club.title", which I can then eval. (The point of this is to write a method for views that will allow me ...

PHP's ability to handle recursion

I've seen in a few places lately people saying that PHP has a poor capacity for recursion. Recently I wrote a recursive php function for graph traversal and found it to be very slow compared to java. I don't know whether this is because of php's capacity for recursion or because php is slower than java in general. Some googling reveal...

itoa recursively

Ok, well i have been trying to write a recursive version of itoa, this is what i came up with. void itoa(int n, char s[]) { static int i = 0; if(n / 10 != 0) itoa(n/10, s); else if(n < 0) i = 1; /* s[0] is allready taken by - sign */ else i = 0; /* reset i to 0 */ if(n < 0) { ...

F# - Record-type recursive member functions and the "rec" keyword.

I've always believed that in F# we needed to use the rec keyword for every recursive function, for example: let rec factorial = function | 0 -> 1 | k when k > 0 -> k * (factorial (k - 1)) | failwith "oops!" Today I was playing around with F# and I came up with a code similar to the following: let MyRecordType = { Something :...

Recursion in Linked List

I want to print the reverse of a linked list. I am doing it by recursion. But while calling read(temp) in function read, it gives a BUS ERROR. Any reasons why is this happening ?? #include <iostream> using namespace std; struct node { int info; node *next; }; void read(node *start) { node *temp = start->next; if(sta...

Cocoa iPhone Recursive Array

Hey there, I'm using core-data and I have an entity that has an attribute called SID and another called ParentSID. I'm trying to create a method where I can pass a fetched object from the set and it will return the lineage of that object by checking the ParentSID of each ancestor. If the ParentSID is > 0 it should loop recursively u...

Problem using a UDF as computed col: Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 32).

Hello. My question is a little subjective and might be a little unclear since I don't know where in the UDF crashes, or whether the error is caused in the function itself or from other resources using a column in this table. Let me just tell you the story. I had a function (that used as a computed-column for QuoteItemsGroupFeature.Sal...

C#: How to "unroll" a "recursive" structure

Not sure how to call it, but say you have a class that looks like this: class Person { public string Name; public IEnumerable<Person> Friends; } You then have a person and you want to "unroll" this structure recursively so you end up with a single list of all people without duplicates. How would you do this? I have already ma...

Python: recursively create dictionary from paths

I have several hundred thousand endpoint URLs that I want to generate stats for. For example I have: /a/b/c /a/b/d /a/c/d /b/c/d /b/d/e /a/b/c /b/c/d I want to create a dictionary that looks like this { {'a': {'b': {'c': 2 }, {'d': 1 } }, {'c': {'d': 1 } } ...