Skip to content

Commit 06f1402

Browse files
committed
Merge pull request #8 from sjakobi/matrix
python matrix added.
2 parents 19651a9 + a776b25 commit 06f1402

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

EXERCISES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
bob
22
rna-transcription
3+
matrix
34
word-count
45
anagram
56
beer-song

matrix/example.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Matrix(object):
2+
def __init__(self, s):
3+
self.rows = [[int(n) for n in row.split()]
4+
for row in s.split('\n')]
5+
6+
@property
7+
def columns(self):
8+
return map(list, zip(*self.rows))

matrix/matrix_test.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
try:
2+
from matrix import Matrix
3+
except ImportError:
4+
raise SystemExit('Could not find matrix.py. Does it exist?')
5+
6+
import unittest
7+
8+
9+
class MatrixTest(unittest.TestCase):
10+
def test_extract_a_row(self):
11+
matrix = Matrix("1 2\n10 20")
12+
self.assertEqual([1, 2], matrix.rows[0])
13+
14+
def test_extract_same_row_again(self):
15+
matrix = Matrix("9 7\n8 6")
16+
self.assertEqual([9, 7], matrix.rows[0])
17+
18+
def test_extract_other_row(self):
19+
matrix = Matrix("9 8 7\n19 18 17")
20+
self.assertEqual([19, 18, 17], matrix.rows[1])
21+
22+
def test_extract_other_row_again(self):
23+
matrix = Matrix("1 4 9\n16 25 36")
24+
self.assertEqual([16, 25, 36], matrix.rows[1])
25+
26+
def test_extract_a_column(self):
27+
matrix = Matrix("1 2 3\n4 5 6\n7 8 9\n 8 7 6")
28+
self.assertEqual([1, 4, 7, 8], matrix.columns[0])
29+
30+
def test_extract_another_column(self):
31+
matrix = Matrix("89 1903 3\n18 3 1\n9 4 800")
32+
self.assertEqual([1903, 3, 4], matrix.columns[1])
33+
34+
35+
if __name__ == '__main__':
36+
unittest.main()

0 commit comments

Comments
 (0)