pascals_triangle

This commit is contained in:
Danil Negrienko 2024-07-07 00:41:15 -04:00
parent 44ca0cb19b
commit 58eac331ab
10 changed files with 376 additions and 0 deletions

View File

@ -0,0 +1,30 @@
{
"authors": [
"dalexj"
],
"contributors": [
"amencarini",
"angelikatyborska",
"Cohen-Carlisle",
"devonestes",
"lpil",
"neenjaw",
"parkerl",
"sotojuan",
"waiting-for-dev"
],
"files": {
"solution": [
"lib/pascals_triangle.ex"
],
"test": [
"test/pascals_triangle_test.exs"
],
"example": [
".meta/example.ex"
]
},
"blurb": "Compute Pascal's triangle up to a given number of rows.",
"source": "Pascal's Triangle at Wolfram Math World",
"source_url": "https://www.wolframalpha.com/input/?i=Pascal%27s+triangle"
}

View File

@ -0,0 +1 @@
{"track":"elixir","exercise":"pascals-triangle","id":"71d888253d44412db03d365e545aa233","url":"https://exercism.org/tracks/elixir/exercises/pascals-triangle","handle":"negrienko","is_requester":true,"auto_approve":false}

View File

@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]

24
elixir/pascals-triangle/.gitignore vendored Normal file
View File

@ -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").
pascals_triangle-*.tar

View File

@ -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/<FILE>.exs:LINENUM` - runs only a single test, the test from `<FILE>.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/pascals_triangle.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.

View File

@ -0,0 +1,41 @@
# Pascal's Triangle
Welcome to Pascal's Triangle on Exercism's Elixir Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Compute Pascal's triangle up to a given number of rows.
In Pascal's Triangle each number is computed by adding the numbers to the right and left of the current position in the previous row.
```text
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
# ... etc
```
## Source
### Created by
- @dalexj
### Contributed to by
- @amencarini
- @angelikatyborska
- @Cohen-Carlisle
- @devonestes
- @lpil
- @neenjaw
- @parkerl
- @sotojuan
- @waiting-for-dev
### Based on
Pascal's Triangle at Wolfram Math World - https://www.wolframalpha.com/input/?i=Pascal%27s+triangle

View File

@ -0,0 +1,19 @@
defmodule PascalsTriangle do
@doc """
Calculates the rows of a pascal triangle
with the given height
"""
@spec rows(integer) :: [[integer]]
def rows(num) do
1..num
|> Enum.reduce([[]], fn _, triangle -> [next(hd(triangle)) | triangle] end)
|> Enum.take(num)
|> Enum.reverse
end
defp next(from, acc \\ [])
defp next([], _acc), do: [1]
defp next([1], _acc), do: [1, 1]
defp next([one, two | []], acc), do: [1 | Enum.reverse([1, one + two | acc])]
defp next([one, two | rest], acc), do: next([two | rest], [one + two | acc])
end

View File

@ -0,0 +1,28 @@
defmodule PascalsTriangle.MixProject do
use Mix.Project
def project do
[
app: :pascals_triangle,
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

View File

@ -0,0 +1,152 @@
defmodule PascalsTriangleTest do
use ExUnit.Case
test "one row" do
assert PascalsTriangle.rows(1) == [[1]]
end
test "two rows" do
assert PascalsTriangle.rows(2) == [[1], [1, 1]]
end
test "three rows" do
assert PascalsTriangle.rows(3) == [[1], [1, 1], [1, 2, 1]]
end
test "four rows" do
assert PascalsTriangle.rows(4) == [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1]]
end
test "five rows" do
assert PascalsTriangle.rows(5) == [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]
end
test "six rows" do
assert PascalsTriangle.rows(6) == [
[1],
[1, 1],
[1, 2, 1],
[1, 3, 3, 1],
[1, 4, 6, 4, 1],
[1, 5, 10, 10, 5, 1]
]
end
test "ten rows" do
assert PascalsTriangle.rows(10) == [
[1],
[1, 1],
[1, 2, 1],
[1, 3, 3, 1],
[1, 4, 6, 4, 1],
[1, 5, 10, 10, 5, 1],
[1, 6, 15, 20, 15, 6, 1],
[1, 7, 21, 35, 35, 21, 7, 1],
[1, 8, 28, 56, 70, 56, 28, 8, 1],
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]
]
end
test "20 rows" do
assert PascalsTriangle.rows(20) == [
[1],
[1, 1],
[1, 2, 1],
[1, 3, 3, 1],
[1, 4, 6, 4, 1],
[1, 5, 10, 10, 5, 1],
[1, 6, 15, 20, 15, 6, 1],
[1, 7, 21, 35, 35, 21, 7, 1],
[1, 8, 28, 56, 70, 56, 28, 8, 1],
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1],
[1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1],
[1, 11, 55, 165, 330, 462, 462, 330, 165, 55, 11, 1],
[1, 12, 66, 220, 495, 792, 924, 792, 495, 220, 66, 12, 1],
[1, 13, 78, 286, 715, 1287, 1716, 1716, 1287, 715, 286, 78, 13, 1],
[1, 14, 91, 364, 1001, 2002, 3003, 3432, 3003, 2002, 1001, 364, 91, 14, 1],
[1, 15, 105, 455, 1365, 3003, 5005, 6435, 6435, 5005, 3003, 1365, 455, 105, 15, 1],
[
1,
16,
120,
560,
1820,
4368,
8008,
11440,
12870,
11440,
8008,
4368,
1820,
560,
120,
16,
1
],
[
1,
17,
136,
680,
2380,
6188,
12376,
19448,
24310,
24310,
19448,
12376,
6188,
2380,
680,
136,
17,
1
],
[
1,
18,
153,
816,
3060,
8568,
18564,
31824,
43758,
48620,
43758,
31824,
18564,
8568,
3060,
816,
153,
18,
1
],
[
1,
19,
171,
969,
3876,
11628,
27132,
50388,
75582,
92378,
92378,
75582,
50388,
27132,
11628,
3876,
969,
171,
19,
1
]
]
end
end

View File

@ -0,0 +1,2 @@
ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)