s

Posts Tagged with "code golf"

Code Golf: Numeric Diamonds

Dec 08, 2007  -  Comments

Here is my submission for the Numeric Diamonds challenge. Code size: 219 bytes.

[9,36,100].each{|n|s=Math.sqrt(n).to_i
w=n.to_s.size.to_i
a=[]
j=s+s-1
j.times{a<<[" "*w]*j}
x=0
y=s-1
f=x
g=y
1.upto(n){|t|a[x][y]=t.to_s.rjust w
t%s<1 ?(x=f+1;f=x;y=g-1;g=y):(x+=1;y+=1)}
a.each{|r|puts r.join.rstrip}}

Tagged: rubycode golf

Fore!

Jan 22, 2007  -  Comments

Shawn recently turned me onto a really cool site: Code Golf. Every week (or so) they post a coding challenge. The challenges themselves aren't overly difficult (well, most aren't). The real challenge is to complete the challenge with as little code as possible. . . literally. Your code submissions are first evaluated to determine if they work like they should, then they count how many bytes your program takes up. The shortest program for each accepted language (Perl, PHP, Python, and Ruby) gets 10,000 points, with each longer program getting points based on their size relative to the winning submission.

I've only been able to complete two challenges so far. Both have fallen around the middle of the group with regards to size. I'm not sure I'll ever know the shortcuts to get my 228 byte program down to 102 bytes! It is a great excuse for exercising your brain, though.

UPDATE: I've started posting my submissions to the challenges that I've been able to complete. Just look for posts tagged with code golf.

Tagged: rubycode golf

Code Golf: 99 Bottles of Beer

Jan 12, 2007  -  Comments

Here is my submission for the 99 Bottles of Beer challenge. Code size: 242 bytes.

a=' bottles of beer'
b=' on the wall'
d=a.tr 's',''
99.downto(1){|x|e,f=x.to_s,x<2?d:a
puts e+f+b+', '+e+f+'.'
puts 'Take one down and pass it around, '+(x-1).to_s+(x<3?d:f)+b+".\n\n"if x>1}
puts "Go to the store and buy some more, 99#{a+b}."

Tagged: rubycode golf