iteration

Iterating 1 row at a time with massive amounts of links/joins

Ok, basically what is needed is a way to have row numbers while using a lot of joins and having where clauses using these rownumbers. such as something like select ADDRESS.ADDRESS FROM ADDRESS INNER JOIN WORKHISTORY ON WORKHISTORY.ADDRESSRID=ADDRESS.ADDRESSRID INNER JOIN PERSON ON PERSON.PERSONRID=WORKHISTORY.PERSONRID WHERE PERSONRID...

Iterating over nested lists with a Next() function, without a generator.

Whilst I'd love to solve this problem in python, I'm stuck in Delphi for this one. I have nested lists (actually objects with nested lists as properties, but nevermind), and I want to iterate over them in a generator fashion. That is, I want to write a Next function, which gives me the next item from the leaves of the tree described by t...

Can you remove an item from a List<> whilst iterating through it in C#

Can you remove an item from a List<> whilst iterating through it? Will this work, or is there a better way to do it? My code: foreach (var bullet in bullets) { if (bullet.Offscreen()) { bullets.Remove(bullet); } } -edit- Sorry guys, this is for a silverlight game. I didn't realise silverlight was different to th...

Java How to find a value in a linked list iteratively and recursively

Hi I have a method that has a reference to a linked list and a int value. So, this method would count and return how often the value happens in the linked list. So, I decided to make a class, public class ListNode{ public ListNode (int v, ListNode n) {value = v; next = n;) public int value; public ListNode next; } Then, the metho...

Design patterns for converting recursive algorithms to iterative ones

Are there any general heuristics, tips, tricks, or common design paradigms that can be employed to convert a recursive algorithm to an iterative one? I know it can be done, I'm wondering if there are practices worth keeping in mind when doing so. ...

Most efficient way to add new keys or append to old keys in a dictionary during iteration in Python?

Here's a common situation when compiling data in dictionaries from different sources: Say you have a dictionary that stores lists of things, such as things I like: likes = { 'colors': ['blue','red','purple'], 'foods': ['apples', 'oranges'] } and a second dictionary with some related values in it: favorites = { 'colors':...

ocaml recurssion iteration- find last occurence of element in a list

Assuming l is a List and elem is an element, how can I return the last occurence of the element elem in the list l? Also return -1 if the element does not exisit in l. I dont quite understand how to use recursion for iterating through the list... let rec getLastOccurence l elem = ;; ...

What statistics can be maintained for a set of numerical data without iterating?

Update Just for future reference, I'm going to list all of the statistics that I'm aware of that can be maintained in a rolling collection, recalculated as an O(1) operation on every addition/removal (this is really how I should've worded the question from the beginning): Obvious Count Sum Mean Max* Min* Median** Less Obvious Var...

Ocaml - Iterative to Recursion

For an assignment, i have written the following code in recursion. It takes a list of a vector data type, and a vector and calcuates to closeness of the two vectors. This method works fine, but i dont know how to do the recursive version. let romulus_iter (x:vector list ) (vec:vector) = let vector_close_hash = Hashtbl.create 10 in...

Fastest way to iterate array in PHP

I'm studying for the Zend PHP certification. Not sure the answer to this question. Question: What is the best way to iterate and modify every element of an array using PHP 5? a) You cannot modify an array during iteration b) for($i = 0; $i < count($array); $i++) { /* ... */ } c) foreach($array as $key => &$val) { /* ...

C# iteration requesting scenario based example

I am dropping this line after having visited different websites to try understand real time example of using custom enumeration.I got examples.But they lead me to confusion. Example Take 1 class NumberArray { public int[] scores; public NumberArray() { } public NumberArray(int[] scores) { this.score...

UserControl array, each control has a method to set the text of a label there, but getting a NullReferenceException. Help!

So, I create an array: TorrentItem[] torrents = new TorrentItem[10]; The TorrentItem control has a method called SetTorrentName(string name): private void SetTorrentName(string Name) { label1.Text = Name; } I'm using a for loop to populate 10 TorrentItems like so: private TorrentItem[] GetTorrents() { TorrentItem[] torrent...

In a java enhanced for loop, is it safe to assume the expression to be looped over will be evaluated only once?

Hi, In Java, a for-each loop. If I have a method that generates an array, called genArray(). In the following code, will the array each time be re-generated by calling genArray()? Or will Java call once the method and store a copy from the array? for (String s : genArray()) { //... } Thanks ...

Java: Changing the properties of an iterable object while iterating over it

The following code is just to produce an example of the problem: public static void main(String[] args) { Collection<Integer> src = new ArrayList<Integer>(); Collection<Integer> dest = new ArrayList<Integer>(); src.add(2); src.add(7); src.add(3); src.add(2201); src.add(-21); dest.add(10); while (src.size() != 0) { ...

Python Scoping/Static Misunderstanding

I'm really stuck on why the following code block 1 result in output 1 instead of output 2? Code block 1: class FruitContainer: def __init__(self,arr=[]): self.array = arr def addTo(self,something): self.array.append(something) def __str__(self): ret = "[" for item in sel...

c# .net iterating twice a list in a inner and outer loop - local copy or reference?

Hallo everyone, i have a list of nodes ListNode and i want to draw a line between two nodes if there is an edge / link between them. My approach so far is: public void drawGraphInBIM(ref BIM bim) { foreach (Node nodeOuter in ListNode) { foreach (Node nodeInner in ListNode) { if (areNodesLinked(nodeOuter, nodeInn...

How to make a loop in PowerPoint VBA?

As far as I know, the code below gets a shape from the active window, nudges it a bit, copies the slide and pastes it right after the current one, then turns the pasted slide into an active window, and nudges it again: Sub Test() ' Get the active presentation Dim oPresentation As Presentation Set oPresentation = ActivePresentation ...

linux iterate over files in directory

I'm trying to iterate over each file in a directory. Here's my code so far. while read inputline do input="$inputline" echo "you entered $input"; if [ -d "${input}" ] then echo "Good Job, it's a directory!" for d in $input do echo "This is $d in directory." done exit my output is always just one...

Java: JGraphT: Iterate through nodes

I'm trying to iterate through all nodes, so I can print them out for graphviz. What is the best way to do that using the JGraphT library? public static void main(String[] args) { UndirectedGraph<String, DefaultEdge> g = new SimpleWeightedGraph<String, DefaultEdge>(DefaultEdge.class); String odp = "ODP"; String cck = "CCK"; String m...

More efficient method for this calculation?

a = 218500000000 s = 6 f = 2 k = 49 d = k + f + s r = a i = 0 while (r >= d): r = r - d #print ('r = ',r) i = i+1 #print ('i = ',i) print (i) I think it does what I expect it to, but its way too slow to calculate such a large number, I waited 5 mins for i to print (while python used 100% cpu to calculate..), but it didn't. ...