Skip to content

Commit 55fa60f

Browse files
glennjsshine
authored andcommitted
Matrix: create exercise (#33)
Test has canonical test version 1.3.0: exercism/problem-specifications#1578.
1 parent 5ff4a63 commit 55fa60f

File tree

6 files changed

+206
-0
lines changed

6 files changed

+206
-0
lines changed

config.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@
7373
"lists"
7474
]
7575
},
76+
{
77+
"slug": "matrix",
78+
"uuid": "32ad6c64-82ae-434e-83e1-1cf11eecc3cc",
79+
"core": true,
80+
"unlocked_by": null,
81+
"difficulty": 3,
82+
"topics": [
83+
"lists",
84+
"transforming"
85+
]
86+
},
7687
{
7788
"slug": "series",
7889
"uuid": "6893e956-8e80-4a3e-a622-6b76a65495c4",

exercises/matrix/.meta/version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.3.0

exercises/matrix/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Matrix
2+
3+
Given a string representing a matrix of numbers, return the rows and columns of
4+
that matrix.
5+
6+
So given a string with embedded newlines like:
7+
8+
```text
9+
9 8 7
10+
5 3 2
11+
6 6 7
12+
```
13+
14+
representing this matrix:
15+
16+
```text
17+
1 2 3
18+
|---------
19+
1 | 9 8 7
20+
2 | 5 3 2
21+
3 | 6 6 7
22+
```
23+
24+
your code should be able to spit out:
25+
26+
- A list of the rows, reading each row left-to-right while moving
27+
top-to-bottom across the rows,
28+
- A list of the columns, reading each column top-to-bottom while moving
29+
from left-to-right.
30+
31+
The rows for our example matrix:
32+
33+
- 9, 8, 7
34+
- 5, 3, 2
35+
- 6, 6, 7
36+
37+
And its columns:
38+
39+
- 9, 5, 6
40+
- 8, 3, 6
41+
- 7, 2, 7
42+
43+
44+
## Submitting Incomplete Solutions
45+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
46+
47+
## Running the tests
48+
To run the test suite, execute one of the following commands:
49+
50+
```bash
51+
tclsh matrix.test # Will stop testing after the first failure.
52+
RUN_ALL=1 tclsh matrix.test # Will run all tests and report all failures.
53+
```
54+
55+
## Feedback, Issues, Pull Requests
56+
The [exercism/tcl](https://github.com/exercism/tcl) repository on GitHub is
57+
the home for all of the Tcl exercises on Exercism.
58+
59+
If you have feedback about an exercise, or want to help implementing a new
60+
one, head over there and create an issue. We'll do our best to help you!
61+
62+
## Source
63+
64+
Warmup to the `saddle-points` warmup. [http://jumpstartlab.com](http://jumpstartlab.com)
65+

exercises/matrix/example.tcl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
oo::class create Matrix {
2+
constructor {inputString} {
3+
variable rows
4+
set rows [lmap line [split $inputString \n] {split $line}]
5+
}
6+
7+
method row {n} {
8+
my variable rows
9+
return [lindex $rows $n-1]
10+
}
11+
12+
method column {n} {
13+
my variable rows
14+
return [lmap row $rows {lindex $row $n-1}]
15+
}
16+
}
17+
18+
proc matrixFrom {inputString} {
19+
Matrix new $inputString
20+
}
21+
22+
proc row {matrix n} {
23+
$matrix row $n
24+
}
25+
26+
proc column {matrix n} {
27+
$matrix column $n
28+
}

exercises/matrix/matrix.tcl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
proc matrixFrom {inputString} {
2+
throw NOT_IMPLEMENTED "Implement this procedure."
3+
}
4+
5+
proc row {matrix n} {
6+
throw NOT_IMPLEMENTED "Implement this procedure."
7+
}
8+
9+
proc column {matrix n} {
10+
throw NOT_IMPLEMENTED "Implement this procedure."
11+
}

exercises/matrix/matrix.test

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env tclsh
2+
set version 1.3.0
3+
package require tcltest
4+
namespace import ::tcltest::*
5+
source "matrix.tcl"
6+
7+
proc fail_fast {} {
8+
return [expr {
9+
![info exists ::env(RUN_ALL)]
10+
|| [string is boolean -strict $::env(RUN_ALL)]
11+
&& !$::env(RUN_ALL)
12+
}]
13+
}
14+
15+
proc failed {} {
16+
return [expr {$::tcltest::numTests(Failed) > 0}]
17+
}
18+
19+
if {[fail_fast]} {
20+
proc test args {
21+
if {[failed]} {::tcltest::configure -skip *}
22+
uplevel [list ::tcltest::test {*}$args]
23+
}
24+
}
25+
26+
proc cleanupTests {} {
27+
set failed [failed]
28+
uplevel 1 ::tcltest::cleanupTests
29+
if {$failed} {exit 1}
30+
}
31+
32+
if {$::argv0 eq [info script]} {
33+
34+
# row tests
35+
test matrix-1.1 "extract row from one number matrix" -body {
36+
set matrix [matrixFrom "1"]
37+
row $matrix 1
38+
} -returnCodes ok -result {1}
39+
40+
test matrix-1.2 "can extract row" -body {
41+
set matrix [matrixFrom "1 2\n3 4"]
42+
row $matrix 2
43+
} -returnCodes ok -result {3 4}
44+
45+
test matrix-1.3 "extract row where numbers have different widths" -body {
46+
set matrix [matrixFrom "1 2\n10 20"]
47+
row $matrix 2
48+
} -returnCodes ok -result {10 20}
49+
50+
test matrix-1.4 "can extract row from non-square matrix" -body {
51+
set matrix [matrixFrom "1 2 3\n4 5 6\n7 8 9\n8 7 6"]
52+
row $matrix 3
53+
} -returnCodes ok -result {7 8 9}
54+
55+
test matrix-1.5 "can extract row from non-square matrix with no corresponding column" -body {
56+
set matrix [matrixFrom "1 2 3\n4 5 6\n7 8 9\n8 7 6"]
57+
row $matrix 4
58+
} -returnCodes ok -result {8 7 6}
59+
60+
61+
# column tests
62+
test matrix-2.1 "extract column from one number matrix" -body {
63+
set matrix [matrixFrom "1"]
64+
column $matrix 1
65+
} -returnCodes ok -result {1}
66+
67+
test matrix-2.2 "can extract column" -body {
68+
set matrix [matrixFrom "1 2 3\n4 5 6\n7 8 9"]
69+
column $matrix 3
70+
} -returnCodes ok -result {3 6 9}
71+
72+
test matrix-2.3 "can extract column from non-square matrix" -body {
73+
set matrix [matrixFrom "1 2 3\n4 5 6\n7 8 9\n8 7 6"]
74+
column $matrix 3
75+
} -returnCodes ok -result {3 6 9 6}
76+
77+
test matrix-2.4 "can extract column from non-square matrix with no corresponding row" -body {
78+
set matrix [matrixFrom "1 2 3 4\n5 6 7 8\n9 8 7 6"]
79+
column $matrix 4
80+
} -returnCodes ok -result {4 8 6}
81+
82+
test matrix-2.5 "extract column where numbers have different widths" -body {
83+
set matrix [matrixFrom "89 1903 3\n18 3 1\n9 4 800"]
84+
column $matrix 2
85+
} -returnCodes ok -result {1903 3 4}
86+
87+
88+
cleanupTests
89+
}
90+

0 commit comments

Comments
 (0)