iteration

Is recursion ever faster than looping?

I know that recursion is sometimes a lot cleaner than looping, and I'm not asking anything about when I should use recursion over iteration, I know there are lots of questions about that already. What I'm asking is, is recursion ever faster than a loop? To me it seems like, you would always be able to refine a loop and get it to perform...

Recursion Vs Loops

I am trying to do work with examples on Trees as given here: http://cslibrary.stanford.edu/110/BinaryTrees.html These examples all solve problems via recursion, I wonder if we can provide a iterative solution for each one of them, meaning, can we always be sure that a problem which can be solved by recursion will also have a iterative so...

How to Iterate in ruby ?

Hi I would like to iterate @some_value outputs the following result {"Meta"=>{"Query"=>"java", "ResultOffset"=>"1", "NumResults"=>"1", "TotalResults"=>"21931"}} i need to retrieve the Value of each individual value for example java 1 1 21931 ...

iterate through fb:random

does anyone know how i would fill data from mysql database in fb:random and iterate through it, to pick a random quote? fb:random $facebook->api_client->fbml_setRefHandle('quotes', '<fb:random> <fb:random-option>Quote 1</fb:random-option> <fb:random-option>Quote 2</fb:random-option> </fb:random>'); mysql data: $rowcount = mysql_resu...

Java - get index of key in HashMap?

In java if I am looping over the keySet() of a HashMap, how do I (inside the loop), get the numerical index of that key? Basically, as I loop through the map, I want to be able to get 0,1,2...I figure this would be cleaner than declaring an int and incrementing with each iteration. Thanks. ...

Is it "iterate through" or "iterate over" something?

Just thought about it. Is there a semantic distinction, or are we free to choose? EDIT I accepted @roygbivs answer, because most of the answers suggested that it is a matter of taste which one to choose, and that it doesn't really change the meaning. In that case, since the through and the over don't carry any information at all (in tha...

Iterate through main function in C?

Here is my main function: int main(int argc, char **argv) { LoadFile(); Node *temp; char *key; switch (GetUserInput()) { case 1: temp = malloc(sizeof(Node)); printf("\nEnter the key of the new node: "); scanf("%s", temp->key); printf("\nEnter the value of the new node: "); scanf("%s", temp->value); AddNode(...

Level-order in Haskell

I have a structure for a tree and I want to print the tree by levels. data Tree a = Nd a [Tree a] deriving Show type Nd = String tree = Nd "a" [Nd "b" [Nd "c" [], Nd "g" [Nd "h" [], Nd "i" [], Nd "j" [], Nd "k" []]], ...

itertools.islice compared to list slice

I've been trying to apply an algorithm to reduce a python list into a smaller one based on a certain criteria. Due to the large volume of the original list, in the order of 100k elements, I tried to itertools for avoiding multiple memory allocations so I came up with this: reducedVec = [ 'F' if sum( 1 for x in islice(vec, i, i+ratio) if...

calculating frequency of a number in an Array

I have an array, scores[5][5] and it is filled with test scores. I need to find the most frequently occurring score and return it. ...

Does the foreach statement iterate in order or it might be random order?

I was wondering if the foreach statement in Perl iterates the items in an array in consistent order? That is, do I get different results if I use foreach multiple times on the same array or list? ...

Periodically iterating over a collection that's constantly changing

I have a collection of objects that's constantly changing, and I want to display some information about the contents every so often (my application is multi-threaded, and differently threads are constantly submitting requests to modify an object in the collection, so it's unpredictable). If I lock the collection, I can iterate over it a...

PHP: Iterate through folders and display HTML contents

Hi, I’m currently trying to develop a method to get a overview of all my different web templates I’ve created and (legally) downloaded over the years. I thought about a displaying them like Wordpress is previewing it’s templates view a small preview windows, displaying the concrete file with styles and everything. How to divide them in...

Using Ruby, what is the most efficient way to check if any key in a hash matches any values within an Array.

I want to compare the keys in a hash of parameters against an array of elements for a match. For example: params = {"key1", "key2", "key3"} params_to_match = ["key2","key3"] I could do this, but I'm sure there is a much more elegant way to acheive the same result params.each_key{|key| if params_to_match.include?(key.to_s)...

Is there a way to increase performance on my simple textfilter?

Hey guys, I'm writing a filter that will pick out items. I have a list of Objects. The objects contain a number, name and some other irrelevant items. At the moment, the list contains 200 items. When typing in a textbox, i'm looking if the string matches a part of the number/name of the objects in the list. If so, add them to the listbo...

Does fast enumeration in Objective-C guarantee the order of iteration?

Can I expect it to go from the start of an array to the end in order? Can't find anything in the docs about this. i.e. is for (id val in array) { NSLog(@"%@", val); } always going to print out the same as for (int i = 0; i < [array count]; ++i) { NSLog(@"%@", [array objectAtIndex:i]); } ...

Java: iterating through list of lists?

The question but in C#. So does Java have C#'s command? I need it for Matches-SearchTerm-Files-relationship. foreach(var i in BunchOfItems.SelectMany(k => k.Items)) {} [Why not for-loops?] I have done such structures in nested for loops but they soon become bloated. So I prefer something more succint like the above. public static Sta...

Comparing values longitudinally in R... with a twist

I have the results of a test taken by a number of individuals at as many as four time periods. Here's a sample: dat <- structure(list(Participant_ID = c("A", "A", "A", "A", "B", "B", "B", "B", "C", "C", "C", "C"), phase = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("base", "sixmos", "twelvemos", "eighteen...

C# acting weird when reading in values from a file to an array

This is the structure of my file: 1111111111111111111111111 2222222222222222222222222 3333333333333333333333333 4444444444444444444444444 5555555555555555555555555 6666666666666666666666666 7777777777777777777777777 8888888888888888888888888 9999999999999999999999999 0000000000000000000000000 0000000000000000000000000 000000000000000000...

Iterating over two arrays at a time in Javascript

I want to iterate over two arrays at the same time, as the values for any given index i in array A corresponds to the value in array B. I am currently using this code, and getting 'undefined' when I call alert(queryPredicates[i]) or alert(queryObjects[i]), I know my array is populated as I print out the array prior to calling this, I ha...