Skip to content

Instantly share code, notes, and snippets.

@humbroll
Created October 28, 2014 07:51
Show Gist options
  • Save humbroll/ed78f4a56c13fb48d483 to your computer and use it in GitHub Desktop.
Save humbroll/ed78f4a56c13fb48d483 to your computer and use it in GitHub Desktop.
draw empty body diamond
# Requirements
# * 속이 빈 다이아몬드를 그린다.
# * size는 다이아몬드의 높이를 나타낸다.
# * 높이가 짝수이든 홀수이든 잘 그려져야 한다.
def diamond(size = 8)
str = ""
# up side
c = size / 2
c.downto(1) do |i|
str << (" "*(i+(size%2))) + "*"
unless c == i
str << " " * (2*(c -(i+1)) + 1)
str << "*"
end
str << "\n"
end
# down side
c = size - c
1.upto(c) do |i|
str << (" "*i) + "*"
unless c == i
str << " " * (2*(c -(i+1)) + 1)
str << "*"
end
str << "\n"
end
puts str
end
diamond(7)
# *
# * *
# * *
# * *
# * *
# * *
# *
diamond(6)
# *
# * *
# * *
# * *
# * *
# *
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment