mirror of
https://github.com/ArthurDanjou/exercism-rust.git
synced 2026-01-14 12:14:35 +01:00
Initial commit
This commit is contained in:
25
high-scores/.exercism/config.json
Normal file
25
high-scores/.exercism/config.json
Normal file
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"blurb": "Manage a player's High Score list",
|
||||
"authors": [
|
||||
"Br1ght0ne"
|
||||
],
|
||||
"contributors": [
|
||||
"coriolinus",
|
||||
"cwhakes",
|
||||
"efx",
|
||||
"ErikSchierboom"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"src/lib.rs",
|
||||
"Cargo.toml"
|
||||
],
|
||||
"test": [
|
||||
"tests/high-scores.rs"
|
||||
],
|
||||
"example": [
|
||||
".meta/example.rs"
|
||||
]
|
||||
},
|
||||
"source": "Tribute to the eighties' arcade game Frogger"
|
||||
}
|
||||
1
high-scores/.exercism/metadata.json
Normal file
1
high-scores/.exercism/metadata.json
Normal file
@@ -0,0 +1 @@
|
||||
{"track":"rust","exercise":"high-scores","id":"024dda37cd244e269df7d69c653b766a","url":"https://exercism.org/tracks/rust/exercises/high-scores","handle":"ArthurDanjou","is_requester":true,"auto_approve":false}
|
||||
8
high-scores/.gitignore
vendored
Normal file
8
high-scores/.gitignore
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
# Generated by exercism rust track exercise tool
|
||||
# will have compiled files and executables
|
||||
/target/
|
||||
**/*.rs.bk
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
|
||||
Cargo.lock
|
||||
6
high-scores/Cargo.toml
Normal file
6
high-scores/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[dependencies]
|
||||
|
||||
[package]
|
||||
edition = "2021"
|
||||
name = "high-scores"
|
||||
version = "4.0.0"
|
||||
85
high-scores/HELP.md
Normal file
85
high-scores/HELP.md
Normal file
@@ -0,0 +1,85 @@
|
||||
# Help
|
||||
|
||||
## Running the tests
|
||||
|
||||
Execute the tests with:
|
||||
|
||||
```bash
|
||||
$ cargo test
|
||||
```
|
||||
|
||||
All but the first test have been ignored. After you get the first test to
|
||||
pass, open the tests source file which is located in the `tests` directory
|
||||
and remove the `#[ignore]` flag from the next test and get the tests to pass
|
||||
again. Each separate test is a function with `#[test]` flag above it.
|
||||
Continue, until you pass every test.
|
||||
|
||||
If you wish to run _only ignored_ tests without editing the tests source file, use:
|
||||
|
||||
```bash
|
||||
$ cargo test -- --ignored
|
||||
```
|
||||
|
||||
If you are using Rust 1.51 or later, you can run _all_ tests with
|
||||
|
||||
```bash
|
||||
$ cargo test -- --include-ignored
|
||||
```
|
||||
|
||||
To run a specific test, for example `some_test`, you can use:
|
||||
|
||||
```bash
|
||||
$ cargo test some_test
|
||||
```
|
||||
|
||||
If the specific test is ignored, use:
|
||||
|
||||
```bash
|
||||
$ cargo test some_test -- --ignored
|
||||
```
|
||||
|
||||
To learn more about Rust tests refer to the online [test documentation][rust-tests].
|
||||
|
||||
[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
|
||||
|
||||
## Submitting your solution
|
||||
|
||||
You can submit your solution using the `exercism submit src/lib.rs Cargo.toml` command.
|
||||
This command will upload your solution to the Exercism website and print the solution page's URL.
|
||||
|
||||
It's possible to submit an incomplete solution which allows you to:
|
||||
|
||||
- See how others have completed the exercise
|
||||
- Request help from a mentor
|
||||
|
||||
## Need to get help?
|
||||
|
||||
If you'd like help solving the exercise, check the following pages:
|
||||
|
||||
- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
|
||||
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
|
||||
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
|
||||
|
||||
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
|
||||
|
||||
## Rust Installation
|
||||
|
||||
Refer to the [exercism help page][help-page] for Rust installation and learning
|
||||
resources.
|
||||
|
||||
## Submitting the solution
|
||||
|
||||
Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer.
|
||||
|
||||
## Feedback, Issues, Pull Requests
|
||||
|
||||
The GitHub [track repository][github] is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!
|
||||
|
||||
If you want to know more about Exercism, take a look at the [contribution guide].
|
||||
|
||||
## Submitting Incomplete Solutions
|
||||
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
||||
|
||||
[help-page]: https://exercism.org/tracks/rust/learning
|
||||
[github]: https://github.com/exercism/rust
|
||||
[contribution guide]: https://exercism.org/docs/community/contributors
|
||||
30
high-scores/README.md
Normal file
30
high-scores/README.md
Normal file
@@ -0,0 +1,30 @@
|
||||
# High Scores
|
||||
|
||||
Welcome to High Scores on Exercism's Rust Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
|
||||
## Instructions
|
||||
|
||||
Manage a game player's High Score list.
|
||||
|
||||
Your task is to build a high-score component of the classic Frogger game, one of the highest selling and addictive games of all time, and a classic of the arcade era. Your task is to write methods that return the highest score from the list, the last added score and the three highest scores.
|
||||
|
||||
Consider retaining a reference to `scores` in the struct - copying is not
|
||||
necessary. You will require some lifetime annotations, though.
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @Br1ght0ne
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @coriolinus
|
||||
- @cwhakes
|
||||
- @efx
|
||||
- @ErikSchierboom
|
||||
|
||||
### Based on
|
||||
|
||||
Tribute to the eighties' arcade game Frogger
|
||||
31
high-scores/src/lib.rs
Normal file
31
high-scores/src/lib.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
#[derive(Debug)]
|
||||
pub struct HighScores<'a> {
|
||||
scores: &'a [u32]
|
||||
}
|
||||
|
||||
impl<'a> HighScores<'a> {
|
||||
pub fn new(scores: &'a [u32]) -> Self {
|
||||
Self {
|
||||
scores
|
||||
}
|
||||
}
|
||||
|
||||
pub fn scores(&self) -> &[u32] {
|
||||
self.scores
|
||||
}
|
||||
|
||||
pub fn latest(&self) -> Option<u32> {
|
||||
self.scores.last().cloned()
|
||||
}
|
||||
|
||||
pub fn personal_best(&self) -> Option<u32> {
|
||||
self.scores.iter().max().cloned()
|
||||
}
|
||||
|
||||
pub fn personal_top_three(&self) -> Vec<u32> {
|
||||
let mut top_vec = self.scores.to_vec();
|
||||
top_vec.sort_unstable_by(|a, b| b.cmp(a));
|
||||
top_vec.truncate(3);
|
||||
top_vec
|
||||
}
|
||||
}
|
||||
78
high-scores/tests/high-scores.rs
Normal file
78
high-scores/tests/high-scores.rs
Normal file
@@ -0,0 +1,78 @@
|
||||
use high_scores::HighScores;
|
||||
|
||||
#[test]
|
||||
fn test_list_of_scores() {
|
||||
let expected = [30, 50, 20, 70];
|
||||
let high_scores = HighScores::new(&expected);
|
||||
assert_eq!(high_scores.scores(), &expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_latest_score() {
|
||||
let high_scores = HighScores::new(&[100, 0, 90, 30]);
|
||||
assert_eq!(high_scores.latest(), Some(30));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_latest_score_empty() {
|
||||
let high_scores = HighScores::new(&[]);
|
||||
assert_eq!(high_scores.latest(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_personal_best() {
|
||||
let high_scores = HighScores::new(&[40, 100, 70]);
|
||||
assert_eq!(high_scores.personal_best(), Some(100));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_personal_best_empty() {
|
||||
let high_scores = HighScores::new(&[]);
|
||||
assert_eq!(high_scores.personal_best(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_personal_top_three() {
|
||||
let high_scores = HighScores::new(&[10, 30, 90, 30, 100, 20, 10, 0, 30, 40, 40, 70, 70]);
|
||||
assert_eq!(high_scores.personal_top_three(), vec![100, 90, 70]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_personal_top_three_highest_to_lowest() {
|
||||
let high_scores = HighScores::new(&[20, 10, 30]);
|
||||
assert_eq!(high_scores.personal_top_three(), vec![30, 20, 10]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_personal_top_three_with_tie() {
|
||||
let high_scores = HighScores::new(&[40, 20, 40, 30]);
|
||||
assert_eq!(high_scores.personal_top_three(), vec![40, 40, 30]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_personal_top_three_with_less_than_three_scores() {
|
||||
let high_scores = HighScores::new(&[30, 70]);
|
||||
assert_eq!(high_scores.personal_top_three(), vec![70, 30]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_personal_top_three_only_one_score() {
|
||||
let high_scores = HighScores::new(&[40]);
|
||||
assert_eq!(high_scores.personal_top_three(), vec![40]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_personal_top_three_empty() {
|
||||
let high_scores = HighScores::new(&[]);
|
||||
assert!(high_scores.personal_top_three().is_empty());
|
||||
}
|
||||
Reference in New Issue
Block a user