Skip to content

Instantly share code, notes, and snippets.

@gouravtiwari
Last active December 22, 2015 02:28
Show Gist options
  • Select an option

  • Save gouravtiwari/6403512 to your computer and use it in GitHub Desktop.

Select an option

Save gouravtiwari/6403512 to your computer and use it in GitHub Desktop.
fibered prismatic shapes
# Prism shapes - naive implementation
counter = 1
stars = 5
(1..20).each do |row|
if counter == stars * 2 - 1
counter = 1
else
if counter > stars
puts '*'*(stars * 2 - counter)
elsif counter < stars
puts '*'*counter
else
puts '*'*stars
end
counter += 1
end
end
20.times{puts '*' * fiber.resume}
# Prism shapes - Fiber implementation
class Prismatic
attr_accessor :stars
def initialize(stars)
@stars = stars
end
def fibered
Fiber.new do
counter = 1
loop do
if counter == @stars * 2 - 1
counter = 1
else
if counter > @stars
Fiber.yield(@stars * 2 - counter)
elsif counter < @stars
Fiber.yield(counter)
else
Fiber.yield(@stars)
end
counter +=1
end
end
end
end
def shape(repetition)
fiber = fibered
(repetition * (stars * 2 -1) - (repeatation -1)).times{ puts '*' * fiber.resume}
end
end
Prismatic.new(5).shape(3)
# produces
# *
# **
# ***
# ****
# *****
# ****
# ***
# **
# *
# **
# ***
# ****
# *****
# ****
# ***
# **
# *
# **
# ***
# ****
# *****
# ****
# ***
# **
# *
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment