Modify list and dictionary during iteration, why does it fail on dict?

I think the reason is simple. lists are ordered, dicts (prior to Python 3.6/3.7) and sets are not. So modifying a lists as you iterate may be not advised as best practise, but it leads to consistent, reproducible, and guaranteed behaviour. You could use this, for example let’s say you wanted to split a list … Read more

Nifty way to iterate over parallel arrays in Java using foreach

I would use a Map myself. But taking you at your word that a pair of arrays makes sense in your case, how about a utility method that takes your two arrays and returns an Iterable wrapper? Conceptually: for (Pair<K,V> p : wrap(list1, list2)) { doStuff(p.getKey()); doStuff(p.getValue()); } The Iterable<Pair<K,V>> wrapper would hide the bounds … Read more

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

From Apples’ Objective-C documentation on fast enumeration: For collections or enumerators that have a well-defined order—such as NSArray or NSEnumerator instance derived from an array—the enumeration proceeds in that order, so simply counting iterations will give you the proper index into the collection if you need it.

Iterators in C++ (stl) vs Java, is there a conceptual difference?

Perhaps a bit more theoretical. Mathematically, collections in C++ can be described as a half-open interval of iterators, namely one iterator pointing to the start of the collection and one iterator pointing just behind the last element. This convention opens up a host of possibilities. The way algorithms work in C++, they can all be … Read more

Algorithm for iterating over an outward spiral on a discrete 2D grid from the origin

There’s nothing wrong with direct, “ad-hoc” solution. It can be clean enough too. Just notice that spiral is built from segments. And you can get next segment from current one rotating it by 90 degrees. And each two rotations, length of segment grows by 1. edit Illustration, those segments numbered … 11 10 7 7 … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)