Skip to content

Commit 4583c38

Browse files
committed
Taking matrix challenge, solved
1 parent e31c762 commit 4583c38

File tree

5 files changed

+145
-0
lines changed

5 files changed

+145
-0
lines changed

matrix/.exercism/metadata.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"track":"python","exercise":"matrix","id":"c1309c393613453381d47116befa63da","url":"https://exercism.io/my/solutions/c1309c393613453381d47116befa63da","handle":"sinanata","is_requester":true,"auto_approve":false}

matrix/README.md

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
In this exercise you're going to create a **class**. _Don't worry, it's not as complicated as you think!_
44+
45+
- [**A First Look at Classes**](https://docs.python.org/3/tutorial/classes.html#a-first-look-at-classes) from the Python 3 documentation.
46+
- [**How to Define a Class in Python**](https://realpython.com/python3-object-oriented-programming/#how-to-define-a-class-in-python) from the Real Python website.
47+
- [**Data Structures in Python**](https://docs.python.org/3/tutorial/datastructures.html) from the Python 3 documentation.
48+
49+
50+
## Exception messages
51+
52+
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to
53+
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not
54+
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
55+
a message.
56+
57+
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
58+
`raise Exception`, you should write:
59+
60+
```python
61+
raise Exception("Meaningful message indicating the source of the error")
62+
```
63+
64+
## Running the tests
65+
66+
To run the tests, run `pytest matrix_test.py`
67+
68+
Alternatively, you can tell Python to run the pytest module:
69+
`python -m pytest matrix_test.py`
70+
71+
### Common `pytest` options
72+
73+
- `-v` : enable verbose output
74+
- `-x` : stop running tests on first failure
75+
- `--ff` : run failures from previous test before running other test cases
76+
77+
For other options, see `python -m pytest -h`
78+
79+
## Submitting Exercises
80+
81+
Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/matrix` directory.
82+
83+
You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`.
84+
85+
For more detailed information about running tests, code style and linting,
86+
please see [Running the Tests](http://exercism.io/tracks/python/tests).
87+
88+
## Source
89+
90+
Warmup to the `saddle-points` warmup. [http://jumpstartlab.com](http://jumpstartlab.com)
91+
92+
## Submitting Incomplete Solutions
93+
94+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

matrix/matrix.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Matrix(object):
2+
def __init__(self, matrix_string):
3+
A = matrix_string.split('\n')
4+
rows = [list(map(int,a.split(' '))) for a in A]
5+
cols = list(map(list,zip(*rows)))
6+
self.row = lambda index:rows[index-1]
7+
self.column = lambda index:cols[index-1]

matrix/matrix.pyc

1.02 KB
Binary file not shown.

matrix/matrix_test.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import unittest
2+
3+
from matrix import Matrix
4+
5+
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.3.0
6+
7+
8+
class MatrixTest(unittest.TestCase):
9+
def test_extract_row_from_one_number_matrix(self):
10+
matrix = Matrix("1")
11+
self.assertEqual(matrix.row(1), [1])
12+
13+
def test_can_extract_row(self):
14+
matrix = Matrix("1 2\n3 4")
15+
self.assertEqual(matrix.row(2), [3, 4])
16+
17+
def test_extract_row_where_numbers_have_different_widths(self):
18+
matrix = Matrix("1 2\n10 20")
19+
self.assertEqual(matrix.row(2), [10, 20])
20+
21+
def test_can_extract_row_from_non_square_matrix_with_no_corresponding_column(self):
22+
matrix = Matrix("1 2 3\n4 5 6\n7 8 9\n8 7 6")
23+
self.assertEqual(matrix.row(4), [8, 7, 6])
24+
25+
def test_extract_column_from_one_number_matrix(self):
26+
matrix = Matrix("1")
27+
self.assertEqual(matrix.column(1), [1])
28+
29+
def test_can_extract_column(self):
30+
matrix = Matrix("1 2 3\n4 5 6\n7 8 9")
31+
self.assertEqual(matrix.column(3), [3, 6, 9])
32+
33+
def test_can_extract_column_from_non_square_matrix_with_no_corresponding_row(self):
34+
matrix = Matrix("1 2 3 4\n5 6 7 8\n9 8 7 6")
35+
self.assertEqual(matrix.column(4), [4, 8, 6])
36+
37+
def test_extract_column_where_numbers_have_different_widths(self):
38+
matrix = Matrix("89 1903 3\n18 3 1\n9 4 800")
39+
self.assertEqual(matrix.column(2), [1903, 3, 4])
40+
41+
42+
if __name__ == "__main__":
43+
unittest.main()

0 commit comments

Comments
 (0)