Add python/matrix
This commit is contained in:
@@ -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.
|
||||||
@@ -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
|
||||||
Binary file not shown.
Binary file not shown.
@@ -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
|
||||||
@@ -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()
|
||||||
Reference in New Issue
Block a user