diff --git a/exercises/practice/beer-song/.meta/example.cairo b/exercises/practice/beer-song/.meta/example.cairo index 732b6006..ae925d3a 100644 --- a/exercises/practice/beer-song/.meta/example.cairo +++ b/exercises/practice/beer-song/.meta/example.cairo @@ -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 { @@ -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 { @@ -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)] diff --git a/exercises/practice/beer-song/src/lib.cairo b/exercises/practice/beer-song/src/lib.cairo index ccd41309..b3461d59 100644 --- a/exercises/practice/beer-song/src/lib.cairo +++ b/exercises/practice/beer-song/src/lib.cairo @@ -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 { diff --git a/exercises/practice/beer-song/src/tests.cairo b/exercises/practice/beer-song/src/tests.cairo index 5debf51b..1a345128 100644 --- a/exercises/practice/beer-song/src/tests.cairo +++ b/exercises/practice/beer-song/src/tests.cairo @@ -41,7 +41,7 @@ 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" ); } @@ -49,7 +49,7 @@ fn first_two_verses() { #[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" ); }