From 1b90a248972762947c8f3d097da50a8af82f8bbb Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Fri, 11 Oct 2024 19:44:18 +0200 Subject: [PATCH] Add `matrix` exercise --- config.json | 8 ++++ .../practice/matrix/.docs/instructions.md | 38 +++++++++++++++++++ exercises/practice/matrix/.meta/config.json | 19 ++++++++++ exercises/practice/matrix/.meta/example.R | 9 +++++ exercises/practice/matrix/.meta/tests.toml | 34 +++++++++++++++++ exercises/practice/matrix/matrix.R | 7 ++++ exercises/practice/matrix/test_matrix.R | 34 +++++++++++++++++ 7 files changed, 149 insertions(+) create mode 100644 exercises/practice/matrix/.docs/instructions.md create mode 100644 exercises/practice/matrix/.meta/config.json create mode 100644 exercises/practice/matrix/.meta/example.R create mode 100644 exercises/practice/matrix/.meta/tests.toml create mode 100644 exercises/practice/matrix/matrix.R create mode 100644 exercises/practice/matrix/test_matrix.R diff --git a/config.json b/config.json index 5a3956fb..c85b75c9 100644 --- a/config.json +++ b/config.json @@ -728,6 +728,14 @@ "practices": [], "prerequisites": [], "difficulty": 1 + }, + { + "slug": "matrix", + "name": "Matrix", + "uuid": "7a91501a-210f-4082-a029-a0a9fe368623", + "practices": [], + "prerequisites": [], + "difficulty": 2 } ] }, diff --git a/exercises/practice/matrix/.docs/instructions.md b/exercises/practice/matrix/.docs/instructions.md new file mode 100644 index 00000000..dadea8ac --- /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/config.json b/exercises/practice/matrix/.meta/config.json new file mode 100644 index 00000000..ce4fb82e --- /dev/null +++ b/exercises/practice/matrix/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "erikschierboom" + ], + "files": { + "solution": [ + "matrix.R" + ], + "test": [ + "test_matrix.R" + ], + "example": [ + ".meta/example.R" + ] + }, + "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/example.R b/exercises/practice/matrix/.meta/example.R new file mode 100644 index 00000000..46b0f967 --- /dev/null +++ b/exercises/practice/matrix/.meta/example.R @@ -0,0 +1,9 @@ +matrix_row <- function(input, row_idx) { + tbl <- read.table(text = input) + tbl[row_idx, ] |> unlist() |> unname() +} + +matrix_col <- function(input, col_idx) { + tbl <- read.table(text = input) + tbl[, col_idx] |> unlist() |> unname() +} diff --git a/exercises/practice/matrix/.meta/tests.toml b/exercises/practice/matrix/.meta/tests.toml new file mode 100644 index 00000000..90b509c4 --- /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.R b/exercises/practice/matrix/matrix.R new file mode 100644 index 00000000..3ab62b41 --- /dev/null +++ b/exercises/practice/matrix/matrix.R @@ -0,0 +1,7 @@ +matrix_row <- function(input, row_idx) { + +} + +matrix_col <- function(input, col_idx) { + +} diff --git a/exercises/practice/matrix/test_matrix.R b/exercises/practice/matrix/test_matrix.R new file mode 100644 index 00000000..fb9f0c0f --- /dev/null +++ b/exercises/practice/matrix/test_matrix.R @@ -0,0 +1,34 @@ +source("./matrix.R") +library(testthat) + +test_that("Extract row from one number matrix", { + expect_equal(matrix_row("1", 1), c(1)) +}) + +test_that("Can extract row", { + expect_equal(matrix_row("1 2\n3 4", 2), c(3, 4)) +}) + +test_that("Extract row where numbers have different widths", { + expect_equal(matrix_row("1 2\n10 20", 2), c(10, 20)) +}) + +test_that("Can extract row from non-square matrix with no corresponding column", { # nolint + expect_equal(matrix_row("1 2 3\n4 5 6\n7 8 9\n8 7 6", 4), c(8, 7, 6)) +}) + +test_that("Extract column from one number matrix", { + expect_equal(matrix_col("1", 1), c(1)) +}) + +test_that("Can extract column", { + expect_equal(matrix_col("1 2 3\n4 5 6\n7 8 9", 3), c(3, 6, 9)) +}) + +test_that("Can extract column from non-square matrix with no corresponding row", { # nolint + expect_equal(matrix_col("1 2 3 4\n5 6 7 8\n9 8 7 6", 4), c(4, 8, 6)) +}) + +test_that("Extract column where numbers have different widths", { + expect_equal(matrix_col("89 1903 3\n18 3 1\n9 4 800", 2), c(1903, 3, 4)) +})