Skip to content

Commit

Permalink
refactor beer-song
Browse files Browse the repository at this point in the history
  • Loading branch information
Nenad committed Jun 20, 2024
1 parent da7ea8f commit 329bb68
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
13 changes: 11 additions & 2 deletions exercises/practice/beer-song/.meta/example.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub fn verse(n: u32) -> ByteArray {
assert!(0 <= n && n <= 99, "Verse must be between 0 and 99");
if n == 0 {
return "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 if n == 1 {
Expand All @@ -15,9 +16,16 @@ pub fn verse(n: u32) -> ByteArray {
}
}

pub fn verses(start: u32, end: u32) -> ByteArray {

pub fn verses(start: u32, take_count: u32) -> ByteArray {
assert!(0 <= start && start <= 99, "Start verse must be between 0 and 99");
assert!(0 <= take_count && take_count <= 99, "Count of bottles must be between 0 and 99");
assert!(take_count <= start, "Cannot take down more than {start} bottles");

let mut lyrics: ByteArray = "";

let mut n = start;
let end = start - take_count;
while n >= end {
lyrics.append(@verse(n));
if n == end {
Expand All @@ -26,11 +34,12 @@ pub fn verses(start: u32, end: u32) -> ByteArray {
lyrics.append(@"\n");
n -= 1;
};

lyrics
}

pub fn song() -> ByteArray {
verses(99, 0)
verses(99, 99)
}

#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions exercises/practice/beer-song/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ pub fn verse(n: u32) -> ByteArray {
panic!("emit verse {n}")
}

pub fn verses(start: u32, end: u32) -> ByteArray {
panic!("sing verses {start} to {end}, inclusive")
pub fn verses(start: u32, take_count: u32) -> ByteArray {
panic!("sing {take_count} verses from {start}")
}

pub fn song() -> ByteArray {
Expand Down
4 changes: 2 additions & 2 deletions exercises/practice/beer-song/src/tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ fn last_generic_verse() {
#[test]
fn first_two_verses() {
assert_eq!(
beer_song::verses(99, 98),
beer_song::verses(99, 1),
"99 bottles of beer on the wall, 99 bottles of beer.\nTake one down and pass it around, 98 bottles of beer on the wall.\n\n98 bottles of beer on the wall, 98 bottles of beer.\nTake one down and pass it around, 97 bottles of beer on the wall.\n"
);
}

#[test]
fn last_three_verses() {
assert_eq!(
beer_song::verses(2, 0),
beer_song::verses(2, 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\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"
);
}
Expand Down

0 comments on commit 329bb68

Please sign in to comment.