Skip to content

Commit

Permalink
Add high-scores exercise (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom authored Oct 8, 2024
1 parent b667f92 commit e5224a2
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 57 deletions.
15 changes: 15 additions & 0 deletions exercises/practice/high-scores/.meta/example.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
scores_list <- function(scores) {
scores
}

latest <- function(scores) {
tail(scores, n = 1)
}

personal_best <- function(scores) {
max(scores)
}

personal_top_three <- function(scores) {
sort(scores, decreasing = TRUE)[1:(min(length(scores), 3))]
}
15 changes: 15 additions & 0 deletions exercises/practice/high-scores/high-scores.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
scores_list <- function(scores) {

}

latest <- function(scores) {

}

personal_best <- function(scores) {

}

personal_top_three <- function(scores) {

}
123 changes: 66 additions & 57 deletions exercises/practice/high-scores/test_high-scores.R
Original file line number Diff line number Diff line change
@@ -1,69 +1,78 @@
source("./high-scores.R")
library(testthat)

test_that("list of scores", {
scores <- c(30, 50, 20, 70)
expected <- c(30, 50, 20, 70)
expect_equal(scores_list(scores), expected)
})

def test_list_of_scores(self):
scores = [30, 50, 20, 70]
expected = [30, 50, 20, 70]
self.assertEqual(HighScores(scores).scores, expected)
test_that("latest score", {
scores <- c(100, 0, 90, 30)
expected <- 30
expect_equal(latest(scores), expected)
})

def test_latest_score(self):
scores = [100, 0, 90, 30]
expected = 30
self.assertEqual(HighScores(scores).latest(), expected)
test_that("personal best", {
scores <- c(40, 100, 70)
expected <- 100
expect_equal(personal_best(scores), expected)
})

def test_personal_best(self):
scores = [40, 100, 70]
expected = 100
self.assertEqual(HighScores(scores).personal_best(), expected)
test_that("personal top three from a list of scores", {
scores <- c(10, 30, 90, 30, 100, 20, 10, 0, 30, 40, 40, 70, 70)
expected <- c(100, 90, 70)
expect_equal(personal_top_three(scores), expected)
})

def test_personal_top_three_from_a_list_of_scores(self):
scores = [10, 30, 90, 30, 100, 20, 10, 0, 30, 40, 40, 70, 70]
expected = [100, 90, 70]
self.assertEqual(HighScores(scores).personal_top_three(), expected)
test_that("personal top highest to lowest", {
scores <- c(20, 10, 30)
expected <- c(30, 20, 10)
expect_equal(personal_top_three(scores), expected)
})

def test_personal_top_highest_to_lowest(self):
scores = [20, 10, 30]
expected = [30, 20, 10]
self.assertEqual(HighScores(scores).personal_top_three(), expected)
test_that("personal top when there is a tie", {
scores <- c(40, 20, 40, 30)
expected <- c(40, 40, 30)
expect_equal(personal_top_three(scores), expected)
})

def test_personal_top_when_there_is_a_tie(self):
scores = [40, 20, 40, 30]
expected = [40, 40, 30]
self.assertEqual(HighScores(scores).personal_top_three(), expected)
test_that("personal top when there are less than 3", {
scores <- c(30, 70)
expected <- c(70, 30)
expect_equal(personal_top_three(scores), expected)
})

def test_personal_top_when_there_are_less_than_3(self):
scores = [30, 70]
expected = [70, 30]
self.assertEqual(HighScores(scores).personal_top_three(), expected)
test_that("personal top when there is only one", {
scores <- c(40)
expected <- c(40)
expect_equal(personal_top_three(scores), expected)
})

def test_personal_top_when_there_is_only_one(self):
scores = [40]
expected = [40]
self.assertEqual(HighScores(scores).personal_top_three(), expected)
test_that("latest score after personal top scores", {
scores <- c(70, 50, 20, 30)
expected <- 30
personal_top_three(scores)
expect_equal(latest(scores), expected)
})

def test_latest_score_after_personal_top_scores(self):
scores = [70, 50, 20, 30]
expected = 30
highscores = HighScores(scores)
highscores.personal_top_three()
self.assertEqual(highscores.latest(), expected)
test_that("scores after personal top scores", {
scores <- c(30, 50, 20, 70)
expected <- c(30, 50, 20, 70)
personal_top_three(scores)
expect_equal(scores_list(scores), expected)
})

def test_scores_after_personal_top_scores(self):
scores = [30, 50, 20, 70]
expected = [30, 50, 20, 70]
highscores = HighScores(scores)
highscores.personal_top_three()
self.assertEqual(highscores.scores, expected)
test_that("latest score after personal best", {
scores <- c(20, 70, 15, 25, 30)
expected <- 30
personal_best(scores)
expect_equal(latest(scores), expected)
})

def test_latest_score_after_personal_best(self):
scores = [20, 70, 15, 25, 30]
expected = 30
highscores = HighScores(scores)
highscores.personal_best()
self.assertEqual(highscores.latest(), expected)

def test_scores_after_personal_best(self):
scores = [20, 70, 15, 25, 30]
expected = [20, 70, 15, 25, 30]
highscores = HighScores(scores)
highscores.personal_best()
self.assertEqual(highscores.scores, expected)
test_that("scores after personal best", {
scores <- c(20, 70, 15, 25, 30)
expected <- c(20, 70, 15, 25, 30)
personal_best(scores)
expect_equal(scores_list(scores), expected)
})

0 comments on commit e5224a2

Please sign in to comment.