mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-05-15 17:58:44 +00:00
[Sync Iteration] rust/kindergarten-garden/2
This commit is contained in:
parent
f80fbca12e
commit
2c47703d7e
9
solutions/rust/kindergarten-garden/2/Cargo.toml
Normal file
9
solutions/rust/kindergarten-garden/2/Cargo.toml
Normal 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]
|
||||
38
solutions/rust/kindergarten-garden/2/src/lib.rs
Normal file
38
solutions/rust/kindergarten-garden/2/src/lib.rs
Normal 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
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user