From: John W Higgins Date: 2009-09-02T02:11:36+09:00 Subject: Re: Nice algorithm for 'spreading' indexes across an array? --0016e64714e2f5628d0472873d7b Content-Type: text/plain; charset=ISO-8859-1 Morning Max, On Tue, Sep 1, 2009 at 9:41 AM, Max Williams wrote: > Little ruby algorithm puzzle... > > I have a situation where i have an array of 12 items. If someone > chooses to have n of them (where n can be between 3 and 12) then i want > to always include the first and last, and then 'spread' the others out > as evenly as possible between the rest. > > So, lets say for the sake of argument that the array holds the numbers 1 > to 12. > > >> arr = (1..12).to_a > => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] > > I would get results back like this > > arr.spread(3) > => [1,6,12] (or [1,7,12], either is fine) > > arr.spread(4) > => [1, 5, 9, 12] (or [1,4,8,12] or [1, 5, 8, 12]) > > It feels like there should be a simple solution for this but i can't > think of a nice way. Anyone? > > thanks > max > -- > Posted via http://ww RijndaelManaged > w.ruby-forum.com/ . > > So here is my silly attempt - simple but probably not the best performing answer you will get here. class Array def spread(members) ret = Array.new #We want sections that total 1 less then the number of members we want back - for example if we #want 3 members in the spread we want 2 equal groups. We use .to_f because we care about fractions here. dist = count.to_f/(members-1) #Now we simply run thru the array and take the members at each spread point. (members-1).times{ |slot| ret << at((slot*dist).ceil) } #Add the last member which is always the last member of the array ret << at(-1) end end --0016e64714e2f5628d0472873d7b--