Skip to content
Merged
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
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,14 @@
"prerequisites": [],
"difficulty": 4
},
{
"slug": "matrix",
"name": "Matrix",
"uuid": "213bf83a-3a6b-44c5-b145-439b262cbd3d",
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "perfect-numbers",
"name": "Perfect Numbers",
Expand Down
38 changes: 38 additions & 0 deletions exercises/practice/matrix/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions exercises/practice/matrix/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"matrix.fut"
],
"test": [
"test.fut"
],
"example": [
".meta/example.fut"
]
},
"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"
}
20 changes: 20 additions & 0 deletions exercises/practice/matrix/.meta/example.fut
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def select [n] (string: [n]u8) (required_row: i32) (required_column: i32): []i32 =
let (a, count, _, _, _, _) = loop (a, count, current_row, current_column, i, number) = (replicate n 0, 0, 1, 1, 0, 0) while i < n do
let ch = string[i]
in
if ch == ' ' then (a, count, current_row, current_column + 1, i + 1, 0)
else if ch == '\n' then (a, count, current_row + 1, 1, i + 1, 0)
else
let number = 10 * number + i32.u8(ch - '0')
in
if i + 1 < n && string[i + 1] > ' ' then (a, count, current_row, current_column, i + 1, number)
else if current_row != required_row && current_column != required_column then (a, count, current_row, current_column, i + 1, 0)
else (a with [count] = number, count + 1, current_row, current_column, i + 1, 0)
in
a[0:count]

def row (string: []u8) (index: i32): []i32 =
select string index 0

def column (string: []u8) (index: i32): []i32 =
select string 0 index
34 changes: 34 additions & 0 deletions exercises/practice/matrix/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -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"
3 changes: 3 additions & 0 deletions exercises/practice/matrix/matrix.fut
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def row (string: []u8) (index: i32): []i32 = ???

def column (string: []u8) (index: i32): []i32 = ???
55 changes: 55 additions & 0 deletions exercises/practice/matrix/test.fut
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import "matrix"

-- extract row from one number matrix
-- ==
-- entry: test_row
-- input { "1" 1 }
-- output { [1] }

-- can extract row
-- ==
-- entry: test_row
-- input { "1 2\n3 4" 2 }
-- output { [3, 4] }

-- extract row where numbers have different widths
-- ==
-- entry: test_row
-- input { "1 2\n10 20" 2 }
-- output { [10, 20] }

-- can extract row from non-square matrix with no corresponding column
-- ==
-- entry: test_row
-- input { "1 2 3\n4 5 6\n7 8 9\n8 7 6" 4 }
-- output { [8, 7, 6] }

-- extract column from one number matrix
-- ==
-- entry: test_column
-- input { "1" 1 }
-- output { [1] }

-- can extract column
-- ==
-- entry: test_column
-- input { "1 2 3\n4 5 6\n7 8 9" 3 }
-- output { [3, 6, 9] }

-- can extract column from non-square matrix with no corresponding row
-- ==
-- entry: test_column
-- input { "1 2 3 4\n5 6 7 8\n9 8 7 6" 4 }
-- output { [4, 8, 6] }

-- extract column where numbers have different widths
-- ==
-- entry: test_column
-- input { "89 1903 3\n18 3 1\n9 4 800" 2 }
-- output { [1903, 3, 4] }

entry test_row (string: []u8) (index: i32): []i32 =
row string index

entry test_column (string: []u8) (index: i32): []i32 =
column string index
21 changes: 21 additions & 0 deletions generators/exercises/matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def gen_test_case(prop, description, inp, expected, f):
string = inp["string"].replace("\n", "\\n")
index = inp["index"]
if expected == []:
expected = "empty([0]i32)"
else:
expected = str(expected)

f.write(f"-- {description}\n")
f.write("-- ==\n")
f.write(f"-- entry: test_{prop}\n")
f.write(f'-- input {{ "{string}" {index} }}\n')
f.write(f"-- output {{ {expected} }}\n\n")


def gen_main(f):
f.write("entry test_row (string: []u8) (index: i32): []i32 =\n")
f.write(" row string index\n\n")

f.write("entry test_column (string: []u8) (index: i32): []i32 =\n")
f.write(" column string index\n")