raindrops

This commit is contained in:
Danil Negrienko 2024-06-27 17:22:40 -04:00
parent b5a8d35513
commit 146d1d3f89
10 changed files with 326 additions and 0 deletions

View File

@ -0,0 +1,33 @@
{
"authors": [
"rubysolo"
],
"contributors": [
"andrewsardone",
"angelikatyborska",
"Cohen-Carlisle",
"dalexj",
"devonestes",
"jinyeow",
"lpil",
"neenjaw",
"parkerl",
"sotojuan",
"Teapane",
"waiting-for-dev"
],
"files": {
"solution": [
"lib/raindrops.ex"
],
"test": [
"test/raindrops_test.exs"
],
"example": [
".meta/example.ex"
]
},
"blurb": "Convert a number into its corresponding raindrop sounds - Pling, Plang and Plong.",
"source": "A variation on FizzBuzz, a famous technical interview question that is intended to weed out potential candidates. That question is itself derived from Fizz Buzz, a popular children's game for teaching division.",
"source_url": "https://en.wikipedia.org/wiki/Fizz_buzz"
}

View File

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

75
elixir/raindrops/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/raindrops.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,58 @@
# Raindrops
Welcome to Raindrops on Exercism's Elixir Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Introduction
Raindrops is a slightly more complex version of the FizzBuzz challenge, a classic interview question.
## Instructions
Your task is to convert a number into its corresponding raindrop sounds.
If a given number:
- is divisible by 3, add "Pling" to the result.
- is divisible by 5, add "Plang" to the result.
- is divisible by 7, add "Plong" to the result.
- **is not** divisible by 3, 5, or 7, the result should be the number as a string.
## Examples
- 28 is divisible by 7, but not 3 or 5, so the result would be `"Plong"`.
- 30 is divisible by 3 and 5, but not 7, so the result would be `"PlingPlang"`.
- 34 is not divisible by 3, 5, or 7, so the result would be `"34"`.
~~~~exercism/note
A common way to test if one number is evenly divisible by another is to compare the [remainder][remainder] or [modulus][modulo] to zero.
Most languages provide operators or functions for one (or both) of these.
[remainder]: https://exercism.org/docs/programming/operators/remainder
[modulo]: https://en.wikipedia.org/wiki/Modulo_operation
~~~~
## Source
### Created by
- @rubysolo
### Contributed to by
- @andrewsardone
- @angelikatyborska
- @Cohen-Carlisle
- @dalexj
- @devonestes
- @jinyeow
- @lpil
- @neenjaw
- @parkerl
- @sotojuan
- @Teapane
- @waiting-for-dev
### Based on
A variation on FizzBuzz, a famous technical interview question that is intended to weed out potential candidates. That question is itself derived from Fizz Buzz, a popular children's game for teaching division. - https://en.wikipedia.org/wiki/Fizz_buzz

View File

@ -0,0 +1,30 @@
defmodule Raindrops do
@doc """
Returns a string based on raindrop factors.
- If the number contains 3 as a prime factor, output 'Pling'.
- If the number contains 5 as a prime factor, output 'Plang'.
- If the number contains 7 as a prime factor, output 'Plong'.
- If the number does not contain 3, 5, or 7 as a prime factor,
just pass the number's digits straight through.
"""
@results [
{3, "Pling"},
{5, "Plang"},
{7, "Plong"}
]
@spec convert(pos_integer) :: String.t()
def convert(number) do
@results
|> Enum.map_join(&mapper(number, &1))
|> case do
"" -> Integer.to_string(number)
result -> result
end
end
defp mapper(number, {factor, value}) do
if rem(number, factor) == 0, do: value, else: ""
end
end

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

@ -0,0 +1,28 @@
defmodule Raindrops.MixProject do
use Mix.Project
def project do
[
app: :raindrops,
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,71 @@
defmodule RaindropsTest do
use ExUnit.Case
test "1" do
assert Raindrops.convert(1) == "1"
end
test "3" do
assert Raindrops.convert(3) == "Pling"
end
test "5" do
assert Raindrops.convert(5) == "Plang"
end
test "7" do
assert Raindrops.convert(7) == "Plong"
end
test "6" do
assert Raindrops.convert(6) == "Pling"
end
test "8" do
assert Raindrops.convert(8) == "8"
end
test "9" do
assert Raindrops.convert(9) == "Pling"
end
test "10" do
assert Raindrops.convert(10) == "Plang"
end
test "14" do
assert Raindrops.convert(14) == "Plong"
end
test "15" do
assert Raindrops.convert(15) == "PlingPlang"
end
test "21" do
assert Raindrops.convert(21) == "PlingPlong"
end
test "25" do
assert Raindrops.convert(25) == "Plang"
end
test "35" do
assert Raindrops.convert(35) == "PlangPlong"
end
test "49" do
assert Raindrops.convert(49) == "Plong"
end
test "52" do
assert Raindrops.convert(52) == "52"
end
test "105" do
assert Raindrops.convert(105) == "PlingPlangPlong"
end
test "12121" do
assert Raindrops.convert(12121) == "12121"
end
end

View File

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