[Sync Iteration] rust/kindergarten-garden/2

This commit is contained in:
exercism-solutions-syncer[bot] 2025-12-11 13:32:29 +00:00 committed by GitHub
parent f80fbca12e
commit 2c47703d7e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 47 additions and 0 deletions

View File

@ -0,0 +1,9 @@
[package]
name = "kindergarten_garden"
version = "0.1.0"
edition = "2024"
# Not all libraries from crates.io are available in Exercism's test runner.
# The full list of available libraries is here:
# https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml
[dependencies]

View File

@ -0,0 +1,38 @@
pub fn plants(diagram: &str, student: &str) -> Vec<&'static str> {
let students = [
"Alice", "Bob", "Charlie", "David", "Eve", "Fred", "Ginny", "Harriet", "Ileana", "Joseph",
"Kincaid", "Larry",
];
let sliced_diagram: Vec<char> =diagram.trim().chars().collect();
let student_index = students.iter().position(|&x| x == student);
if student_index.is_none() {
return vec![];
}
let student_index = student_index.unwrap();
let sliced_diagram_len = sliced_diagram.len();
let mut solution: Vec<&'static str> = Vec::new();
for i in 0..2 {
let plant_index = (student_index * 2) + i;
let plant_row_one = match sliced_diagram[plant_index] {
'V' => "violets",
'R' => "radishes",
'C' => "clover",
'G' => "grass",
_ => "",
};
solution.push(plant_row_one);
}
for i in 0..2 {
let plant_index = (student_index * 2) + i+1 + (sliced_diagram_len / 2);
let plant_row_two = match sliced_diagram[plant_index] {
'V' => "violets",
'R' => "radishes",
'C' => "clover",
'G' => "grass",
_ => "",
};
solution.push(plant_row_two);
}
solution
}