Skip to content

Instantly share code, notes, and snippets.

@topher6345
Last active November 6, 2019 05:47
Show Gist options
  • Save topher6345/b7705497649c77d07d25dae0f79dd25a to your computer and use it in GitHub Desktop.
Save topher6345/b7705497649c77d07d25dae0f79dd25a to your computer and use it in GitHub Desktop.
# 99 Bottles - Pre Thoughts
# Here is my initial thoughts on the problem in the book
class Song
MAYBE_S = -> x { x != 1 ? 's ' : ' '} #check this
def initialize(number_of_bottles: 99, take: 101)
@bottle_sequence = number_of_bottles.downto(0).cycle.take(take).freeze
end
def call
@song ||= @bottle_sequence.inject('', &method(:apply_stanza))
end
private def apply_stanza(mutable_reference, next_line)
mutable_reference += stanza(next_line)
end
def stanza(n)
if n > 1
"#{n} bottles of beer on the wall, "\
"#{n} bottles of beer.\n"\
'Take one down, pass it around, '\
"#{n - 1} bottle#{MAYBE_S[n - 1]}of beer on the wall...\n"
elsif n.eql?(1)
"#{n} bottle of beer on the wall, "\
"#{n} bottle of beer.\n"\
"If that one bottle should happen to fall, what a waste of alcohol!\n"
elsif n.eql?(0)
"No more bottles of beer on the wall, no more bottles of beer.\n"\
"Go to the store and buy some more, 99 bottles of beer on the wall...\n"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment