From: Ken Bloom Date: 2007-02-12T04:10:11+09:00 Subject: Re: One-Liners (#113) On Sun, 11 Feb 2007 09:55:59 -0800, Chris Shea wrote: > I wasn't able to get down to less than 80 chars on two of the items. > For commaizing numbers and getting a random line from a file (assuming > loading the whole file into memory is against the rules) I think I was > just barking up the wrong path. I like my nested hash solution (which > I wrote in both golf and non-golf modes. > > # Commaize (works for both floats and integers) > a=quiz.to_s.split('.');a[0].reverse.gsub(/ > (\d{3})/,'\1,').chomp(',').reverse+"#{'.'+a[1] if a[1]}" Too long: 96 characters (but I couldn't get it under 80 either) > # Flatten once > a=[];quiz.each{|i| if i.is_a? Array;i.each{|j| a< > # Randomize array (the obvious way) > quiz.sort_by {rand} > > # Class from String (fails for some) > begin eval "#{quiz}.allocate.class" rescue nil end > > # Wrap lines (no newline at the end!) > a='';b=quiz;until b.size<=40;a< 40))<<"\n";end;a< > # Find anagrams > quiz.find_all {|x| x.split('').sort == quiz[0].split('').sort} > > # Binarize > a=''; quiz.each_byte {|b| a << (b == 32 ? "\n" : "%b" % b)}; a > > # Random line (kludge, reads the whole file twice) > f=quiz;c=0;f.each{c+=1};r=rand(c)+1;f.pos=0;c=0;a='';f.each{|line|c > +=1;a=line if c==r};a 89 characters > # Wondrous sequence > b=quiz;a=[b];while b>1;if b%2==1;b=3*b+1;else;b=b/2;end;a< > # Nested hash (golf and non-golf, and my best answer (I think)) > a=quiz.pop;quiz.reverse_each{|i|a={i=>a}};a > hash = quiz.pop; quiz.reverse_each { |item| hash = { item => hash } }; > hash > > Thanks for the good time. -- Ken Bloom. PhD candidate. Linguistic Cognition Laboratory. Department of Computer Science. Illinois Institute of Technology. http://www.iit.edu/~kbloom1/