Merge pull request #6 from Weltenbummler397/exercism-sync/f73acc3564f101aa

[Sync Iteration] rust/high-scores/1
This commit is contained in:
Weltenbummler397 2026-05-04 09:53:49 +02:00 committed by GitHub
commit 687d15216a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 0 deletions

View File

@ -0,0 +1,9 @@
[package]
name = "high_scores"
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,31 @@
#[derive(Debug)]
pub struct HighScores {
scores_vec: Vec<u32>,
}
impl HighScores {
pub fn new(scores: &[u32]) -> Self {
HighScores {
scores_vec: scores.to_vec(),
}
}
pub fn scores(&self) -> &[u32] {
&self.scores_vec
}
pub fn latest(&self) -> Option<u32> {
self.scores_vec.last().copied()
}
pub fn personal_best(&self) -> Option<u32> {
self.scores_vec.iter().max().copied()
}
pub fn personal_top_three(&self) -> Vec<u32> {
let mut sorted_scores = self.scores_vec.to_vec();
sorted_scores.sort();
sorted_scores.reverse();
sorted_scores.iter().take(3).copied().collect()
}
}