Created
March 20, 2017 13:57
-
-
Save prihandi/04c31454b31f96c3a739b47bd37bbf40 to your computer and use it in GitHub Desktop.
Revisions
-
prihandi created this gist
Mar 20, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,44 @@ 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)