[QUIZ] Number Spiral (#109)

M

Matthew Moss

My second attempt/solution... Slightly different in that it does a
counter-clockwise spiral, but basically follows a similar idea as my
previous solution, though I think this looks nicer.

Looking at the website, it seems like Eric I and I came up with
similar solutions, but I'll say he wins, because his solution looks
better, arrived first, and uses rjust. =)
 
A

avy

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
 
K

Krishna Dole

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
 
W

William James

Simon said:
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)

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.
 
S

Simon Kröger

William said:
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
 
S

Simon Kröger

William said:
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.

depends on your line end character(s) :)
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.

Well, consider it cheating, most spirals have a column width of
3 - so you may have to post it twice to have it accepted.

cheers

Simon
 
W

William James

Simon said:
William said:
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.

depends on your line end character(s) :)

Only if you're under windoze and you neglect to remove
the carriage-returns before uploading. Using vile, for example,
I do :set-unix-mode and save before uploading. If you're not
doing this, then you are really hurting your score.

By the way, in the ASCII Art contest I got the size
down to 76. I would pay to see how "flagitious" did it
in 71 bytes!
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.

Well, consider it cheating, most spirals have a column width of
3 - so you may have to post it twice to have it accepted.

The site really should do more thorough testing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top