Skip to content

Commit f8edaab

Browse files
committed
exercism-70 Beer Song
1 parent daa6e90 commit f8edaab

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ solution of many challenges of [Leetcode](https://leetcode.com/), [Exercism](htt
287287
67. [Allergies](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/allergies.rb)
288288
68. [Run Length Encoding](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/run_length_encoding.rb)
289289
69. [Robot Simulator](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/robot_simulator.rb)
290+
70. [Beer Song](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/beer_song.rb)
290291

291292
<a name="leetcode"/>
292293

exercism/beer_song.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Problem: https://exercism.org/tracks/ruby/exercises/beer-song
2+
3+
# Solution
4+
module BeerSong
5+
def self.recite(bottles, verses)
6+
output = []
7+
verses.times do
8+
output << verse(bottles)
9+
bottles -= 1
10+
end
11+
output.join("\n")
12+
end
13+
14+
def self.verse(bottles)
15+
standard_verse = <<~TEXT
16+
#{bottles} bottles of beer on the wall, #{bottles} bottles of beer.
17+
Take one down and pass it around, #{bottles - 1} bottle#{'s' if bottles > 2 } of beer on the wall.
18+
TEXT
19+
single_bottle_verse = <<~TEXT
20+
1 bottle of beer on the wall, 1 bottle of beer.
21+
Take it down and pass it around, no more bottles of beer on the wall.
22+
TEXT
23+
no_bottles_verse = <<~TEXT
24+
No more bottles of beer on the wall, no more bottles of beer.
25+
Go to the store and buy some more, 99 bottles of beer on the wall.
26+
TEXT
27+
return no_bottles_verse if bottles.zero?
28+
bottles > 1 ? standard_verse : single_bottle_verse
29+
end
30+
end

0 commit comments

Comments
 (0)