Tuesday, July 7, 2009

Assignment for the July 13th class

Hi All,

For next week's class, the assignment is to write a method for the Array class that returns the mode of a given array. The mode in statistics is the most commonly occurring value or values. If there is a tie, then all of the numbers in the tie are considered the mode, so the return value of the method should be an array with 1 or more methods.

To start you off, here is some code.

class Array
   def mode

     ... your code goes here ...

   end
end

[100, 2, -10, 52.7, 2, nil].mode # returns [2]
[17.1, 2, 142.3, -22, nil, 17.1, 2].mode # returns [2, 17.1] or [17.1, 2]

To solve this, you will need to use a hash. Solving it without using a hash will be very difficult.

Good luck!

Please email or post a comment if you have any questions. This is a tough assignment, so don't be discouraged if it seems hard to do!

Glenn

No comments:

Post a Comment