Skip to content

Commit

Permalink
Update exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
lpil committed Jan 16, 2024
1 parent 0bb626b commit 351e5c3
Show file tree
Hide file tree
Showing 38 changed files with 531 additions and 545 deletions.
1 change: 0 additions & 1 deletion .tool-versions

This file was deleted.

58 changes: 33 additions & 25 deletions exercise_generator/src/exercise_generator.gleam
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import gleam/io
import gleam/map.{type Map}
import gleam/dict.{type Dict}
import gleam/function
import gleam/list
import gleam/int
Expand Down Expand Up @@ -28,7 +28,7 @@ type TestData {
reimplements: Result(String, Nil),
description: String,
function: String,
input: Map(String, JsonData),
input: Dict(String, JsonData),
expected: JsonData,
)
}
Expand All @@ -54,7 +54,7 @@ type JsonData {
JsonFloat(Float)
JsonString(String)
JsonList(List(JsonData))
JsonObject(Map(String, JsonData))
JsonObject(Dict(String, JsonData))
}

fn json_data_to_gleam_type(data: JsonData) -> String {
Expand Down Expand Up @@ -83,7 +83,7 @@ fn json_data_to_gleam_value(data: JsonData) -> String {
JsonObject(map) -> {
let args =
map
|> map.to_list
|> dict.to_list
|> list.map(fn(arg) {
clean_variable(arg.0) <> ": " <> json_data_to_gleam_value(arg.1)
})
Expand All @@ -105,11 +105,11 @@ fn have_same_type(a: JsonData, b: JsonData) -> Bool {
JsonObject(x), JsonObject(y) -> {
let x: List(#(String, JsonData)) =
x
|> map.to_list
|> dict.to_list
|> list.sort(fn(a, b) { string.compare(a.0, b.0) })
let y: List(#(String, JsonData)) =
y
|> map.to_list
|> dict.to_list
|> list.sort(fn(a, b) { string.compare(a.0, b.0) })
let comparaison =
list.strict_zip(x, y)
Expand Down Expand Up @@ -181,7 +181,7 @@ fn write_solution_files(

let content =
functions
|> map.to_list
|> dict.to_list
|> list.sort(by: fn(a, b) { int.compare({ a.1 }.order, { b.1 }.order) })
|> list.map(fn(item) {
let #(name, Function(arguments, return_type, can_error, need_labels, _)) =
Expand Down Expand Up @@ -215,7 +215,13 @@ fn write_solution_files(
})
|> string.join(", ")

"pub fn " <> clean_variable(name) <> "(" <> args <> ") -> " <> return <> " {\n todo \n}"
"pub fn "
<> clean_variable(name)
<> "("
<> args
<> ") -> "
<> return
<> " {\n todo \n}"
})
|> string.join("\n")

Expand All @@ -237,14 +243,14 @@ fn write_solution_files(
let assert Ok(Nil) = file.write(content, example_path)
}

fn functions_to_implement(test_cases: List(TestCase)) -> Map(String, Function) {
list.fold(over: test_cases, from: map.new(), with: check_test_case)
fn functions_to_implement(test_cases: List(TestCase)) -> Dict(String, Function) {
list.fold(over: test_cases, from: dict.new(), with: check_test_case)
}

fn check_test_case(
functions: Map(String, Function),
functions: Dict(String, Function),
test_case: TestCase,
) -> Map(String, Function) {
) -> Dict(String, Function) {
case test_case {
TestGroup(cases: cases, ..) ->
list.fold(over: cases, from: functions, with: check_test_case)
Expand All @@ -255,13 +261,13 @@ fn check_test_case(
..,
)) -> {
let can_error = case expected {
JsonObject(object) -> map.has_key(object, "error")
JsonObject(object) -> dict.has_key(object, "error")
_ -> False
}

let args =
input
|> map.to_list
|> dict.to_list
|> list.map(fn(arg) { Argument(arg.0, arg.1) })

let need_labels = case args {
Expand All @@ -272,7 +278,7 @@ fn check_test_case(
_ -> True
}

let current_function = case map.get(functions, function) {
let current_function = case dict.get(functions, function) {
Ok(func) -> {
let func =
Function(..func, need_labels: func.need_labels || need_labels)
Expand All @@ -284,10 +290,10 @@ fn check_test_case(
}

Error(Nil) ->
Function(args, expected, can_error, need_labels, map.size(functions))
Function(args, expected, can_error, need_labels, dict.size(functions))
}

map.insert(functions, function, current_function)
dict.insert(functions, function, current_function)
}
}
}
Expand Down Expand Up @@ -334,7 +340,7 @@ fn print_comments(comments: List(String)) -> String {
fn print_tests(
slug: String,
prefix: String,
functions: Map(String, Function),
functions: Dict(String, Function),
tests: List(TestCase),
) -> String {
tests
Expand All @@ -345,7 +351,7 @@ fn print_tests(
fn print_test(
slug: String,
prefix: String,
functions: Map(String, Function),
functions: Dict(String, Function),
test: TestCase,
) -> String {
case test {
Expand All @@ -368,18 +374,20 @@ fn print_test(
[
"This test reimplements the test with uuid " <> uuid,
"Please identify that test and remove it. Link:",
"https://github.com/exercism/problem-specifications/blob/main/exercises/" <> slug <> "/canonical-data.json",
"https://github.com/exercism/problem-specifications/blob/main/exercises/"
<> slug
<> "/canonical-data.json",
]
|> list.append(comments)
|> print_comments()
_ -> print_comments(comments)
}
let test_name = flatten_description(prefix <> description)
let assert Ok(Function(need_labels: need_labels, ..)) =
map.get(functions, function)
dict.get(functions, function)
let input =
input
|> map.to_list
|> dict.to_list
|> list.map(fn(item) {
case need_labels {
True ->
Expand Down Expand Up @@ -424,14 +432,14 @@ fn flatten_description(description: String) -> String {

fn get_expected_value(
function: String,
functions: Map(String, Function),
functions: Dict(String, Function),
expected: JsonData,
) {
case map.get(functions, function) {
case dict.get(functions, function) {
Ok(Function(can_error: True, ..)) ->
case expected {
JsonObject(map) ->
case map.get(map, "error") {
case dict.get(map, "error") {
Ok(value) -> "Error(" <> json_data_to_gleam_value(value) <> ")"
Error(Nil) -> "Ok(" <> json_data_to_gleam_value(expected) <> ")"
}
Expand Down
16 changes: 8 additions & 8 deletions exercises/concept/high-score-board/.meta/example.gleam
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
import gleam/map.{type Map}
import gleam/dict.{type Dict}

pub type ScoreBoard =
Map(String, Int)
Dict(String, Int)

pub fn create_score_board() -> ScoreBoard {
map.from_list([#("The Best Ever", 1_000_000)])
dict.from_list([#("The Best Ever", 1_000_000)])
}

pub fn add_player(
score_board: ScoreBoard,
player: String,
score: Int,
) -> ScoreBoard {
map.insert(score_board, player, score)
dict.insert(score_board, player, score)
}

pub fn remove_player(score_board: ScoreBoard, player: String) -> ScoreBoard {
map.delete(score_board, player)
dict.delete(score_board, player)
}

pub fn update_score(
score_board: ScoreBoard,
player: String,
points: Int,
) -> ScoreBoard {
case map.get(score_board, player) {
Ok(score) -> map.insert(score_board, player, score + points)
case dict.get(score_board, player) {
Ok(score) -> dict.insert(score_board, player, score + points)
Error(Nil) -> score_board
}
}

pub fn apply_monday_bonus(score_board: ScoreBoard) -> ScoreBoard {
map.map_values(score_board, fn(_, score) { score + 100 })
dict.map_values(score_board, fn(_, score) { score + 100 })
}
4 changes: 2 additions & 2 deletions exercises/concept/high-score-board/src/high_score_board.gleam
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import gleam/map.{type Map}
import gleam/dict.{type Dict}

pub type ScoreBoard =
Map(String, Int)
Dict(String, Int)

pub fn create_score_board() -> ScoreBoard {
todo
Expand Down
22 changes: 11 additions & 11 deletions exercises/concept/high-score-board/test/high_score_board_test.gleam
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import high_score_board
import exercism/test_runner
import exercism/should
import gleam/map
import gleam/dict

pub fn main() {
test_runner.main()
}

pub fn create_score_board_test() {
high_score_board.create_score_board()
|> should.equal(map.from_list([#("The Best Ever", 1_000_000)]))
|> should.equal(dict.from_list([#("The Best Ever", 1_000_000)]))
}

pub fn add_player_board_test() {
[#("Amil Pastorius", 99_373), #("Min-seo Shin", 0)]
|> map.from_list
|> dict.from_list
|> high_score_board.add_player("Jessie Johnson", 1337)
|> should.equal(map.from_list([
|> should.equal(dict.from_list([
#("Amil Pastorius", 99_373),
#("Min-seo Shin", 0),
#("Jessie Johnson", 1337),
Expand All @@ -25,17 +25,17 @@ pub fn add_player_board_test() {

pub fn remove_player_test() {
[#("Amil Pastorius", 99_373), #("Min-seo Shin", 0), #("Jesse Johnson", 1337)]
|> map.from_list
|> dict.from_list
|> high_score_board.remove_player("Jesse Johnson")
|> should.equal(map.from_list([
|> should.equal(dict.from_list([
#("Amil Pastorius", 99_373),
#("Min-seo Shin", 0),
]))
}

pub fn remove_player_unknown_test() {
let board =
map.from_list([
dict.from_list([
#("Amil Pastorius", 99_373),
#("Min-seo Shin", 0),
#("Jesse Johnson", 1337),
Expand All @@ -48,11 +48,11 @@ pub fn remove_player_unknown_test() {

pub fn update_score_test() {
[#("Amil Pastorius", 99_373), #("Min-seo Shin", 0), #("Jesse Johnson", 1337)]
|> map.from_list
|> dict.from_list
|> high_score_board.update_score("Min-seo Shin", 1999)
|> high_score_board.update_score("Jesse Johnson", 1337)
|> high_score_board.update_score("Unknown player", 1)
|> should.equal(map.from_list([
|> should.equal(dict.from_list([
#("Amil Pastorius", 99_373),
#("Min-seo Shin", 1999),
#("Jesse Johnson", 2674),
Expand All @@ -61,9 +61,9 @@ pub fn update_score_test() {

pub fn apply_monday_bonus_test() {
[#("Amil Pastorius", 345), #("Min-seo Shin", 19), #("Jesse Johnson", 122)]
|> map.from_list
|> dict.from_list
|> high_score_board.apply_monday_bonus
|> should.equal(map.from_list([
|> should.equal(dict.from_list([
#("Amil Pastorius", 445),
#("Min-seo Shin", 119),
#("Jesse Johnson", 222),
Expand Down
14 changes: 5 additions & 9 deletions exercises/practice/accumulate/test/accumulate_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,10 @@ pub fn accumulate_reversed_strings_test() {
}

pub fn accumulate_recursively_test() {
accumulate.accumulate(
["a", "b", "c"],
fn(x) -> List(String) {
accumulate.accumulate(
["1", "2", "3"],
fn(y) -> String { string.append(x, y) },
)
},
)
accumulate.accumulate(["a", "b", "c"], fn(x) -> List(String) {
accumulate.accumulate(["1", "2", "3"], fn(y) -> String {
string.append(x, y)
})
})
|> should.equal([["a1", "a2", "a3"], ["b1", "b2", "b3"], ["c1", "c2", "c3"]])
}
Loading

0 comments on commit 351e5c3

Please sign in to comment.