From 15ab62c4f15ca4b82499c69ac0f36b64b0f6d4b4 Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Sat, 29 Jun 2024 15:58:33 -0400 Subject: [PATCH] sum_of_multiples --- elixir/sum-of-multiples/.exercism/config.json | 38 +++++++++ .../sum-of-multiples/.exercism/metadata.json | 1 + elixir/sum-of-multiples/.formatter.exs | 4 + elixir/sum-of-multiples/.gitignore | 24 ++++++ elixir/sum-of-multiples/HELP.md | 75 ++++++++++++++++++ elixir/sum-of-multiples/README.md | 69 +++++++++++++++++ .../sum-of-multiples/lib/sum_of_multiples.ex | 27 +++++++ elixir/sum-of-multiples/mix.exs | 28 +++++++ .../test/sum_of_multiples_test.exs | 77 +++++++++++++++++++ elixir/sum-of-multiples/test/test_helper.exs | 2 + 10 files changed, 345 insertions(+) create mode 100644 elixir/sum-of-multiples/.exercism/config.json create mode 100644 elixir/sum-of-multiples/.exercism/metadata.json create mode 100644 elixir/sum-of-multiples/.formatter.exs create mode 100644 elixir/sum-of-multiples/.gitignore create mode 100644 elixir/sum-of-multiples/HELP.md create mode 100644 elixir/sum-of-multiples/README.md create mode 100644 elixir/sum-of-multiples/lib/sum_of_multiples.ex create mode 100644 elixir/sum-of-multiples/mix.exs create mode 100644 elixir/sum-of-multiples/test/sum_of_multiples_test.exs create mode 100644 elixir/sum-of-multiples/test/test_helper.exs diff --git a/elixir/sum-of-multiples/.exercism/config.json b/elixir/sum-of-multiples/.exercism/config.json new file mode 100644 index 0000000..3606ba3 --- /dev/null +++ b/elixir/sum-of-multiples/.exercism/config.json @@ -0,0 +1,38 @@ +{ + "authors": [ + "petehuang" + ], + "contributors": [ + "andrewsardone", + "angelikatyborska", + "CoderDennis", + "Cohen-Carlisle", + "dalexj", + "devonestes", + "elasticdog", + "jinyeow", + "krishandley", + "kytrinyx", + "lpil", + "neenjaw", + "parkerl", + "rubysolo", + "sotojuan", + "Teapane", + "waiting-for-dev" + ], + "files": { + "solution": [ + "lib/sum_of_multiples.ex" + ], + "test": [ + "test/sum_of_multiples_test.exs" + ], + "example": [ + ".meta/example.ex" + ] + }, + "blurb": "Given a number, find the sum of all the multiples of particular numbers up to but not including that number.", + "source": "A variation on Problem 1 at Project Euler", + "source_url": "https://projecteuler.net/problem=1" +} diff --git a/elixir/sum-of-multiples/.exercism/metadata.json b/elixir/sum-of-multiples/.exercism/metadata.json new file mode 100644 index 0000000..8405015 --- /dev/null +++ b/elixir/sum-of-multiples/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"sum-of-multiples","id":"3fe0813a41a1451aac780f8d7b811319","url":"https://exercism.org/tracks/elixir/exercises/sum-of-multiples","handle":"negrienko","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/elixir/sum-of-multiples/.formatter.exs b/elixir/sum-of-multiples/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/sum-of-multiples/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/sum-of-multiples/.gitignore b/elixir/sum-of-multiples/.gitignore new file mode 100644 index 0000000..7c5a43d --- /dev/null +++ b/elixir/sum-of-multiples/.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"). +sum_of_multiples-*.tar + diff --git a/elixir/sum-of-multiples/HELP.md b/elixir/sum-of-multiples/HELP.md new file mode 100644 index 0000000..c1d97ac --- /dev/null +++ b/elixir/sum-of-multiples/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/sum_of_multiples.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/sum-of-multiples/README.md b/elixir/sum-of-multiples/README.md new file mode 100644 index 0000000..4590abe --- /dev/null +++ b/elixir/sum-of-multiples/README.md @@ -0,0 +1,69 @@ +# Sum of Multiples + +Welcome to Sum of Multiples on Exercism's Elixir Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Introduction + +You work for a company that makes an online, fantasy-survival game. + +When a player finishes a level, they are awarded energy points. +The amount of energy awarded depends on which magical items the player found while exploring that level. + +## Instructions + +Your task is to write the code that calculates the energy points that get awarded to players when they complete a level. + +The points awarded depend on two things: + +- The level (a number) that the player completed. +- The base value of each magical item collected by the player during that level. + +The energy points are awarded according to the following rules: + +1. For each magical item, take the base value and find all the multiples of that value that are less than the level number. +2. Combine the sets of numbers. +3. Remove any duplicates. +4. Calculate the sum of all the numbers that are left. + +Let's look at an example: + +**The player completed level 20 and found two magical items with base values of 3 and 5.** + +To calculate the energy points earned by the player, we need to find all the unique multiples of these base values that are less than level 20. + +- Multiples of 3 less than 20: `{3, 6, 9, 12, 15, 18}` +- Multiples of 5 less than 20: `{5, 10, 15}` +- Combine the sets and remove duplicates: `{3, 5, 6, 9, 10, 12, 15, 18}` +- Sum the unique multiples: `3 + 5 + 6 + 9 + 10 + 12 + 15 + 18 = 78` +- Therefore, the player earns **78** energy points for completing level 20 and finding the two magical items with base values of 3 and 5. + +## Source + +### Created by + +- @petehuang + +### Contributed to by + +- @andrewsardone +- @angelikatyborska +- @CoderDennis +- @Cohen-Carlisle +- @dalexj +- @devonestes +- @elasticdog +- @jinyeow +- @krishandley +- @kytrinyx +- @lpil +- @neenjaw +- @parkerl +- @rubysolo +- @sotojuan +- @Teapane +- @waiting-for-dev + +### Based on + +A variation on Problem 1 at Project Euler - https://projecteuler.net/problem=1 \ No newline at end of file diff --git a/elixir/sum-of-multiples/lib/sum_of_multiples.ex b/elixir/sum-of-multiples/lib/sum_of_multiples.ex new file mode 100644 index 0000000..9e6452c --- /dev/null +++ b/elixir/sum-of-multiples/lib/sum_of_multiples.ex @@ -0,0 +1,27 @@ +defmodule SumOfMultiples do + @doc """ + Adds up all numbers from 1 to a given end number that are multiples of the factors provided. + """ + @spec to(non_neg_integer, [non_neg_integer]) :: non_neg_integer + def to(limit, factors) do + for x <- 1..(limit - 1), + Enum.any?(factors, &(&1 != 0 && rem(x, &1) == 0 )), + reduce: 0 + do + acc -> + IO.inspect(acc) + x + acc + end + end + + # def to(limit, factors) do + # factors + # |> Enum.flat_map(&multipliers(&1, limit)) + # |> Enum.uniq() + # |> Enum.sum() + # end + + # defp multipliers(from, to) when from <= to and rem(to, from) == 0, do: Enum.take_every(from..(to - 1), from) + # defp multipliers(from, to) when from <= to, do: Enum.take_every(from..to, from) + # defp multipliers(_from, _to), do: [] +end diff --git a/elixir/sum-of-multiples/mix.exs b/elixir/sum-of-multiples/mix.exs new file mode 100644 index 0000000..364c1a8 --- /dev/null +++ b/elixir/sum-of-multiples/mix.exs @@ -0,0 +1,28 @@ +defmodule SumOfMultiples.MixProject do + use Mix.Project + + def project do + [ + app: :sum_of_multiples, + 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/sum-of-multiples/test/sum_of_multiples_test.exs b/elixir/sum-of-multiples/test/sum_of_multiples_test.exs new file mode 100644 index 0000000..6053904 --- /dev/null +++ b/elixir/sum-of-multiples/test/sum_of_multiples_test.exs @@ -0,0 +1,77 @@ +defmodule SumOfMultiplesTest do + use ExUnit.Case + + test "no multiples within limit" do + assert SumOfMultiples.to(1, [3, 5]) == 0 + end + + test "one factor has multiples within limit" do + assert SumOfMultiples.to(4, [3, 5]) == 3 + end + + test "more than one multiple within limit0" do + assert SumOfMultiples.to(7, [3]) == 9 + end + + test "more than one factor with multiples within limit" do + assert SumOfMultiples.to(10, [3, 5]) == 23 + end + + test "each multiple is only counted once" do + assert SumOfMultiples.to(100, [3, 5]) == 2318 + end + + test "a much larger limit" do + assert SumOfMultiples.to(1000, [3, 5]) == 233_168 + end + + test "three factors" do + multiples = [7, 13, 17] + assert SumOfMultiples.to(20, multiples) == 51 + end + + test "factors not relatively prime" do + multiples = [4, 6] + assert SumOfMultiples.to(15, multiples) == 30 + end + + test "some pairs of factors relatively prime and some not" do + multiples = [5, 6, 8] + assert SumOfMultiples.to(150, multiples) == 4419 + end + + test "one factor is a multiple of another" do + multiples = [5, 25] + assert SumOfMultiples.to(51, multiples) == 275 + end + + test "much larger factors" do + multiples = [43, 47] + assert SumOfMultiples.to(10000, multiples) == 2_203_160 + end + + test "all numbers are multiples of 1" do + multiples = [1] + assert SumOfMultiples.to(100, multiples) == 4950 + end + + test "no factors means an empty sum" do + multiples = [] + assert SumOfMultiples.to(10000, multiples) == 0 + end + + test "the only multiple of 0 is 0" do + multiples = [0] + assert SumOfMultiples.to(1, multiples) == 0 + end + + test "the factor 0 does not affect the sum of multiples of other factors" do + multiples = [3, 0] + assert SumOfMultiples.to(4, multiples) == 3 + end + + test "solutions using include-exclude must extend to cardinality greater than 3" do + multiples = [2, 3, 5, 7, 11] + assert SumOfMultiples.to(10000, multiples) == 39_614_537 + end +end diff --git a/elixir/sum-of-multiples/test/test_helper.exs b/elixir/sum-of-multiples/test/test_helper.exs new file mode 100644 index 0000000..35fc5bf --- /dev/null +++ b/elixir/sum-of-multiples/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true)