Skip to content

Instantly share code, notes, and snippets.

@prihandi
Created March 20, 2017 13:57
Show Gist options
  • Save prihandi/04c31454b31f96c3a739b47bd37bbf40 to your computer and use it in GitHub Desktop.
Save prihandi/04c31454b31f96c3a739b47bd37bbf40 to your computer and use it in GitHub Desktop.
def cetakPola(size=1)
arr = Array.new(size) { Array.new(size) }
chars = (size**2).to_s.size
maks = size-1
x = maks / 2
y = maks / 2
(1..size).each do |ps|
start = (ps-1)**2 + 1
ends = ps**2
mid = start + ((ends - start) / 2)
if ps == 1
arr[x][y] = 1
y += 1
elsif ps.even?
#right side move down
(start..mid).each { |i| arr[x][y] = i; x += 1}
#move cursor
y -= 1
x -= 1
#bottom side move left
(mid+1..ends).each { |i| arr[x][y] = i; y -= 1}
else
#left side move up
(start..mid).each { |i| arr[x][y] = i; x -= 1}
#move cursor
y += 1
x += 1
#upper side move right
(mid+1..ends).each { |i| arr[x][y] = i; y += 1}
end
end
p chars
arr.each do |a|
a = a.map { |i| i.to_s.ljust( chars, ' ')}
print a.join(' '),"\n"
end
end
cetakPola()
cetakPola(1)
cetakPola(2)
cetakPola(3)
cetakPola(4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment