From 2eb14fcd40e04009f10b3d007119733901ce6476 Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Wed, 3 Jul 2024 01:39:58 -0400 Subject: [PATCH] grains --- elixir/grains/.exercism/config.json | 39 ++++++++++++++ elixir/grains/.exercism/metadata.json | 1 + elixir/grains/.formatter.exs | 4 ++ elixir/grains/.gitignore | 24 +++++++++ elixir/grains/HELP.md | 75 +++++++++++++++++++++++++++ elixir/grains/README.md | 51 ++++++++++++++++++ elixir/grains/lib/grains.ex | 14 +++++ elixir/grains/mix.exs | 28 ++++++++++ elixir/grains/test/grains_test.exs | 50 ++++++++++++++++++ elixir/grains/test/test_helper.exs | 2 + 10 files changed, 288 insertions(+) create mode 100644 elixir/grains/.exercism/config.json create mode 100644 elixir/grains/.exercism/metadata.json create mode 100644 elixir/grains/.formatter.exs create mode 100644 elixir/grains/.gitignore create mode 100644 elixir/grains/HELP.md create mode 100644 elixir/grains/README.md create mode 100644 elixir/grains/lib/grains.ex create mode 100644 elixir/grains/mix.exs create mode 100644 elixir/grains/test/grains_test.exs create mode 100644 elixir/grains/test/test_helper.exs diff --git a/elixir/grains/.exercism/config.json b/elixir/grains/.exercism/config.json new file mode 100644 index 0000000..33e4bcc --- /dev/null +++ b/elixir/grains/.exercism/config.json @@ -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" +} diff --git a/elixir/grains/.exercism/metadata.json b/elixir/grains/.exercism/metadata.json new file mode 100644 index 0000000..b14a6ea --- /dev/null +++ b/elixir/grains/.exercism/metadata.json @@ -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} \ No newline at end of file diff --git a/elixir/grains/.formatter.exs b/elixir/grains/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/grains/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/grains/.gitignore b/elixir/grains/.gitignore new file mode 100644 index 0000000..0be0524 --- /dev/null +++ b/elixir/grains/.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"). +grains-*.tar + diff --git a/elixir/grains/HELP.md b/elixir/grains/HELP.md new file mode 100644 index 0000000..cb1a54c --- /dev/null +++ b/elixir/grains/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/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. \ No newline at end of file diff --git a/elixir/grains/README.md b/elixir/grains/README.md new file mode 100644 index 0000000..3b1fd1a --- /dev/null +++ b/elixir/grains/README.md @@ -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 \ No newline at end of file diff --git a/elixir/grains/lib/grains.ex b/elixir/grains/lib/grains.ex new file mode 100644 index 0000000..d26ce7b --- /dev/null +++ b/elixir/grains/lib/grains.ex @@ -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 diff --git a/elixir/grains/mix.exs b/elixir/grains/mix.exs new file mode 100644 index 0000000..d6eb7b0 --- /dev/null +++ b/elixir/grains/mix.exs @@ -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 diff --git a/elixir/grains/test/grains_test.exs b/elixir/grains/test/grains_test.exs new file mode 100644 index 0000000..8a14909 --- /dev/null +++ b/elixir/grains/test/grains_test.exs @@ -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 diff --git a/elixir/grains/test/test_helper.exs b/elixir/grains/test/test_helper.exs new file mode 100644 index 0000000..35fc5bf --- /dev/null +++ b/elixir/grains/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true)