Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

High Scores: small redesign to use an instance variable to hold the scores #400

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions dev/src/Exercise@HighScores/HighScores.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,39 @@ A simple exercise, of note is how to handle the top three scores - ideally you c
Class {
#name : #HighScores,
#superclass : #Object,
#instVars : [
'scores'
],
#category : #'Exercise@HighScores'
}

{ #category : #exercism }
HighScores >> latestScore: scoreCollection [
^scoreCollection last
HighScores >> addScores: aCollection [
scores addAll: aCollection
]

{ #category : #initialization }
HighScores >> initialize [
scores := OrderedCollection new.
]

{ #category : #exercism }
HighScores >> latestScore [
^scores last
]

{ #category : #exercism }
HighScores >> personalBestScores: scoreCollection [
^scoreCollection max
HighScores >> personalBestScores [
^scores max
]

{ #category : #exercism }
HighScores >> personalTopThreeScores: aCollection [
^ (aCollection asSortedCollection: [ :a :b | a > b ]) asArray
first: (3 min: aCollection size)
HighScores >> personalTopThreeScores [
^ (scores asSortedCollection: [ :a :b | a > b ]) asArray
first: (3 min: scores size)
]

{ #category : #exercism }
HighScores >> scores: aCollection [
^aCollection
HighScores >> scores [
^scores asArray
]
67 changes: 59 additions & 8 deletions dev/src/Exercise@HighScores/HighScoresTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -55,62 +55,113 @@ HighScoresTest >> setUp [
HighScoresTest >> test01_ListOfScores [
| result |

result := highScoresCalculator scores: #(30 50 20 70 ) .
highScoresCalculator addScores: #(30 50 20 70 ) .
result := highScoresCalculator scores .
self assert: result equals: #(30 50 20 70 )
]

{ #category : #tests }
HighScoresTest >> test02_LatestScore [
| result |

result := highScoresCalculator latestScore: #(100 0 90 30 ) .
highScoresCalculator addScores: #(100 0 90 30 ) .
result := highScoresCalculator latestScore .
self assert: result equals: 30
]

{ #category : #tests }
HighScoresTest >> test03_PersonalBest [
| result |

result := highScoresCalculator personalBestScores: #(40 100 70 ) .
highScoresCalculator addScores: #(40 100 70 ) .
result := highScoresCalculator personalBestScores .
self assert: result equals: 100
]

{ #category : #tests }
HighScoresTest >> test04_Top3ScoresPersonalTopThreeFromAListOfScores [
| result |

result := highScoresCalculator personalTopThreeScores: #(10 30 90 30 100 20 10 0 30 40 40 70 70 ) .
highScoresCalculator addScores: #(10 30 90 30 100 20 10 0 30 40 40 70 70 ) .
result := highScoresCalculator personalTopThreeScores .
self assert: result equals: #(100 90 70 )
]

{ #category : #tests }
HighScoresTest >> test05_Top3ScoresPersonalTopHighestToLowest [
| result |

result := highScoresCalculator personalTopThreeScores: #(20 10 30 ) .
highScoresCalculator addScores: #(20 10 30 ) .
result := highScoresCalculator personalTopThreeScores .
self assert: result equals: #(30 20 10 )
]

{ #category : #tests }
HighScoresTest >> test06_Top3ScoresPersonalTopWhenThereIsATie [
| result |

result := highScoresCalculator personalTopThreeScores: #(40 20 40 30 ) .
highScoresCalculator addScores: #(40 20 40 30 ) .
result := highScoresCalculator personalTopThreeScores .
self assert: result equals: #(40 40 30 )
]

{ #category : #tests }
HighScoresTest >> test07_Top3ScoresPersonalTopWhenThereAreLessThan3 [
| result |

result := highScoresCalculator personalTopThreeScores: #(30 70 ) .
highScoresCalculator addScores: #(30 70 ) .
result := highScoresCalculator personalTopThreeScores .
self assert: result equals: #(70 30 )
]

{ #category : #tests }
HighScoresTest >> test08_Top3ScoresPersonalTopWhenThereIsOnlyOne [
| result |

result := highScoresCalculator personalTopThreeScores: #(40 ) .
highScoresCalculator addScores: #(40 ) .
result := highScoresCalculator personalTopThreeScores .
self assert: result equals: #(40 )
]

{ #category : #extra }
HighScoresTest >> testLoadMultipleBatchesOfScores [
highScoresCalculator addScores: #(20 50 40) .
highScoresCalculator addScores: #(30 10) .
self assert: highScoresCalculator latestScore equals: 10.
self assert: highScoresCalculator personalBestScores equals: 50.
self assert: highScoresCalculator personalTopThreeScores equals: #(50 40 30)
]

{ #category : #extra }
HighScoresTest >> testPersonalBestWithTies [
| result |

highScoresCalculator addScores: #(40 100 70 60 100) .
result := highScoresCalculator personalBestScores .
self assert: result equals: 100
]

{ #category : #extra }
HighScoresTest >> testPersonalTopThreeDoesNotSortStoredScores [
"Ensure that the process of returning the top three
does not alter the order that the scores are stored in."

| topThree |
highScoresCalculator addScores: #(1 2 3 4) .
topThree := highScoresCalculator personalTopThreeScores .
self assert: topThree equals: #(4 3 2) .
self assert: highScoresCalculator latestScore equals: 4
]

{ #category : #extra }
HighScoresTest >> testScoresAreImmutable [
"Ensure that the collection of scores returned is a copy"
| scores |

highScoresCalculator addScores: #(1 2 3 4).
scores := highScoresCalculator scores.
scores atLast: 1 put: 5.

"fetch the scores again and verify the first one"
self assert: highScoresCalculator latestScore equals: 4
]
29 changes: 21 additions & 8 deletions exercises/high-scores/.meta/solution/HighScores.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,39 @@ A simple exercise, of note is how to handle the top three scores - ideally you c
Class {
#name : #HighScores,
#superclass : #Object,
#instVars : [
'scores'
],
#category : #'Exercise@HighScores'
}

{ #category : #initialization }
HighScores >> initialize [
scores := OrderedCollection new.
]

{ #category : #exercism }
HighScores >> latestScore [
^scores last
]

{ #category : #exercism }
HighScores >> latestScore: scoreCollection [
^scoreCollection last
HighScores >> personalBestScores [
^scores max
]

{ #category : #exercism }
HighScores >> personalBestScores: scoreCollection [
^scoreCollection max
HighScores >> personalTopThreeScores [
^ (scores asSortedCollection: [ :a :b | a > b ]) asArray
first: (3 min: scores size)
]

{ #category : #exercism }
HighScores >> personalTopThreeScores: aCollection [
^ (aCollection asSortedCollection: [ :a :b | a > b ]) asArray
first: (3 min: aCollection size)
HighScores >> scores [
^scores asArray
]

{ #category : #exercism }
HighScores >> scores: aCollection [
^aCollection
scores addAll: aCollection
]
67 changes: 59 additions & 8 deletions exercises/high-scores/HighScoresTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -55,62 +55,113 @@ HighScoresTest >> setUp [
HighScoresTest >> test01_ListOfScores [
| result |

result := highScoresCalculator scores: #(30 50 20 70 ) .
highScoresCalculator scores: #(30 50 20 70 ) .
result := highScoresCalculator scores .
self assert: result equals: #(30 50 20 70 )
]

{ #category : #tests }
HighScoresTest >> test02_LatestScore [
| result |

result := highScoresCalculator latestScore: #(100 0 90 30 ) .
highScoresCalculator scores: #(100 0 90 30 ) .
result := highScoresCalculator latestScore .
self assert: result equals: 30
]

{ #category : #tests }
HighScoresTest >> test03_PersonalBest [
| result |

result := highScoresCalculator personalBestScores: #(40 100 70 ) .
highScoresCalculator scores: #(40 100 70 ) .
result := highScoresCalculator personalBestScores .
self assert: result equals: 100
]

{ #category : #tests }
HighScoresTest >> test04_Top3ScoresPersonalTopThreeFromAListOfScores [
| result |

result := highScoresCalculator personalTopThreeScores: #(10 30 90 30 100 20 10 0 30 40 40 70 70 ) .
highScoresCalculator scores: #(10 30 90 30 100 20 10 0 30 40 40 70 70 ) .
result := highScoresCalculator personalTopThreeScores .
self assert: result equals: #(100 90 70 )
]

{ #category : #tests }
HighScoresTest >> test05_Top3ScoresPersonalTopHighestToLowest [
| result |

result := highScoresCalculator personalTopThreeScores: #(20 10 30 ) .
highScoresCalculator scores: #(20 10 30 ) .
result := highScoresCalculator personalTopThreeScores .
self assert: result equals: #(30 20 10 )
]

{ #category : #tests }
HighScoresTest >> test06_Top3ScoresPersonalTopWhenThereIsATie [
| result |

result := highScoresCalculator personalTopThreeScores: #(40 20 40 30 ) .
highScoresCalculator scores: #(40 20 40 30 ) .
result := highScoresCalculator personalTopThreeScores .
self assert: result equals: #(40 40 30 )
]

{ #category : #tests }
HighScoresTest >> test07_Top3ScoresPersonalTopWhenThereAreLessThan3 [
| result |

result := highScoresCalculator personalTopThreeScores: #(30 70 ) .
highScoresCalculator scores: #(30 70 ) .
result := highScoresCalculator personalTopThreeScores .
self assert: result equals: #(70 30 )
]

{ #category : #tests }
HighScoresTest >> test08_Top3ScoresPersonalTopWhenThereIsOnlyOne [
| result |

result := highScoresCalculator personalTopThreeScores: #(40 ) .
highScoresCalculator scores: #(40 ) .
result := highScoresCalculator personalTopThreeScores .
self assert: result equals: #(40 )
]

{ #category : #extra }
HighScoresTest >> testLoadMultipleBatchesOfScores [
highScoresCalculator scores: #(20 50 40) .
highScoresCalculator scores: #(30 10) .
self assert: highScoresCalculator latestScore equals: 10.
self assert: highScoresCalculator personalBestScores equals: 50.
self assert: highScoresCalculator personalTopThreeScores equals: #(50 40 30)
]

{ #category : #extra }
HighScoresTest >> testPersonalBestWithTies [
| result |

highScoresCalculator scores: #(40 100 70 60 100) .
result := highScoresCalculator personalBestScores .
self assert: result equals: 100
]

{ #category : #extra }
HighScoresTest >> testPersonalTopThreeDoesNotSortStoredScores [
"Ensure that the process of returning the top three
does not alter the order that the scores are stored in."

| topThree |
highScoresCalculator scores: #(1 2 3 4) .
topThree := highScoresCalculator personalTopThreeScores .
self assert: topThree equals: #(4 3 2) .
self assert: highScoresCalculator latestScore equals: 4
]

{ #category : #extra }
HighScoresTest >> testScoresAreImmutable [
"Ensure that the collection of scores returned is a copy"
| scores |

highScoresCalculator scores: #(1 2 3 4).
scores := highScoresCalculator scores.
scores atLast: 1 put: 5.

"fetch the scores again and verify the first one"
self assert: highScoresCalculator latestScore equals: 4
]