Created
May 30, 2017 00:10
-
-
Save epequeno/5717e04816b1b34a0fb9d7c88417a084 to your computer and use it in GitHub Desktop.
solution to exercism beer-song problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashMap; | |
fn main() { | |
println!("{}", sing(3, 0)); | |
for i in (0..11).rev() { | |
println!("{}", verse(i)); | |
} | |
} | |
fn sing(start: isize, end: isize) -> String { | |
let mut lines = Vec::new(); | |
for line in (end..start+1).rev() { | |
lines.push(verse(line)); | |
} | |
let res = lines.join("\n"); | |
res | |
} | |
fn pluralize(n: isize) -> HashMap<isize, String> { | |
let mut res = HashMap::new(); | |
if n >= 2 { | |
res.insert(n, String::from("bottles")); | |
} else if n == 1 { | |
res.insert(n, String::from("bottle")); | |
} else { | |
res.insert(n, String::from("no more bottles")); | |
} | |
res | |
} | |
fn verse(num_bottles: isize) -> String { | |
let one_less = num_bottles - 1; | |
let first_part = pluralize(num_bottles); | |
let second_part = pluralize(one_less); | |
if one_less == 0 { | |
format!("{} {} of beer on the wall, {} {} of beer.\nTake it down and pass it around, {} of beer on the wall.", num_bottles, first_part[&num_bottles], num_bottles, first_part[&num_bottles], second_part[&(num_bottles-1)]) | |
} else if num_bottles == 0 { | |
String::from("No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n") | |
} else { | |
format!("{} {} of beer on the wall, {} {} of beer.\nTake one down and pass it around, {} {} of beer on the wall.", num_bottles, first_part[&num_bottles], num_bottles, first_part[&num_bottles], num_bottles-1, second_part[&(num_bottles-1)]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment