From: "Jesús Gabriel y Galán" Date: 2007-04-23T22:41:12+09:00 Subject: Re: [QUIZ] Morse Code (#121) On 4/23/07, James Edward Gray II wrote: > >> From: "Jes�s Gabriel y Gal�n" > * This bit of code: > sorted_keys = letters.keys.sort_by {|x| x.length} > min_length = sorted_keys[0].length > max_length = sorted_keys[-1].length > > could be: > > min_length = letters.keys.min {|a,b| a.length <=> b.length } > max_length = letters.keys.max {|a,b| a.length <=> b.length } > > or: > > min_length, max_length = letters.keys.sort_by {|x| x.length}.values_at > (0, -1) > > Just some thoughts. In fact, I have done a modification to my version, to generalize that thing: instead of assuming we have keys from min to max I actually get an array with all the possible key lengths with: key_lengths = letters.keys.map {|x| x.length}.uniq and then modify the min_length.upto(up_to) with key_lengths.each which optimizes the loop for arbitrary key lengths... but haven't been able to test my code yet. > Welcome to Ruby. Thanks, I'm in complete love with the language, and the community is great. Ruby Quiz is a great tool to learn. Jesus.