I’ve got this message in test 2 and test 3 of bottle song:
It looks like the test runner failed to retrieve the code for this test. Please consider reporting this on the forum so we can try to fix it. Thanks!
And I don’t - how I can to improve my code:
use std::collections::HashMap;
//rust
fn capitalize_first_letter(s: &str) -> String {
let mut chars = s.chars();
match chars.next() {
None => String::new(),
Some(first) => {
let capitalized = first.to_uppercase().collect::<String>();
let rest = chars.collect::<String>();
capitalized + &rest
}
}
}
pub fn recite(start_bottles: u32, take_down: u32) -> String {
let mut map: HashMap<u32, &str> = HashMap::new();
map.insert(0, "no");
map.insert(1 ,"one");
map.insert(2, "two");
map.insert(3 ,"three");
map.insert(4, "four");
map.insert(5 ,"five");
map.insert(6, "six");
map.insert(7 ,"seven");
map.insert(8, "eigth");
map.insert(9 ,"nine");
map.insert(10 ,"ten");
let mut result = String::new();
let end_bottles = start_bottles.saturating_sub(take_down - 1);
for i in (end_bottles..=start_bottles).rev() {
let current_bottles = capitalize_first_letter(map.get(&i).unwrap_or(&"no"));
let next_bottles = map.get(&(i.saturating_sub(1))).unwrap_or(&"no");
result.push_str(&format!(
"{} green bottle{} hanging on the wall,\n",
current_bottles,
if i == 1 { "" } else { "s" }
));
result.push_str(&format!(
"{} green bottle{} hanging on the wall,\n",
current_bottles,
if i == 1 { "" } else { "s" }
));
result.push_str(&format!(
"And if one green bottle should accidentally fall,\n"
));
result.push_str(&format!(
"There'll be {} green bottle{} hanging on the wall.\n\n",
next_bottles,
if i == 2 { "" } else { "s" }
));
}
result.trim().to_string()
}