From: Ryan Leavengood Date: 2005-11-22T02:38:16+09:00 Subject: Re: [QUIZ SOLUTION] Euchre Hands (#55) On 11/21/05, David Balmain wrote: > Got to get back to work. 166 chars; > a=$<.readlines;s='dchs';c='AKQJT9';t=a.shift;u=(t[0]+32).chr;i=s.index(u);v=s[i-2].chr;puts > t,a.sort_by{|k|(k=~/J[#{u+v}]/?0:50)+(s.index(k[1])-i)%4*10+c.index(k[0])} Brilliant. I'm extremely impressed. I learned quite a bit from it too, which is one of the reasons I like code golfing so much. Plus it is fun and rather addictive, as you have learned. For those wondering, here is what I learned from David's changes to my code: 1. The extra newlines don't need to be cleaned out from the input since "puts" seems to ignore them. 2. For compactness it is better to use strings rather than %w{} arrays. 3. Ruby's array and string indexing already takes care of "wrap-around" when indexing since negative indexes work. So given a string s of length 4, s[(i+2)%4] and s[i-2] are equivalent for values of i from 0 to 5. 4. You can get the index of either a string or a fixnum representing a character using String#index. 5. Using % on a negative number yields a positive number, which is cleverly used above in calculating the weight. My math is rusty I guess, or in fact, I'm not sure I ever learned what performing modulus on a negative number is supposed to return. But I Googled it and learned now. But there is a problem in my code (both the above and the longer version) which I just noticed (and that Zed brought up earlier): if a suit is missing in certain cases the suits are not interlaced properly. I'm wondering if I will bother going back to the drawing board or not :) Ryan