This commit is contained in:
Danil Negrienko 2024-07-03 01:39:58 -04:00
parent 643a41d53c
commit 2eb14fcd40
10 changed files with 288 additions and 0 deletions

View File

@ -0,0 +1,39 @@
{
"authors": [
"rubysolo"
],
"contributors": [
"andrewsardone",
"angelikatyborska",
"bunnymatic",
"Cohen-Carlisle",
"dalexj",
"devonestes",
"elasticdog",
"henrik",
"herminiotorres",
"jinyeow",
"lpil",
"neenjaw",
"parkerl",
"petehuang",
"pminten",
"sotojuan",
"Teapane",
"waiting-for-dev"
],
"files": {
"solution": [
"lib/grains.ex"
],
"test": [
"test/grains_test.exs"
],
"example": [
".meta/example.ex"
]
},
"blurb": "Calculate the number of grains of wheat on a chessboard given that the number on each square doubles.",
"source": "The CodeRanch Cattle Drive, Assignment 6",
"source_url": "https://coderanch.com/wiki/718824/Grains"
}

View File

@ -0,0 +1 @@
{"track":"elixir","exercise":"grains","id":"cfa89389b43c46c79214bc6e2ad0038c","url":"https://exercism.org/tracks/elixir/exercises/grains","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/grains/.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").
grains-*.tar

75
elixir/grains/HELP.md Normal file
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/grains.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.

51
elixir/grains/README.md Normal file
View File

@ -0,0 +1,51 @@
# Grains
Welcome to Grains on Exercism's Elixir Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Calculate the number of grains of wheat on a chessboard given that the number on each square doubles.
There once was a wise servant who saved the life of a prince.
The king promised to pay whatever the servant could dream up.
Knowing that the king loved chess, the servant told the king he would like to have grains of wheat.
One grain on the first square of a chess board, with the number of grains doubling on each successive square.
There are 64 squares on a chessboard (where square 1 has one grain, square 2 has two grains, and so on).
Write code that shows:
- how many grains were on a given square, and
- the total number of grains on the chessboard
## Source
### Created by
- @rubysolo
### Contributed to by
- @andrewsardone
- @angelikatyborska
- @bunnymatic
- @Cohen-Carlisle
- @dalexj
- @devonestes
- @elasticdog
- @henrik
- @herminiotorres
- @jinyeow
- @lpil
- @neenjaw
- @parkerl
- @petehuang
- @pminten
- @sotojuan
- @Teapane
- @waiting-for-dev
### Based on
The CodeRanch Cattle Drive, Assignment 6 - https://coderanch.com/wiki/718824/Grains

View File

@ -0,0 +1,14 @@
defmodule Grains do
@doc """
Calculate two to the power of the input minus one.
"""
@spec square(pos_integer()) :: {:ok, pos_integer()} | {:error, String.t()}
def square(number) when number > 64 or number < 1, do: {:error, "The requested square must be between 1 and 64 (inclusive)"}
def square(number), do: {:ok, 2 ** (number - 1)}
@doc """
Adds square of each number from 1 to 64.
"""
@spec total :: {:ok, pos_integer()}
def total, do: {:ok, 2 ** 64 - 1}
end

28
elixir/grains/mix.exs Normal file
View File

@ -0,0 +1,28 @@
defmodule Grains.MixProject do
use Mix.Project
def project do
[
app: :grains,
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,50 @@
defmodule GrainsTest do
use ExUnit.Case
test "square 1" do
assert Grains.square(1) === {:ok, 1}
end
test "square 2" do
assert Grains.square(2) === {:ok, 2}
end
test "square 3" do
assert Grains.square(3) === {:ok, 4}
end
test "square 4" do
assert Grains.square(4) === {:ok, 8}
end
test "square 16" do
assert Grains.square(16) === {:ok, 32768}
end
test "square 32" do
assert Grains.square(32) === {:ok, 2_147_483_648}
end
test "square 64" do
assert Grains.square(64) === {:ok, 9_223_372_036_854_775_808}
end
test "total grains" do
assert Grains.total() === {:ok, 18_446_744_073_709_551_615}
end
test "square greater than 64 returns an error" do
assert Grains.square(65) ===
{:error, "The requested square must be between 1 and 64 (inclusive)"}
end
test "negative square returns an error" do
assert Grains.square(-1) ===
{:error, "The requested square must be between 1 and 64 (inclusive)"}
end
test "square 0 returns an error" do
assert Grains.square(0) ===
{:error, "The requested square must be between 1 and 64 (inclusive)"}
end
end

View File

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