diff --git a/elixir/matrix/.exercism/config.json b/elixir/matrix/.exercism/config.json new file mode 100644 index 0000000..bab721c --- /dev/null +++ b/elixir/matrix/.exercism/config.json @@ -0,0 +1,26 @@ +{ + "authors": [ + "DoggettCK" + ], + "contributors": [ + "angelikatyborska", + "Cohen-Carlisle", + "devonestes", + "neenjaw", + "sotojuan" + ], + "files": { + "solution": [ + "lib/matrix.ex" + ], + "test": [ + "test/matrix_test.exs" + ], + "example": [ + ".meta/example.ex" + ] + }, + "blurb": "Given a string representing a matrix of numbers, return the rows and columns of that matrix.", + "source": "Exercise by the JumpstartLab team for students at The Turing School of Software and Design.", + "source_url": "https://turing.edu" +} diff --git a/elixir/matrix/.exercism/metadata.json b/elixir/matrix/.exercism/metadata.json new file mode 100644 index 0000000..57f1a97 --- /dev/null +++ b/elixir/matrix/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"matrix","id":"fe0c39c0c4124236a5ff391451e0c59d","url":"https://exercism.org/tracks/elixir/exercises/matrix","handle":"negrienko","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/elixir/matrix/.formatter.exs b/elixir/matrix/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/matrix/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/matrix/.gitignore b/elixir/matrix/.gitignore new file mode 100644 index 0000000..02bfeee --- /dev/null +++ b/elixir/matrix/.gitignore @@ -0,0 +1,24 @@ +# The directory Mix will write compiled artifacts to. +/_build/ + +# If you run "mix test --cover", coverage assets end up here. +/cover/ + +# The directory Mix downloads your dependencies sources to. +/deps/ + +# Where third-party dependencies like ExDoc output generated docs. +/doc/ + +# Ignore .fetch files in case you like to edit your project deps locally. +/.fetch + +# If the VM crashes, it generates a dump, let's ignore it too. +erl_crash.dump + +# Also ignore archive artifacts (built via "mix archive.build"). +*.ez + +# Ignore package tarball (built via "mix hex.build"). +matrix-*.tar + diff --git a/elixir/matrix/HELP.md b/elixir/matrix/HELP.md new file mode 100644 index 0000000..b9c6afc --- /dev/null +++ b/elixir/matrix/HELP.md @@ -0,0 +1,75 @@ +# Help + +## Running the tests + +From the terminal, change to the base directory of the exercise then execute the tests with: + +```bash +$ mix test +``` + +This will execute the test file found in the `test` subfolder -- a file ending in `_test.exs` + +Documentation: + +* [`mix test` - Elixir's test execution tool](https://hexdocs.pm/mix/Mix.Tasks.Test.html) +* [`ExUnit` - Elixir's unit test library](https://hexdocs.pm/ex_unit/ExUnit.html) + +## Pending tests + +In test suites of practice exercises, all but the first test have been tagged to be skipped. + +Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol. + +For example: + +```elixir +# @tag :pending +test "shouting" do + assert Bob.hey("WATCH OUT!") == "Whoa, chill out!" +end +``` + +If you wish to run all tests at once, you can include all skipped test by using the `--include` flag on the `mix test` command: + +```bash +$ mix test --include pending +``` + +Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the file `test/test_helper.exs`. + +```elixir +# ExUnit.configure(exclude: :pending, trace: true) +``` + +## Useful `mix test` options + +* `test/.exs:LINENUM` - runs only a single test, the test from `.exs` whose definition is on line `LINENUM` +* `--failed` - runs only tests that failed the last time they ran +* `--max-failures` - the suite stops evaluating tests when this number of test failures +is reached +* `--seed 0` - disables randomization so the tests in a single file will always be ran +in the same order they were defined in + +## Submitting your solution + +You can submit your solution using the `exercism submit lib/matrix.ex` 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 [Elixir track's documentation](https://exercism.org/docs/tracks/elixir) +- The [Elixir track's programming category on the forum](https://forum.exercism.org/c/programming/elixir) +- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5) +- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) + +Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. + +If you're stuck on something, it may help to look at some of the [available resources](https://exercism.org/docs/tracks/elixir/resources) out there where answers might be found. \ No newline at end of file diff --git a/elixir/matrix/README.md b/elixir/matrix/README.md new file mode 100644 index 0000000..4896dde --- /dev/null +++ b/elixir/matrix/README.md @@ -0,0 +1,61 @@ +# Matrix + +Welcome to Matrix on Exercism's Elixir 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 + +## Source + +### Created by + +- @DoggettCK + +### Contributed to by + +- @angelikatyborska +- @Cohen-Carlisle +- @devonestes +- @neenjaw +- @sotojuan + +### Based on + +Exercise by the JumpstartLab team for students at The Turing School of Software and Design. - https://turing.edu \ No newline at end of file diff --git a/elixir/matrix/lib/matrix.ex b/elixir/matrix/lib/matrix.ex new file mode 100644 index 0000000..f6fd7b9 --- /dev/null +++ b/elixir/matrix/lib/matrix.ex @@ -0,0 +1,60 @@ +defmodule Matrix do + defstruct ~w(rows columns)a + + defp at(list, index), do: Enum.at(list, index - 1) + + defp transpose(list), do: Enum.zip_with(list, &Function.identity/1) + + defp parse_columns(string), do: String.split(string, "\n") + + defp parse_row(row) do + row + |> String.split(" ") + |> Enum.map(&String.to_integer/1) + end + + @doc """ + Convert an `input` string, with rows separated by newlines and values + separated by single spaces, into a `Matrix` struct. + """ + @spec from_string(input :: String.t()) :: %Matrix{} + def from_string(input) do + matrix = + input + |> parse_columns() + |> Enum.map(&parse_row/1) + + struct(__MODULE__, rows: matrix, columns: transpose(matrix)) + end + + @doc """ + Write the `matrix` out as a string, with rows separated by newlines and + values separated by single spaces. + """ + @spec to_string(matrix :: %Matrix{}) :: String.t() + def to_string(matrix), do: Enum.map_join(matrix.rows, "\n", &Enum.join(&1, " ")) + + @doc """ + Given a `matrix`, return its rows as a list of lists of integers. + """ + @spec rows(matrix :: %Matrix{}) :: list(list(integer)) + def rows(matrix), do: matrix.rows + + @doc """ + Given a `matrix` and `index`, return the row at `index`. + """ + @spec row(matrix :: %Matrix{}, index :: integer) :: list(integer) + def row(matrix, index), do: at(matrix.rows, index) + + @doc """ + Given a `matrix`, return its columns as a list of lists of integers. + """ + @spec columns(matrix :: %Matrix{}) :: list(list(integer)) + def columns(matrix), do: matrix.columns + + @doc """ + Given a `matrix` and `index`, return the column at `index`. + """ + @spec column(matrix :: %Matrix{}, index :: integer) :: list(integer) + def column(matrix, index), do: at(matrix.columns, index) +end diff --git a/elixir/matrix/mix.exs b/elixir/matrix/mix.exs new file mode 100644 index 0000000..047ab8a --- /dev/null +++ b/elixir/matrix/mix.exs @@ -0,0 +1,28 @@ +defmodule Matrix.MixProject do + use Mix.Project + + def project do + [ + app: :matrix, + version: "0.1.0", + # elixir: "~> 1.8", + start_permanent: Mix.env() == :prod, + deps: deps() + ] + end + + # Run "mix help compile.app" to learn about applications. + def application do + [ + extra_applications: [:logger] + ] + end + + # Run "mix help deps" to learn about dependencies. + defp deps do + [ + # {:dep_from_hexpm, "~> 0.3.0"}, + # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} + ] + end +end diff --git a/elixir/matrix/test/matrix_test.exs b/elixir/matrix/test/matrix_test.exs new file mode 100644 index 0000000..a6558bd --- /dev/null +++ b/elixir/matrix/test/matrix_test.exs @@ -0,0 +1,78 @@ +defmodule MatrixTest do + use ExUnit.Case + + describe "row" do + test "extract row from one number matrix" do + matrix = Matrix.from_string("1") + assert Matrix.row(matrix, 1) == [1] + end + + test "can extract row" do + matrix = Matrix.from_string("1 2\n3 4") + assert Matrix.row(matrix, 2) == [3, 4] + end + + test "extract row where numbers have different widths" do + matrix = Matrix.from_string("1 2\n10 20") + assert Matrix.row(matrix, 2) == [10, 20] + end + + test "can extract row from non-square matrix with no corresponding column" do + matrix = Matrix.from_string("1 2 3\n4 5 6\n7 8 9\n8 7 6") + assert Matrix.row(matrix, 4) == [8, 7, 6] + end + end + + describe "rows" do + test "rows should return nested lists regardless of internal structure" do + matrix = Matrix.from_string("1 2 3\n4 5 6\n7 8 9") + + assert Matrix.rows(matrix) == [ + [1, 2, 3], + [4, 5, 6], + [7, 8, 9] + ] + end + end + + describe "column" do + test "extract column from one number matrix" do + matrix = Matrix.from_string("1") + assert Matrix.column(matrix, 1) == [1] + end + + test "can extract column" do + matrix = Matrix.from_string("1 2 3\n4 5 6\n7 8 9") + assert Matrix.column(matrix, 3) == [3, 6, 9] + end + + test "can extract column from non-square matrix with no corresponding row" do + matrix = Matrix.from_string("1 2 3 4\n5 6 7 8\n9 8 7 6") + assert Matrix.column(matrix, 4) == [4, 8, 6] + end + + test "extract column where numbers have different widths" do + matrix = Matrix.from_string("89 1903 3\n18 3 1\n9 4 800") + assert Matrix.column(matrix, 2) == [1903, 3, 4] + end + end + + describe "columns" do + test "columns should return nested lists regardless of internal structure" do + matrix = Matrix.from_string("1 2 3\n4 5 6\n7 8 9") + + assert Matrix.columns(matrix) == [ + [1, 4, 7], + [2, 5, 8], + [3, 6, 9] + ] + end + end + + describe "to_string" do + test "writing to string" do + matrix = Matrix.from_string("1 2 3\n4 5 6\n7 8 9") + assert Matrix.to_string(matrix) == "1 2 3\n4 5 6\n7 8 9" + end + end +end diff --git a/elixir/matrix/test/test_helper.exs b/elixir/matrix/test/test_helper.exs new file mode 100644 index 0000000..35fc5bf --- /dev/null +++ b/elixir/matrix/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true)