From 276629662dee265512084c9d0e8de777781513c6 Mon Sep 17 00:00:00 2001 From: dwrz Date: Mon, 15 Nov 2021 20:53:07 +0000 Subject: [PATCH] Add python/matrix --- python/matrix/HELP.md | 55 +++++++++++++ python/matrix/README.md | 77 ++++++++++++++++++ .../matrix/__pycache__/matrix.cpython-39.pyc | Bin 0 -> 864 bytes .../matrix_test.cpython-39-pytest-6.2.5.pyc | Bin 0 -> 2640 bytes python/matrix/matrix.py | 16 ++++ python/matrix/matrix_test.py | 45 ++++++++++ 6 files changed, 193 insertions(+) create mode 100644 python/matrix/HELP.md create mode 100644 python/matrix/README.md create mode 100644 python/matrix/__pycache__/matrix.cpython-39.pyc create mode 100644 python/matrix/__pycache__/matrix_test.cpython-39-pytest-6.2.5.pyc create mode 100644 python/matrix/matrix.py create mode 100644 python/matrix/matrix_test.py diff --git a/python/matrix/HELP.md b/python/matrix/HELP.md new file mode 100644 index 0000000..b44bfba --- /dev/null +++ b/python/matrix/HELP.md @@ -0,0 +1,55 @@ +# Help + +## Running the tests + +To run the included *tests*, run the test file using the `pytest` module, replacing `{exercise_name}`: + +```bash +$ python3 -m pytest {exercise_name}_test.py +``` + +Many IDE's and code editors have built-in support for using Pytest to run tests; check them out [here](https://github.com/exercism/python/blob/main/docs/TOOLS.md#editors-and-ides). + +For more information about running tests using `pytest`, checkout our [Python testing guide](https://github.com/exercism/python/blob/main/docs/TESTS.md#pytest). + +### Common pytest options + +- `-v` : enable verbose output. +- `-x` : stop running tests on first failure. +- `--ff` : run failures from previous test before running other test cases. + +For other options, see `python3 -m pytest -h`. + +## Submitting your solution + +You can submit your solution using the `exercism submit matrix.py` command. +This command will upload your solution to the Exercism website and print the solution page's URL. + +It's possible to submit an incomplete solution which allows you to: + +- See how others have completed the exercise +- Request help from a mentor + +## Need to get help? + +If you'd like help solving the exercise, check the following pages: + +- The [Python track's documentation](https://exercism.org/docs/tracks/python) +- [Exercism's support channel on gitter](https://gitter.im/exercism/support) +- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) + +Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. + +Below are some resources for getting help if you run into trouble: + +- [The PSF](https://www.python.org) hosts Python downloads, documentation, and community resources. +- [Python Community on Discord](https://pythondiscord.com/) is a very helpful and active community. +- [#python on Libera.chat](https://www.python.org/community/irc/) this is where the cored developers for the language hang out and get work done. +- [Exercism on Gitter](https://gitter.im/exercism/home) join the Python room for Python-related questions or problems. +- [/r/learnpython/](https://www.reddit.com/r/learnpython/) is a subreddit designed for Python learners. +- [Python Community Forums](https://discuss.python.org/) +- [Pythontutor](http://pythontutor.com/) for stepping through small code snippets visually. + + +Additionally, [StackOverflow](http://stackoverflow.com/questions/tagged/python) is a good spot to search for your problem/question to see if it has been answered already. + If not - you can always [ask](https://stackoverflow.com/help/how-to-ask) or [answer](https://stackoverflow.com/help/how-to-answer) someone else's question. \ No newline at end of file diff --git a/python/matrix/README.md b/python/matrix/README.md new file mode 100644 index 0000000..2b8cef3 --- /dev/null +++ b/python/matrix/README.md @@ -0,0 +1,77 @@ +# Matrix + +Welcome to Matrix on Exercism's Python Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## 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 + +In this exercise you're going to create a **class**. _Don't worry, it's not as complicated as you think!_ + +- [**A First Look at Classes**](https://docs.python.org/3/tutorial/classes.html#a-first-look-at-classes) from the Python 3 documentation. +- [**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. +- [**Data Structures in Python**](https://docs.python.org/3/tutorial/datastructures.html) from the Python 3 documentation. + +## Source + +### Created by + +- @sjakobi + +### Contributed to by + +- @AnAccountForReportingBugs +- @behrtam +- @BethanyG +- @cmccandless +- @danishprakash +- @Dog +- @kytrinyx +- @N-Parsons +- @pheanex +- @simmol +- @tqa236 +- @yawpitch + +### Based on + +Warmup to the `saddle-points` warmup. - http://jumpstartlab.com \ No newline at end of file diff --git a/python/matrix/__pycache__/matrix.cpython-39.pyc b/python/matrix/__pycache__/matrix.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..f68640c148fc4f93b843f072b28992f52f0bc1c6 GIT binary patch literal 864 zcmZuv&2AGh5FXpRnfN+yv^lj; zQ1JlivCqH@@C?3k;>@ilX1ob%5SC`fW6#g$ZyXN>eZckY$5s5D0sJKA?h@!6q0~cE z??HiLmnau}0*Zfvkx1}-XL0n7Q0kYcG)$O+30F+MAj;d<(X7mMq|VGTicK<0Pj!@(N=JI3O_J7mRGl}ovWU7` z8*^Bl+fd51NE<1MhT6@A1#HMX_M9#EJBkjsF*{sQ0!_+s^#C{Y;+84)6rSO0Z}|8Z z4i`qucx1S_hsOG8q4dI`?i6L3`>5!;cEe!H{-!a$a;GO%`a7QkbZ`!w`r=fcxQVb< zY%w_I`z`($^AJN30i~NB?LCLa&Gvd8Ef(4&s3zS%d4@{__eroU5|&W#tf zw_4xgbyj()ir=_#-nf9VjO;6pk;2SM&moENs~ZdNWPpDDQo-4SfAk9&p2hF0j4<_` z(${2+zRoJl0KdYj%!JlpHD*CuWp&no)?`g~3)&jH&F(;Ju@#8DnpiUC0gvJVf$66g z#sof;XYh|~FfnBNmi$iMDCgu2Iair_0T~>T3l-7tE0?j4MN6S{Dg~v7x}H@ z(|8yLEjjE%Lt{vK;E~r9Ga<(-? zzOm#4tZloKB1Nf&22&DMG^oXl5@~LMo7~Nk8@NFp z;ce)dLAhUqQh56&@~&at?M-`YQ`~{zDW;m<1yi1RXO7m%kc;V}M0DuB=9GDdhww0n z>BM94F#7BovhRz?@~gbWZ2uqm5$xJOuT<^my|XnyZf{#1`?3AR+OfO#uGO`7>?dLs zQgyV{L?&?gQUgpm9X~HxNGa7I3}^(~1jvz61v26x4MI8yh2YU~7%(q50&g_-gEa^< zTm1bZMTN!xlnCXt*${i7&~v3gprAh#0fFo!hD>~+z8_sviF~nS1Ahf`&452H0-tK* zqjQu)E8D~KiddObgjj*p94%|aJ-AP6U<4+-5cJxd7G%=*VZaRGdJ$oMY2|>MfFlKN zcPtP%2)k?Tg2-nA&vY(ObQ!X8Q1b%Muf(z%hx+~C8Ves4_$NlV3EIBs?%M6$&26X+ z?XJCTw}H2g-QAQ0($TySih@)GR*G`ob{6e`C5qd_Yc%~(6iZ<{EHPovsV63-f$MWh z6N^$mWaAOuHz>tZT=ob&eFPS2@j03=(R_^tViHr& z|FT|8!FRoYQm2`i&&S><_JSyB>>Z!dEAJkKPXavupadxBq literal 0 HcmV?d00001 diff --git a/python/matrix/matrix.py b/python/matrix/matrix.py new file mode 100644 index 0000000..6563cd3 --- /dev/null +++ b/python/matrix/matrix.py @@ -0,0 +1,16 @@ +class Matrix: + def __init__(self, matrix_string): + self.matrix = [] + for line in matrix_string.strip().split('\n'): + row = [] + for v in line.split(" "): row.append(int(v)) + self.matrix.append(row) + + def row(self, index): + return self.matrix[index-1] + + def column(self, index): + column = [] + for row in self.matrix: column.append(row[index-1]) + + return column diff --git a/python/matrix/matrix_test.py b/python/matrix/matrix_test.py new file mode 100644 index 0000000..10820ab --- /dev/null +++ b/python/matrix/matrix_test.py @@ -0,0 +1,45 @@ +import unittest + +from matrix import ( + Matrix, +) + +# Tests adapted from `problem-specifications//canonical-data.json` + + +class MatrixTest(unittest.TestCase): + def test_extract_row_from_one_number_matrix(self): + matrix = Matrix("1") + self.assertEqual(matrix.row(1), [1]) + + def test_can_extract_row(self): + matrix = Matrix("1 2\n3 4") + self.assertEqual(matrix.row(2), [3, 4]) + + def test_extract_row_where_numbers_have_different_widths(self): + matrix = Matrix("1 2\n10 20") + self.assertEqual(matrix.row(2), [10, 20]) + + def test_can_extract_row_from_non_square_matrix_with_no_corresponding_column(self): + matrix = Matrix("1 2 3\n4 5 6\n7 8 9\n8 7 6") + self.assertEqual(matrix.row(4), [8, 7, 6]) + + def test_extract_column_from_one_number_matrix(self): + matrix = Matrix("1") + self.assertEqual(matrix.column(1), [1]) + + def test_can_extract_column(self): + matrix = Matrix("1 2 3\n4 5 6\n7 8 9") + self.assertEqual(matrix.column(3), [3, 6, 9]) + + def test_can_extract_column_from_non_square_matrix_with_no_corresponding_row(self): + matrix = Matrix("1 2 3 4\n5 6 7 8\n9 8 7 6") + self.assertEqual(matrix.column(4), [4, 8, 6]) + + def test_extract_column_where_numbers_have_different_widths(self): + matrix = Matrix("89 1903 3\n18 3 1\n9 4 800") + self.assertEqual(matrix.column(2), [1903, 3, 4]) + + +if __name__ == "__main__": + unittest.main()