Skip to content

Commit 7144e63

Browse files
habere-et-dispertirehabere
andauthored
Implementation: High Scores (#793)
-- 🤝 https://forum.exercism.org/t/new-exercise-contributions/4077 Co-authored-by: habere <habere@dispertire.org>
1 parent b076faa commit 7144e63

9 files changed

Lines changed: 291 additions & 0 deletions

File tree

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,14 @@
847847
"practices": [],
848848
"prerequisites": [],
849849
"difficulty": 1
850+
},
851+
{
852+
"slug": "high-scores",
853+
"name": "High Scores",
854+
"uuid": "94471fe8-e533-4046-a7a2-32f35d5a56ff",
855+
"practices": [],
856+
"prerequisites": [],
857+
"difficulty": 1
850858
}
851859
]
852860
},
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Instructions
2+
3+
Manage a game player's High Score list.
4+
5+
Your task is to build a high-score component of the classic Frogger game, one of the highest selling and most addictive games of all time, and a classic of the arcade era.
6+
Your task is to write methods that return the highest score from the list, the last added score and the three highest scores.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"authors": [
3+
"habere-et-dispertire"
4+
],
5+
"files": {
6+
"solution": [
7+
"lib/HighScores.rakumod"
8+
],
9+
"test": [
10+
"t/high-scores.rakutest"
11+
],
12+
"example": [
13+
".meta/solutions/lib/HighScores.rakumod"
14+
]
15+
},
16+
"blurb": "Manage a player's High Score list.",
17+
"source": "Tribute to the eighties' arcade game Frogger"
18+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
unit class HighScores;
2+
3+
has $.scores;
4+
5+
method scores { $!scores }
6+
method latest { $!scores.tail }
7+
method personal-best { $!scores.max }
8+
method personal-top-three { $!scores.sort.reverse.head(3) }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../t/high-scores.rakutest
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
unit: class
2+
3+
properties:
4+
scores:
5+
test: |-
6+
sprintf(q:to/END/, %case<input><scores>.Array.raku, %case<expected>.Array.raku, %case<description>.raku);
7+
cmp-ok(
8+
HighScores.new( scores => %s ).scores,
9+
"eqv",
10+
%s,
11+
%s,
12+
);
13+
END
14+
15+
latest:
16+
test: |-
17+
sprintf(q:to/END/, %case<input><scores>.Array.raku, %case<expected>.Int.raku, %case<description>.raku);
18+
cmp-ok(
19+
HighScores.new( scores => %s ).latest,
20+
"==",
21+
%s,
22+
%s,
23+
);
24+
END
25+
26+
personalBest:
27+
test: |-
28+
sprintf(q:to/END/, %case<input><scores>.Array.raku, %case<expected>.Int.raku, %case<description>.raku);
29+
cmp-ok(
30+
HighScores.new( scores => %s ).personal-best,
31+
"==",
32+
%s,
33+
%s,
34+
);
35+
END
36+
37+
personalTopThree:
38+
test: |-
39+
sprintf(q:to/END/, %case<input><scores>.Array.raku, %case<expected>.Int.raku, %case<description>.raku);
40+
cmp-ok(
41+
HighScores.new( scores => %s ).personal-top-three,
42+
"==",
43+
%s,
44+
%s,
45+
);
46+
END
47+
48+
latestAfterTopThree:
49+
test: |-
50+
sprintf(q:to/END/, %case<input><scores>.Array.raku, %case<expected>.Int.raku, %case<description>.raku);
51+
cmp-ok(
52+
( given HighScores.new( scores => %s ) { sink .personal-top-three; .latest } ),
53+
"==",
54+
%s,
55+
%s,
56+
);
57+
END
58+
59+
scoresAfterTopThree:
60+
test: |-
61+
sprintf(q:to/END/, %case<input><scores>.Array.raku, %case<expected>.Array.raku, %case<description>.raku);
62+
cmp-ok(
63+
( given HighScores.new( scores => %s ) { sink .personal-top-three; .scores } ),
64+
"eqv",
65+
%s,
66+
%s,
67+
);
68+
END
69+
70+
latestAfterBest:
71+
test: |-
72+
sprintf(q:to/END/, %case<input><scores>.Array.raku, %case<expected>.Int.raku, %case<description>.raku);
73+
cmp-ok(
74+
( given HighScores.new( scores => %s ) { sink .personal-best; .latest } ),
75+
"==",
76+
%s,
77+
%s,
78+
);
79+
END
80+
81+
scoresAfterBest:
82+
test: |-
83+
sprintf(q:to/END/, %case<input><scores>.Array.raku, %case<expected>.Array.raku, %case<description>.raku);
84+
cmp-ok(
85+
( given HighScores.new( scores => %s ) { sink .personal-best; .scores } ),
86+
"eqv",
87+
%s,
88+
%s,
89+
);
90+
END
91+
92+
example: |-
93+
has $.scores;
94+
95+
method scores { $!scores }
96+
method latest { $!scores.tail }
97+
method personal-best { $!scores.max }
98+
method personal-top-three { $!scores.sort.reverse.head(3) }
99+
100+
stub: |-
101+
has $.scores;
102+
103+
method scores { }
104+
method latest { }
105+
method personal-best { }
106+
method personal-top-three { }
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[1035eb93-2208-4c22-bab8-fef06769a73c]
13+
description = "List of scores"
14+
15+
[6aa5dbf5-78fa-4375-b22c-ffaa989732d2]
16+
description = "Latest score"
17+
18+
[b661a2e1-aebf-4f50-9139-0fb817dd12c6]
19+
description = "Personal best"
20+
21+
[3d996a97-c81c-4642-9afc-80b80dc14015]
22+
description = "Top 3 scores -> Personal top three from a list of scores"
23+
24+
[1084ecb5-3eb4-46fe-a816-e40331a4e83a]
25+
description = "Top 3 scores -> Personal top highest to lowest"
26+
27+
[e6465b6b-5a11-4936-bfe3-35241c4f4f16]
28+
description = "Top 3 scores -> Personal top when there is a tie"
29+
30+
[f73b02af-c8fd-41c9-91b9-c86eaa86bce2]
31+
description = "Top 3 scores -> Personal top when there are less than 3"
32+
33+
[16608eae-f60f-4a88-800e-aabce5df2865]
34+
description = "Top 3 scores -> Personal top when there is only one"
35+
36+
[2df075f9-fec9-4756-8f40-98c52a11504f]
37+
description = "Top 3 scores -> Latest score after personal top scores"
38+
39+
[809c4058-7eb1-4206-b01e-79238b9b71bc]
40+
description = "Top 3 scores -> Scores after personal top scores"
41+
42+
[ddb0efc0-9a86-4f82-bc30-21ae0bdc6418]
43+
description = "Top 3 scores -> Latest score after personal best"
44+
45+
[6a0fd2d1-4cc4-46b9-a5bb-2fb667ca2364]
46+
description = "Top 3 scores -> Scores after personal best"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
unit class HighScores;
2+
3+
has $.scores;
4+
5+
method scores { }
6+
method latest { }
7+
method personal-best { }
8+
method personal-top-three { }
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env raku
2+
use Test;
3+
use lib $?FILE.IO.parent(2).add('lib');
4+
use HighScores;
5+
6+
cmp-ok( # begin: 1035eb93-2208-4c22-bab8-fef06769a73c
7+
HighScores.new( scores => [30, 50, 20, 70] ).scores,
8+
"eqv",
9+
[30, 50, 20, 70],
10+
"List of scores",
11+
); # end: 1035eb93-2208-4c22-bab8-fef06769a73c
12+
13+
cmp-ok( # begin: 6aa5dbf5-78fa-4375-b22c-ffaa989732d2
14+
HighScores.new( scores => [100, 0, 90, 30] ).latest,
15+
"==",
16+
30,
17+
"Latest score",
18+
); # end: 6aa5dbf5-78fa-4375-b22c-ffaa989732d2
19+
20+
cmp-ok( # begin: b661a2e1-aebf-4f50-9139-0fb817dd12c6
21+
HighScores.new( scores => [40, 100, 70] ).personal-best,
22+
"==",
23+
100,
24+
"Personal best",
25+
); # end: b661a2e1-aebf-4f50-9139-0fb817dd12c6
26+
27+
cmp-ok( # begin: 3d996a97-c81c-4642-9afc-80b80dc14015
28+
HighScores.new( scores => [10, 30, 90, 30, 100, 20, 10, 0, 30, 40, 40, 70, 70] ).personal-top-three,
29+
"==",
30+
3,
31+
"Top 3 scores: Personal top three from a list of scores",
32+
); # end: 3d996a97-c81c-4642-9afc-80b80dc14015
33+
34+
cmp-ok( # begin: 1084ecb5-3eb4-46fe-a816-e40331a4e83a
35+
HighScores.new( scores => [20, 10, 30] ).personal-top-three,
36+
"==",
37+
3,
38+
"Top 3 scores: Personal top highest to lowest",
39+
); # end: 1084ecb5-3eb4-46fe-a816-e40331a4e83a
40+
41+
cmp-ok( # begin: e6465b6b-5a11-4936-bfe3-35241c4f4f16
42+
HighScores.new( scores => [40, 20, 40, 30] ).personal-top-three,
43+
"==",
44+
3,
45+
"Top 3 scores: Personal top when there is a tie",
46+
); # end: e6465b6b-5a11-4936-bfe3-35241c4f4f16
47+
48+
cmp-ok( # begin: f73b02af-c8fd-41c9-91b9-c86eaa86bce2
49+
HighScores.new( scores => [30, 70] ).personal-top-three,
50+
"==",
51+
2,
52+
"Top 3 scores: Personal top when there are less than 3",
53+
); # end: f73b02af-c8fd-41c9-91b9-c86eaa86bce2
54+
55+
cmp-ok( # begin: 16608eae-f60f-4a88-800e-aabce5df2865
56+
HighScores.new( scores => [40] ).personal-top-three,
57+
"==",
58+
1,
59+
"Top 3 scores: Personal top when there is only one",
60+
); # end: 16608eae-f60f-4a88-800e-aabce5df2865
61+
62+
cmp-ok( # begin: 2df075f9-fec9-4756-8f40-98c52a11504f
63+
( given HighScores.new( scores => [70, 50, 20, 30] ) { sink .personal-top-three; .latest } ),
64+
"==",
65+
30,
66+
"Top 3 scores: Latest score after personal top scores",
67+
); # end: 2df075f9-fec9-4756-8f40-98c52a11504f
68+
69+
cmp-ok( # begin: 809c4058-7eb1-4206-b01e-79238b9b71bc
70+
( given HighScores.new( scores => [30, 50, 20, 70] ) { sink .personal-top-three; .scores } ),
71+
"eqv",
72+
[30, 50, 20, 70],
73+
"Top 3 scores: Scores after personal top scores",
74+
); # end: 809c4058-7eb1-4206-b01e-79238b9b71bc
75+
76+
cmp-ok( # begin: ddb0efc0-9a86-4f82-bc30-21ae0bdc6418
77+
( given HighScores.new( scores => [20, 70, 15, 25, 30] ) { sink .personal-best; .latest } ),
78+
"==",
79+
30,
80+
"Top 3 scores: Latest score after personal best",
81+
); # end: ddb0efc0-9a86-4f82-bc30-21ae0bdc6418
82+
83+
cmp-ok( # begin: 6a0fd2d1-4cc4-46b9-a5bb-2fb667ca2364
84+
( given HighScores.new( scores => [20, 70, 15, 25, 30] ) { sink .personal-best; .scores } ),
85+
"eqv",
86+
[20, 70, 15, 25, 30],
87+
"Top 3 scores: Scores after personal best",
88+
); # end: 6a0fd2d1-4cc4-46b9-a5bb-2fb667ca2364
89+
90+
done-testing;

0 commit comments

Comments
 (0)