From: avy on
Here is my solution:

n = (ARGV[0] || 8).to_i
(0...n).each do |row|
lev = (row-n/2).abs
m = [2*lev+1,n].min
p = (n-m+1)/2
(0...p).each do |col|
s = (n/2-col)*2
s = s*(s-1)-(row-col)
printf "%2d ",s
end
delta = n/2<=>row
s = lev*2
s *= (s-delta)
s += m-1 if delta<0
m.times do
printf "%2d ",s
s += delta
end
(0...n-p-m).each do |col|
s = (lev+col+1)*2
s = s*(s+1)-(p+m+col-row)
printf "%2d ",s
end
puts
end



From: Krishna Dole on
My first submission to Ruby Quiz:

n = ARGV[0].to_i

# pass this method two coordinates relative to the center of the spiral
def spiral(x, y)
max_xy = [x,y].collect{|num| num.abs}.max
offset = (max_xy * 2 - 1)**2 - 1

if -(x) == max_xy and x != y
y + offset + max_xy
elsif y == max_xy
x + offset + (3 * max_xy)
elsif x == max_xy
-y + offset + (5 * max_xy)
elsif -(y) == max_xy
-x + offset + (7 * max_xy)
end
end

for row in 0..(n - 1)
# the ease of writing one-liners in ruby lends itself to abuse...
puts (0..(n - 1)).map{|col| spiral(col - (n / 2), (n / 2) -
row).to_s.rjust(4) }.join
end

From: James Edward Gray II on
On Jan 17, 2007, at 2:31 PM, Krishna Dole wrote:

> My first submission to Ruby Quiz:

Welcome and thanks for sharing.

James Edward Gray II

From: William James on
Simon Kröger wrote:
> Dear Ruby Quiz,
>
> this isn't really a solution to the quiz 109 because it violates
> some (if not all) of the rules. But as James noted there was a
> code golf problem very similar to this quiz and here is my
> solution to that.
> (see http://codegolf.com/oblongular-number-spirals for detailed
> description of the code golf problem)
>
> ----------------------------------------------------------------
> s,f=1,proc{|x,y|y<1?[]:[[*s...s+=x]]+f[y-1,x].reverse.transpose}

This is more obfuscated than

s=1
f=proc{|x,y|y<1?[]:[[*s...s+=x]]+f[y-1,x].reverse.transpose}

and is no shorter.

> puts f[w=gets(' ').to_i,gets.to_i].map{|i|['%3i']*w*' '%i}
> ----------------------------------------------------------------
>
> It draws a number spiral, starting with '1' in the upper left
> corner and the highest number in the middle, it also features
> spirals that are not quadratic.
>
> Yes, you will get some score at the codegolf site if you repost
> this solution there

If the site accepted this, then it wasn't tested thoroughly
enough. '%3i' gives every number-spiral a column-width
of 3; the column-width should equal the width of the
largest number.

From: Simon Kröger on
William James wrote:

> E:\Ruby>ruby -v
> ruby 1.8.2 (2004-12-25) [i386-mswin32]
>
> So it seems that this won't work without modification under 1.8.2.
>
> As for making it shorter,
> change
> puts f[w=gets(' ').to_i,gets.to_i].map{|i|['%3i']*w*' '%i}
> to
> puts f[w=gets.to_i,$_[-3,2].to_i].map{|i|"%3d "*w%i}
>
> An invisible extra space is printed at the end of each line.


It may be invisible but it will void your solution on codegolf.com.

cheers

Simon
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5 6
Prev: Send over raw socket?
Next: Web service client help needed