Posts Tagged with "code golf"
Dec 08, 2007
-
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
Feb 07, 2007
-
Here is my submission for the Roman to Deciman challenge. Code size: 121 bytes.
v={'I',1,'V',5,'X',10,'L',50,'C',?d,'D',500,'M',1000}
y=q=0
gets.chop.split('').each{|n|j=v[n]
y+=j-(q<j ?q*2:0)
q=j}
p y
Tagged: rubycode golf
Jan 22, 2007
-
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
Jan 12, 2007
-
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: code golfruby