From 4c3da6abe4e3a251b454b7273f59e3e397bb0eaa Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Wed, 26 Jun 2024 22:42:04 -0400 Subject: [PATCH] armstrong_number --- .../armstrong-numbers/.exercism/config.json | 27 +++++++ .../armstrong-numbers/.exercism/metadata.json | 1 + elixir/armstrong-numbers/.formatter.exs | 4 + elixir/armstrong-numbers/.gitignore | 24 ++++++ elixir/armstrong-numbers/HELP.md | 75 +++++++++++++++++++ elixir/armstrong-numbers/README.md | 38 ++++++++++ .../armstrong-numbers/lib/armstrong_number.ex | 13 ++++ elixir/armstrong-numbers/mix.exs | 28 +++++++ .../test/armstrong_number_test.exs | 47 ++++++++++++ elixir/armstrong-numbers/test/test_helper.exs | 2 + 10 files changed, 259 insertions(+) create mode 100644 elixir/armstrong-numbers/.exercism/config.json create mode 100644 elixir/armstrong-numbers/.exercism/metadata.json create mode 100644 elixir/armstrong-numbers/.formatter.exs create mode 100644 elixir/armstrong-numbers/.gitignore create mode 100644 elixir/armstrong-numbers/HELP.md create mode 100644 elixir/armstrong-numbers/README.md create mode 100644 elixir/armstrong-numbers/lib/armstrong_number.ex create mode 100644 elixir/armstrong-numbers/mix.exs create mode 100644 elixir/armstrong-numbers/test/armstrong_number_test.exs create mode 100644 elixir/armstrong-numbers/test/test_helper.exs diff --git a/elixir/armstrong-numbers/.exercism/config.json b/elixir/armstrong-numbers/.exercism/config.json new file mode 100644 index 0000000..c767833 --- /dev/null +++ b/elixir/armstrong-numbers/.exercism/config.json @@ -0,0 +1,27 @@ +{ + "authors": [ + "Bscruz19" + ], + "contributors": [ + "angelikatyborska", + "Br1ght0ne", + "Cohen-Carlisle", + "devonestes", + "neenjaw", + "tsuka" + ], + "files": { + "solution": [ + "lib/armstrong_number.ex" + ], + "test": [ + "test/armstrong_number_test.exs" + ], + "example": [ + ".meta/example.ex" + ] + }, + "blurb": "Determine if a number is an Armstrong number.", + "source": "Wikipedia", + "source_url": "https://en.wikipedia.org/wiki/Narcissistic_number" +} diff --git a/elixir/armstrong-numbers/.exercism/metadata.json b/elixir/armstrong-numbers/.exercism/metadata.json new file mode 100644 index 0000000..c01e1bc --- /dev/null +++ b/elixir/armstrong-numbers/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"armstrong-numbers","id":"87b5aa5429664271a572c9f3ac0722d1","url":"https://exercism.org/tracks/elixir/exercises/armstrong-numbers","handle":"negrienko","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/elixir/armstrong-numbers/.formatter.exs b/elixir/armstrong-numbers/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/armstrong-numbers/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/armstrong-numbers/.gitignore b/elixir/armstrong-numbers/.gitignore new file mode 100644 index 0000000..4d235be --- /dev/null +++ b/elixir/armstrong-numbers/.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"). +armstrong_numbers-*.tar + diff --git a/elixir/armstrong-numbers/HELP.md b/elixir/armstrong-numbers/HELP.md new file mode 100644 index 0000000..92542b2 --- /dev/null +++ b/elixir/armstrong-numbers/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/armstrong_number.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/armstrong-numbers/README.md b/elixir/armstrong-numbers/README.md new file mode 100644 index 0000000..bef2427 --- /dev/null +++ b/elixir/armstrong-numbers/README.md @@ -0,0 +1,38 @@ +# Armstrong Numbers + +Welcome to Armstrong Numbers on Exercism's Elixir Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +An [Armstrong number][armstrong-number] is a number that is the sum of its own digits each raised to the power of the number of digits. + +For example: + +- 9 is an Armstrong number, because `9 = 9^1 = 9` +- 10 is _not_ an Armstrong number, because `10 != 1^2 + 0^2 = 1` +- 153 is an Armstrong number, because: `153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153` +- 154 is _not_ an Armstrong number, because: `154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190` + +Write some code to determine whether a number is an Armstrong number. + +[armstrong-number]: https://en.wikipedia.org/wiki/Narcissistic_number + +## Source + +### Created by + +- @Bscruz19 + +### Contributed to by + +- @angelikatyborska +- @Br1ght0ne +- @Cohen-Carlisle +- @devonestes +- @neenjaw +- @tsuka + +### Based on + +Wikipedia - https://en.wikipedia.org/wiki/Narcissistic_number \ No newline at end of file diff --git a/elixir/armstrong-numbers/lib/armstrong_number.ex b/elixir/armstrong-numbers/lib/armstrong_number.ex new file mode 100644 index 0000000..b18c620 --- /dev/null +++ b/elixir/armstrong-numbers/lib/armstrong_number.ex @@ -0,0 +1,13 @@ +defmodule ArmstrongNumber do + @moduledoc """ + Provides a way to validate whether or not a number is an Armstrong number + """ + + @spec valid?(integer) :: boolean + def valid?(number) do + digits = Integer.digits(number) + power = length(digits) + + Enum.reduce(digits, 0, &(&1 ** power + &2)) == number + end +end diff --git a/elixir/armstrong-numbers/mix.exs b/elixir/armstrong-numbers/mix.exs new file mode 100644 index 0000000..ed4f817 --- /dev/null +++ b/elixir/armstrong-numbers/mix.exs @@ -0,0 +1,28 @@ +defmodule ArmstrongNumber.MixProject do + use Mix.Project + + def project do + [ + app: :armstrong_number, + 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/armstrong-numbers/test/armstrong_number_test.exs b/elixir/armstrong-numbers/test/armstrong_number_test.exs new file mode 100644 index 0000000..5b2955f --- /dev/null +++ b/elixir/armstrong-numbers/test/armstrong_number_test.exs @@ -0,0 +1,47 @@ +defmodule ArmstrongNumberTest do + use ExUnit.Case + + test "Zero is an Armstrong number" do + assert ArmstrongNumber.valid?(0) + end + + test "Single digit numbers are Armstrong numbers" do + assert ArmstrongNumber.valid?(5) + end + + test "There are no two-digit Armstrong Numbers" do + refute ArmstrongNumber.valid?(10) + end + + test "Three-digit number that is an Armstrong number" do + assert ArmstrongNumber.valid?(153) + end + + test "Three-digit number that is not an Armstrong number" do + refute ArmstrongNumber.valid?(100) + end + + test "Four-digit number that is an Armstrong number" do + assert ArmstrongNumber.valid?(9474) + end + + test "Four-digit number that is not an Armstrong number" do + refute ArmstrongNumber.valid?(9475) + end + + test "Seven-digit number that is an Armstrong number" do + assert ArmstrongNumber.valid?(9_926_315) + end + + test "Seven-digit number that is not an Armstrong number" do + refute ArmstrongNumber.valid?(9_926_134) + end + + test "Armstrong number containing seven zeroes" do + assert ArmstrongNumber.valid?(186_709_961_001_538_790_100_634_132_976_990) + end + + test "The largest and last Armstrong number" do + assert ArmstrongNumber.valid?(115_132_219_018_763_992_565_095_597_973_971_522_401) + end +end diff --git a/elixir/armstrong-numbers/test/test_helper.exs b/elixir/armstrong-numbers/test/test_helper.exs new file mode 100644 index 0000000..35fc5bf --- /dev/null +++ b/elixir/armstrong-numbers/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true)