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 Here is my second strategy in Ruby
\n#! /usr/bin/ruby\nclass RangeBucket\n  \n  def initialize (min, max)\n    @min = min;\n    @max = max;\n    @count = 0;\n  end\n\n  def add_if_in (element)\n    if @min < element && element < @max\n      @count += 1;\n    end\n  end\n\n  def to_s ()\n    sprintf("%1.5f - %1.5f: %d", @min, @max, @count);\n  end\n  \nend\n\nranges = [\n  RangeBucket.new(0, 0.1),\n  RangeBucket.new(0.1, 0.4),\n  RangeBucket.new(0.4, 0.8),\n  RangeBucket.new(0.8, 1),\n];\n\n1000.times{\n  value = rand();\n  ranges.each {|range| range.add_if_in(value)};\n}\n\nputs ranges;\n

That should be translatable into any language that you want, including Smalltalk. (Ruby chosen because I know it and its object model is similar to Smalltalk.)
"good ideas and bad code build communities, the other three combinations do not"
- [link|http://archives.real-time.com/pipermail/cocoon-devel/2000-October/003023.html|Stefano Mazzocchi]
New I realized that introducing a bucket class makes it easy
Does it constitute an elegant Smalltalk solution? I had high hopes for iterators, but I guess in OO language, the first answer to anything is to create a class :)
--

The rich, as usual, are employing the elected.
-- [link|http://unfit2print.blogspot.com/|http://unfit2print.blogspot.com/]
New Elegance is in the eye of the beholder
That solution looks pretty clean and natural. Perhaps (now that I am awake) it would be better to have the description and test of when you are in the bucket factored out of the class and into the call. That puts the bucketing behaviour under the caller's control, allowing you to generalize the concept. (For instance one bucket might be even integers, and the next odd ones.)

If you have a lot of buckets and a lot of numbers, though, it won't scale very well. It is O(n*m). In that case you probably want to go to an array implementation and use a binary search. Which again can be hidden in a class if you wish...

Cheers,
Ben
"good ideas and bad code build communities, the other three combinations do not"
- [link|http://archives.real-time.com/pipermail/cocoon-devel/2000-October/003023.html|Stefano Mazzocchi]
     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)

Mmmmm. Bacteria injections.
69 ms