Created
March 17, 2014 14:18
-
-
Save theotherzach/9600002 to your computer and use it in GitHub Desktop.
Yeah Zach, we have talked about this one sooooo much in the past, I feel like I know where this might be going....
class Bottles
def sing
verses 99, 0
end
def verses start, finish
result = ""
start.downto finish do |i|
result += "#{verse i}\n"
end
result
end
def verse number
case number
when 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"
when 1
"1 bottle of beer on the wall, 1 bottle of beer.\n" \
"Take it down and pass it around, no more bottles of beer on the wall.\n"
when 2
"2 bottles of beer on the wall, 2 bottles of beer.\n" \
"Take one down and pass it around, 1 bottle of beer on the wall.\n"
when 3..99
"#{number} bottles of beer on the wall, #{number} bottles of beer.\n" \
"Take one down and pass it around, #{number - 1} bottles of beer on the wall.\n"
end
end
end
...and F#...cause...I'm an asshole
type Bottles() =
member x.verse(number) =
match number with
| 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"
| 1 -> "1 bottle of beer on the wall, 1 bottle of beer.\n\
Take it down and pass it around, no more bottles of beer on the wall.\n"
| 2 -> "2 bottles of beer on the wall, 2 bottles of beer.\n\
Take one down and pass it around, 1 bottle of beer on the wall.\n"
| _ -> sprintf "%d bottles of beer on the wall, %d bottles of beer.\n\
Take one down and pass it around, %d bottles of beer on the wall.\n" number number (number - 1)
member x.verses(start, finish) =
if start = finish then sprintf "%s\n" (x.verse finish)
else sprintf "%s\n%s" (x.verse start) (x.verses((start - 1), finish))
member x.sing() =
x.verses(99, 0)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's mine in Groovy. Bryan and I have talked about this before and I like his approach of handling the 3 special cases completely separately to reduce some of the complexity.