IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New The iterator doesn't change the algorithm (with assumption)
(Assumption: Smalltalk iterators work similar to Java && C++ iterators)

Because you're walking through the array in both cases.

1st example (with iterator)
collection collect
collection ub
collection lb

Create 3 iterators,
C = collect.iterator
U = ub.iterator
L = lb.iterator
\nfor each result X  {\n\n     for (C = collect.begin(), U = ub.begin(), L = lb.begin();\n         C != collect.end();\n         C++, U++, L++)  {\n        if ((X < U.value()) && (X > L.value())  (C.value())++;\n     }\n}\n



Hint, it's even easier in method two, since you only need one iterator in this case.
\nit = array.iterator();\nfor each result X {\n    for (it = array.begin(); it != array.end(); it++) {\n       if ((X < it.value().U) && (X > it.value().L))  (it.value().C)++;\n    }\n}\n

New Smalltalk iterators are different
You pass a block of code to a collection's method, and this block is executed for every array element:

myArray do: [ :element| element modifyAsNeeded]

A variation I used in my cheating implementation was:

randomNumbers groupedBy: [:number | (number / increment) truncated]

This produces a dictionary of sets that has integers as keys, and set of all numbers that produced that integer in the block as vaules.
--

The rich, as usual, are employing the elected.
-- [link|http://unfit2print.blogspot.com/|http://unfit2print.blogspot.com/]
New Okay...so your 'cheat' is the method
Rather than walk through our collection, we apply to each element of the collection at one time.

I still would use my 2nd way, making the collection a set of objects, each object knowing whether or not to increment it's counter when presented with a random number X. (Each object would have an upper/lower bound and test for them).

Add a method to pull out the counter out of each object afterwards and you're done.
New see my answer to Ben below...
--

The rich, as usual, are employing the elected.
-- [link|http://unfit2print.blogspot.com/|http://unfit2print.blogspot.com/]
     Smalltalk question - (Arkadiy) - (19)
         What's the unsolved part? -NT - (tuberculosis) - (1)
             The original problem - (Arkadiy)
         Why not solve it like you would in any language? - (ben_tilly) - (16)
             That is the problem I solved - (Arkadiy) - (15)
                 2 ways - (Simon_Jester) - (11)
                     Re: 2 ways - (Arkadiy) - (10)
                         The iterator doesn't change the algorithm (with assumption) - (Simon_Jester) - (3)
                             Smalltalk iterators are different - (Arkadiy) - (2)
                                 Okay...so your 'cheat' is the method - (Simon_Jester) - (1)
                                     see my answer to Ben below... -NT - (Arkadiy)
                         Some iterators do provide indexes - (tuberculosis) - (5)
                             Yay! - (Arkadiy)
                             Why worry about that factor of 2? - (ben_tilly) - (3)
                                 Habit, I suppose - (tuberculosis) - (2)
                                     It isn't just you though - (ben_tilly) - (1)
                                         I know what you mean - (tuberculosis)
                 Here is my second strategy in Ruby - (ben_tilly) - (2)
                     I realized that introducing a bucket class makes it easy - (Arkadiy) - (1)
                         Elegance is in the eye of the beholder - (ben_tilly)

I see mass suicides and riots in the future as every urkel fashion sense inspired computer nerd throws down their oxy pads and mutters, "enough is enough," then takes to the streets, scsi cards in hand, and it will be dark, dark day indeed my friend.
81 ms