Skip to content

Commit

Permalink
https://exercism.org/tracks/rust/exercises/beer-song
Browse files Browse the repository at this point in the history
  • Loading branch information
hyunjun committed Feb 19, 2024
1 parent 426cbdd commit 4cd06ae
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions rust/beer-song/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
edition = "2021"
name = "beer-song"
version = "0.0.0"
22 changes: 22 additions & 0 deletions rust/beer-song/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pub fn verse(n: u32) -> String {
match n {
0 => "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".to_string(),
1 => "1 bottle of beer on the wall, 1 bottle of beer.\nTake it down and pass it around, no more bottles of beer on the wall.\n".to_string(),
_ => format!("{} bottles of beer on the wall, {} bottles of beer.\nTake one down and pass it around, {} bottle{} of beer on the wall.\n", n, n, n - 1, if 1 < n - 1 { "s" } else {""}).to_string(),
}
}

pub fn sing(start: u32, end: u32) -> String {
/*
let mut s = String::from("");
let mut n = start;
while end < n {
s.push_str(&verse(n));
s.push_str("\n");
n -= 1;
}
s.push_str(&verse(n));
s
*/
(end..=start).rev().map(verse).collect::<Vec<_>>().join("\n")
}
31 changes: 31 additions & 0 deletions rust/beer-song/tests/beer-song.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use beer_song as beer;

#[test]
fn verse_0() {
assert_eq!(beer::verse(0), "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");
}

#[test]
fn verse_1() {
assert_eq!(beer::verse(1), "1 bottle of beer on the wall, 1 bottle of beer.\nTake it down and pass it around, no more bottles of beer on the wall.\n");
}

#[test]
fn verse_2() {
assert_eq!(beer::verse(2), "2 bottles of beer on the wall, 2 bottles of beer.\nTake one down and pass it around, 1 bottle of beer on the wall.\n");
}

#[test]
fn verse_8() {
assert_eq!(beer::verse(8), "8 bottles of beer on the wall, 8 bottles of beer.\nTake one down and pass it around, 7 bottles of beer on the wall.\n");
}

#[test]
fn song_8_6() {
assert_eq!(beer::sing(8, 6), "8 bottles of beer on the wall, 8 bottles of beer.\nTake one down and pass it around, 7 bottles of beer on the wall.\n\n7 bottles of beer on the wall, 7 bottles of beer.\nTake one down and pass it around, 6 bottles of beer on the wall.\n\n6 bottles of beer on the wall, 6 bottles of beer.\nTake one down and pass it around, 5 bottles of beer on the wall.\n");
}

#[test]
fn song_3_0() {
assert_eq!(beer::sing(3, 0), "3 bottles of beer on the wall, 3 bottles of beer.\nTake one down and pass it around, 2 bottles of beer on the wall.\n\n2 bottles of beer on the wall, 2 bottles of beer.\nTake one down and pass it around, 1 bottle of beer on the wall.\n\n1 bottle of beer on the wall, 1 bottle of beer.\nTake it down and pass it around, no more bottles of beer on the wall.\n\nNo 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");
}

0 comments on commit 4cd06ae

Please sign in to comment.