aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--high-scores/.exercism/metadata.json1
-rw-r--r--high-scores/.gitignore8
-rw-r--r--high-scores/Cargo.toml6
-rw-r--r--high-scores/README.md95
-rw-r--r--high-scores/src/lib.rs29
-rw-r--r--high-scores/tests/high-scores.rs68
6 files changed, 207 insertions, 0 deletions
diff --git a/high-scores/.exercism/metadata.json b/high-scores/.exercism/metadata.json
new file mode 100644
index 0000000..467784c
--- /dev/null
+++ b/high-scores/.exercism/metadata.json
@@ -0,0 +1 @@
{"track":"rust","exercise":"high-scores","id":"4454fe8e84a84eb9860d6790c283b2c3","url":"https://exercism.io/my/solutions/4454fe8e84a84eb9860d6790c283b2c3","handle":"dyamon","is_requester":true,"auto_approve":false} \ No newline at end of file
diff --git a/high-scores/.gitignore b/high-scores/.gitignore
new file mode 100644
index 0000000..e130ceb
--- /dev/null
+++ b/high-scores/.gitignore
@@ -0,0 +1,8 @@
1# Generated by exercism rust track exercise tool
2# will have compiled files and executables
3/target/
4**/*.rs.bk
5
6# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
7# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
8Cargo.lock
diff --git a/high-scores/Cargo.toml b/high-scores/Cargo.toml
new file mode 100644
index 0000000..f4d70fe
--- /dev/null
+++ b/high-scores/Cargo.toml
@@ -0,0 +1,6 @@
1[dependencies]
2
3[package]
4edition = "2018"
5name = "high-scores"
6version = "4.0.0"
diff --git a/high-scores/README.md b/high-scores/README.md
new file mode 100644
index 0000000..f60b890
--- /dev/null
+++ b/high-scores/README.md
@@ -0,0 +1,95 @@
1# High Scores
2
3Manage a game player's High Score list.
4
5Your task is to build a high-score component of the classic Frogger
6game, one of the highest selling and addictive games of all time, and a
7classic of the arcade era. Your task is to write methods that return the
8highest score from the list, the last added score and the three highest
9scores.
10
11## Hints
12
13Consider retaining a reference to `scores` in the struct - copying is not
14necessary. You will require some lifetime annotations, though.
15
16
17## Rust Installation
18
19Refer to the [exercism help page][help-page] for Rust installation and learning
20resources.
21
22## Writing the Code
23
24Execute the tests with:
25
26```bash
27$ cargo test
28```
29
30All but the first test have been ignored. After you get the first test to
31pass, open the tests source file which is located in the `tests` directory
32and remove the `#[ignore]` flag from the next test and get the tests to pass
33again. Each separate test is a function with `#[test]` flag above it.
34Continue, until you pass every test.
35
36If you wish to run all ignored tests without editing the tests source file, use:
37
38```bash
39$ cargo test -- --ignored
40```
41
42To run a specific test, for example `some_test`, you can use:
43
44```bash
45$ cargo test some_test
46```
47
48If the specific test is ignored use:
49
50```bash
51$ cargo test some_test -- --ignored
52```
53
54To learn more about Rust tests refer to the [online test documentation][rust-tests]
55
56Make sure to read the [Modules][modules] chapter if you
57haven't already, it will help you with organizing your files.
58
59## Further improvements
60
61After you have solved the exercise, please consider using the additional utilities, described in the [installation guide](https://exercism.io/tracks/rust/installation), to further refine your final solution.
62
63To format your solution, inside the solution directory use
64
65```bash
66cargo fmt
67```
68
69To see, if your solution contains some common ineffective use cases, inside the solution directory use
70
71```bash
72cargo clippy --all-targets
73```
74
75## Submitting the solution
76
77Generally 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.
78
79## Feedback, Issues, Pull Requests
80
81The [exercism/rust](https://github.com/exercism/rust) repository on 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!
82
83If you want to know more about Exercism, take a look at the [contribution guide](https://github.com/exercism/docs/blob/master/contributing-to-language-tracks/README.md).
84
85[help-page]: https://exercism.io/tracks/rust/learning
86[modules]: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html
87[cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html
88[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
89
90## Source
91
92Tribute to the eighties' arcade game Frogger
93
94## Submitting Incomplete Solutions
95It's possible to submit an incomplete solution so you can see how others have completed the exercise.
diff --git a/high-scores/src/lib.rs b/high-scores/src/lib.rs
new file mode 100644
index 0000000..a7e6c3d
--- /dev/null
+++ b/high-scores/src/lib.rs
@@ -0,0 +1,29 @@
1#[derive(Debug)]
2pub struct HighScores<'a> {
3 scores: &'a [u32]
4}
5
6impl<'a> HighScores<'a> {
7 pub fn new(scores: &'a [u32]) -> Self {
8 HighScores{ scores }
9 }
10
11 pub fn scores(&self) -> &[u32] {
12 self.scores
13 }
14
15 pub fn latest(&self) -> Option<u32> {
16 self.scores.last().copied()
17 }
18
19 pub fn personal_best(&self) -> Option<u32> {
20 self.scores.iter().max().copied()
21 }
22
23 pub fn personal_top_three(&self) -> Vec<u32> {
24 let mut top3 = self.scores.to_vec();
25 top3.sort_unstable_by(|a,b| b.cmp(a));
26 top3.truncate(3);
27 top3
28 }
29}
diff --git a/high-scores/tests/high-scores.rs b/high-scores/tests/high-scores.rs
new file mode 100644
index 0000000..4d2feb7
--- /dev/null
+++ b/high-scores/tests/high-scores.rs
@@ -0,0 +1,68 @@
1use high_scores::HighScores;
2
3#[test]
4fn test_list_of_scores() {
5 let expected = [30, 50, 20, 70];
6 let high_scores = HighScores::new(&expected);
7 assert_eq!(high_scores.scores(), &expected);
8}
9
10#[test]
11fn test_latest_score() {
12 let high_scores = HighScores::new(&[100, 0, 90, 30]);
13 assert_eq!(high_scores.latest(), Some(30));
14}
15
16#[test]
17fn test_latest_score_empty() {
18 let high_scores = HighScores::new(&[]);
19 assert_eq!(high_scores.latest(), None);
20}
21
22#[test]
23fn test_personal_best() {
24 let high_scores = HighScores::new(&[40, 100, 70]);
25 assert_eq!(high_scores.personal_best(), Some(100));
26}
27
28#[test]
29fn test_personal_best_empty() {
30 let high_scores = HighScores::new(&[]);
31 assert_eq!(high_scores.personal_best(), None);
32}
33
34#[test]
35fn test_personal_top_three() {
36 let high_scores = HighScores::new(&[10, 30, 90, 30, 100, 20, 10, 0, 30, 40, 40, 70, 70]);
37 assert_eq!(high_scores.personal_top_three(), vec![100, 90, 70]);
38}
39
40#[test]
41fn test_personal_top_three_highest_to_lowest() {
42 let high_scores = HighScores::new(&[20, 10, 30]);
43 assert_eq!(high_scores.personal_top_three(), vec![30, 20, 10]);
44}
45
46#[test]
47fn test_personal_top_three_with_tie() {
48 let high_scores = HighScores::new(&[40, 20, 40, 30]);
49 assert_eq!(high_scores.personal_top_three(), vec![40, 40, 30]);
50}
51
52#[test]
53fn test_personal_top_three_with_less_than_three_scores() {
54 let high_scores = HighScores::new(&[30, 70]);
55 assert_eq!(high_scores.personal_top_three(), vec![70, 30]);
56}
57
58#[test]
59fn test_personal_top_three_only_one_score() {
60 let high_scores = HighScores::new(&[40]);
61 assert_eq!(high_scores.personal_top_three(), vec![40]);
62}
63
64#[test]
65fn test_personal_top_three_empty() {
66 let high_scores = HighScores::new(&[]);
67 assert!(high_scores.personal_top_three().is_empty());
68}