From: Holger Mack Date: 2008-09-21T06:29:03+09:00 Subject: Re: One-Liners Mashup (#177 again) ------=_Part_301_6614572.1221946620631 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Shorter carpet solution, still not very nice: def carpet(n) n==0?"#\n":carpet(n-1).map{|l| ['\0\0\0','\0 \0','\0\0\0'].map{|c| l.gsub(/#| /,c)}}.join end puts carpet(2) next quiz: Given a text from STDIN and a regexp, print each line which (in part) matches regexp with two preceding and two successional lines. Do not output a line more than once. Regards Holger 2008/9/20 Matthew Moss > > Challenge: > > Print out a Serpinski carpet. > > Here ya go. Non-negative parameter to the function is recursion depth, > so should be 0 for the "null 1x1 carpet", 1 for the 3x3, 2 for the > 9x9, etc. This is golfed, and probably a bit more than 80 chars > (gonna wrap in email, I bet). > > def carpet(n) > def k(s,x,y) (s<=3||k(s/3,x/3,y/3))&&!(x%3==1&&y%3==1) > end;s=3**n;s.times{|y|s.times{|x| print k(s,x,y)?"#":" "};puts} > end > > > Followup: Make my solution shorter. (There's got to be something nicer > than using `times` twice and `print` once. > > ------=_Part_301_6614572.1221946620631--