diff --git a/config.json b/config.json index 970222b..2d3cee3 100644 --- a/config.json +++ b/config.json @@ -263,6 +263,14 @@ "prerequisites": [], "difficulty": 3 }, + { + "slug": "matrix", + "name": "Matrix", + "uuid": "c82cc59d-9428-48c8-8c17-908f1dc0ffb3", + "practices": [], + "prerequisites": [], + "difficulty": 1 + }, { "slug": "pangram", "name": "Pangram", diff --git a/exercises/practice/matrix/.docs/instructions.md b/exercises/practice/matrix/.docs/instructions.md new file mode 100644 index 0000000..dadea8a --- /dev/null +++ b/exercises/practice/matrix/.docs/instructions.md @@ -0,0 +1,38 @@ +# Instructions + +Given a string representing a matrix of numbers, return the rows and columns of that matrix. + +So given a string with embedded newlines like: + +```text +9 8 7 +5 3 2 +6 6 7 +``` + +representing this matrix: + +```text + 1 2 3 + |--------- +1 | 9 8 7 +2 | 5 3 2 +3 | 6 6 7 +``` + +your code should be able to spit out: + +- A list of the rows, reading each row left-to-right while moving top-to-bottom across the rows, +- A list of the columns, reading each column top-to-bottom while moving from left-to-right. + +The rows for our example matrix: + +- 9, 8, 7 +- 5, 3, 2 +- 6, 6, 7 + +And its columns: + +- 9, 5, 6 +- 8, 3, 6 +- 7, 2, 7 diff --git a/exercises/practice/matrix/.meta/Example.roc b/exercises/practice/matrix/.meta/Example.roc new file mode 100644 index 0000000..f1410c1 --- /dev/null +++ b/exercises/practice/matrix/.meta/Example.roc @@ -0,0 +1,34 @@ +module [row, column] + +parseRow : Str -> Result (List I64) [InvalidNumStr] +parseRow = \rowStr -> + rowStr + |> Str.trim + |> Str.split " " + |> List.map Str.trim + |> List.dropIf Str.isEmpty + |> List.mapTry Str.toI64 + +parseMatrix : Str -> Result (List (List I64)) [InvalidNumStr] +parseMatrix = \matrixStr -> + matrixStr + |> Str.trim + |> Str.split "\n" + |> List.mapTry parseRow + +column : Str, U64 -> Result (List I64) [InvalidNumStr, OutOfBounds] +column = \matrixStr, index -> + if index == 0 then + Err OutOfBounds + else + matrix = parseMatrix? matrixStr + matrix |> List.mapTry \r -> r |> List.get (index - 1) + +row : Str, U64 -> Result (List I64) [InvalidNumStr, OutOfBounds] +row = \matrixStr, index -> + if index == 0 then + Err OutOfBounds + else + matrix = parseMatrix? matrixStr + result = matrix |> List.get? (index - 1) + Ok result diff --git a/exercises/practice/matrix/.meta/config.json b/exercises/practice/matrix/.meta/config.json new file mode 100644 index 0000000..31d45ad --- /dev/null +++ b/exercises/practice/matrix/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "ageron" + ], + "files": { + "solution": [ + "Matrix.roc" + ], + "test": [ + "matrix-test.roc" + ], + "example": [ + ".meta/Example.roc" + ] + }, + "blurb": "Given a string representing a matrix of numbers, return the rows and columns of that matrix.", + "source": "Exercise by the JumpstartLab team for students at The Turing School of Software and Design.", + "source_url": "https://turing.edu" +} diff --git a/exercises/practice/matrix/.meta/template.j2 b/exercises/practice/matrix/.meta/template.j2 new file mode 100644 index 0000000..802cddc --- /dev/null +++ b/exercises/practice/matrix/.meta/template.j2 @@ -0,0 +1,14 @@ +{%- import "generator_macros.j2" as macros with context -%} +{{ macros.canonical_ref() }} +{{ macros.header() }} + +import {{ exercise | to_pascal }} exposing [row, column] + +{% for case in cases -%} +# {{ case["description"] }} +expect + matrixStr = {{ case["input"]["string"] | to_roc_multiline_string | indent(8) }} + result = matrixStr |> {{ case["property"] | to_camel }} {{ case["input"]["index"] | to_roc }} + result == Ok {{ case["expected"] | to_roc }} + +{% endfor %} diff --git a/exercises/practice/matrix/.meta/tests.toml b/exercises/practice/matrix/.meta/tests.toml new file mode 100644 index 0000000..90b509c --- /dev/null +++ b/exercises/practice/matrix/.meta/tests.toml @@ -0,0 +1,34 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[ca733dab-9d85-4065-9ef6-a880a951dafd] +description = "extract row from one number matrix" + +[5c93ec93-80e1-4268-9fc2-63bc7d23385c] +description = "can extract row" + +[2f1aad89-ad0f-4bd2-9919-99a8bff0305a] +description = "extract row where numbers have different widths" + +[68f7f6ba-57e2-4e87-82d0-ad09889b5204] +description = "can extract row from non-square matrix with no corresponding column" + +[e8c74391-c93b-4aed-8bfe-f3c9beb89ebb] +description = "extract column from one number matrix" + +[7136bdbd-b3dc-48c4-a10c-8230976d3727] +description = "can extract column" + +[ad64f8d7-bba6-4182-8adf-0c14de3d0eca] +description = "can extract column from non-square matrix with no corresponding row" + +[9eddfa5c-8474-440e-ae0a-f018c2a0dd89] +description = "extract column where numbers have different widths" diff --git a/exercises/practice/matrix/Matrix.roc b/exercises/practice/matrix/Matrix.roc new file mode 100644 index 0000000..b683cc4 --- /dev/null +++ b/exercises/practice/matrix/Matrix.roc @@ -0,0 +1,9 @@ +module [row, column] + +column : Str, U64 -> Result (List I64) _ +column = \matrixStr, index -> + crash "Please implement the 'column' function" + +row : Str, U64 -> Result (List I64) _ +row = \matrixStr, index -> + crash "Please implement the 'row' function" diff --git a/exercises/practice/matrix/matrix-test.roc b/exercises/practice/matrix/matrix-test.roc new file mode 100644 index 0000000..0a98c6f --- /dev/null +++ b/exercises/practice/matrix/matrix-test.roc @@ -0,0 +1,89 @@ +# These tests are auto-generated with test data from: +# https://github.com/exercism/problem-specifications/tree/main/exercises/matrix/canonical-data.json +# File last updated on 2024-09-20 +app [main] { + pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.15.0/SlwdbJ-3GR7uBWQo6zlmYWNYOxnvo8r6YABXD-45UOw.tar.br", +} + +main = + Task.ok {} + +import Matrix exposing [row, column] + +# extract row from one number matrix +expect + matrixStr = "1" + result = matrixStr |> row 1 + result == Ok [1] + +# can extract row +expect + matrixStr = + """ + 1 2 + 3 4 + """ + result = matrixStr |> row 2 + result == Ok [3, 4] + +# extract row where numbers have different widths +expect + matrixStr = + """ + 1 2 + 10 20 + """ + result = matrixStr |> row 2 + result == Ok [10, 20] + +# can extract row from non-square matrix with no corresponding column +expect + matrixStr = + """ + 1 2 3 + 4 5 6 + 7 8 9 + 8 7 6 + """ + result = matrixStr |> row 4 + result == Ok [8, 7, 6] + +# extract column from one number matrix +expect + matrixStr = "1" + result = matrixStr |> column 1 + result == Ok [1] + +# can extract column +expect + matrixStr = + """ + 1 2 3 + 4 5 6 + 7 8 9 + """ + result = matrixStr |> column 3 + result == Ok [3, 6, 9] + +# can extract column from non-square matrix with no corresponding row +expect + matrixStr = + """ + 1 2 3 4 + 5 6 7 8 + 9 8 7 6 + """ + result = matrixStr |> column 4 + result == Ok [4, 8, 6] + +# extract column where numbers have different widths +expect + matrixStr = + """ + 89 1903 3 + 18 3 1 + 9 4 800 + """ + result = matrixStr |> column 2 + result == Ok [1903, 3, 4] +