From d46a9b8e9a9b1c26d0ced3da0c6ce4dde449b1aa Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Mon, 11 Mar 2024 11:30:10 -0400 Subject: [PATCH] two_fer --- elixir/two-fer/.exercism/config.json | 24 +++++++++ elixir/two-fer/.exercism/metadata.json | 1 + elixir/two-fer/.formatter.exs | 4 ++ elixir/two-fer/.gitignore | 24 +++++++++ elixir/two-fer/HELP.md | 75 ++++++++++++++++++++++++++ elixir/two-fer/README.md | 56 +++++++++++++++++++ elixir/two-fer/lib/two_fer.ex | 9 ++++ elixir/two-fer/mix.exs | 28 ++++++++++ elixir/two-fer/test/test_helper.exs | 2 + elixir/two-fer/test/two_fer_test.exs | 38 +++++++++++++ 10 files changed, 261 insertions(+) create mode 100644 elixir/two-fer/.exercism/config.json create mode 100644 elixir/two-fer/.exercism/metadata.json create mode 100644 elixir/two-fer/.formatter.exs create mode 100644 elixir/two-fer/.gitignore create mode 100644 elixir/two-fer/HELP.md create mode 100644 elixir/two-fer/README.md create mode 100644 elixir/two-fer/lib/two_fer.ex create mode 100644 elixir/two-fer/mix.exs create mode 100644 elixir/two-fer/test/test_helper.exs create mode 100644 elixir/two-fer/test/two_fer_test.exs diff --git a/elixir/two-fer/.exercism/config.json b/elixir/two-fer/.exercism/config.json new file mode 100644 index 0000000..79dadd2 --- /dev/null +++ b/elixir/two-fer/.exercism/config.json @@ -0,0 +1,24 @@ +{ + "authors": [ + "Bscruz19" + ], + "contributors": [ + "angelikatyborska", + "Cohen-Carlisle", + "devonestes", + "neenjaw" + ], + "files": { + "solution": [ + "lib/two_fer.ex" + ], + "test": [ + "test/two_fer_test.exs" + ], + "example": [ + ".meta/example.ex" + ] + }, + "blurb": "Create a sentence of the form \"One for X, one for me.\".", + "source_url": "https://github.com/exercism/problem-specifications/issues/757" +} diff --git a/elixir/two-fer/.exercism/metadata.json b/elixir/two-fer/.exercism/metadata.json new file mode 100644 index 0000000..1a4204f --- /dev/null +++ b/elixir/two-fer/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"two-fer","id":"b3c872e5a42f4fd29e819eb5d0fd6ea9","url":"https://exercism.org/tracks/elixir/exercises/two-fer","handle":"negrienko","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/elixir/two-fer/.formatter.exs b/elixir/two-fer/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/two-fer/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/two-fer/.gitignore b/elixir/two-fer/.gitignore new file mode 100644 index 0000000..927e570 --- /dev/null +++ b/elixir/two-fer/.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"). +two_fer-*.tar + diff --git a/elixir/two-fer/HELP.md b/elixir/two-fer/HELP.md new file mode 100644 index 0000000..ec96b0d --- /dev/null +++ b/elixir/two-fer/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/two_fer.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/two-fer/README.md b/elixir/two-fer/README.md new file mode 100644 index 0000000..50d2f5a --- /dev/null +++ b/elixir/two-fer/README.md @@ -0,0 +1,56 @@ +# Two Fer + +Welcome to Two Fer on Exercism's Elixir Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Introduction + +In some English accents, when you say "two for" quickly, it sounds like "two fer". +Two-for-one is a way of saying that if you buy one, you also get one for free. +So the phrase "two-fer" often implies a two-for-one offer. + +Imagine a bakery that has a holiday offer where you can buy two cookies for the price of one ("two-fer one!"). +You go for the offer and (very generously) decide to give the extra cookie to a friend. + +## Instructions + +Your task is to determine what you will say as you give away the extra cookie. + +If your friend likes cookies, and is named Do-yun, then you will say: + +```text +One for Do-yun, one for me. +``` + +If your friend doesn't like cookies, you give the cookie to the next person in line at the bakery. +Since you don't know their name, you will say _you_ instead. + +```text +One for you, one for me. +``` + +Here are some examples: + +| Name | Dialogue | +| :----- | :-------------------------- | +| Alice | One for Alice, one for me. | +| Bohdan | One for Bohdan, one for me. | +| | One for you, one for me. | +| Zaphod | One for Zaphod, one for me. | + +## Source + +### Created by + +- @Bscruz19 + +### Contributed to by + +- @angelikatyborska +- @Cohen-Carlisle +- @devonestes +- @neenjaw + +### Based on + +https://github.com/exercism/problem-specifications/issues/757 \ No newline at end of file diff --git a/elixir/two-fer/lib/two_fer.ex b/elixir/two-fer/lib/two_fer.ex new file mode 100644 index 0000000..393d6fd --- /dev/null +++ b/elixir/two-fer/lib/two_fer.ex @@ -0,0 +1,9 @@ +defmodule TwoFer do + @doc """ + Two-fer or 2-fer is short for two for one. One for you and one for me. + """ + @spec two_fer(String.t()) :: String.t() + def two_fer(name \\ "you") when is_binary(name) do + "One for #{name}, one for me." + end +end diff --git a/elixir/two-fer/mix.exs b/elixir/two-fer/mix.exs new file mode 100644 index 0000000..d34fb49 --- /dev/null +++ b/elixir/two-fer/mix.exs @@ -0,0 +1,28 @@ +defmodule TwoFer.MixProject do + use Mix.Project + + def project do + [ + app: :two_fer, + 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/two-fer/test/test_helper.exs b/elixir/two-fer/test/test_helper.exs new file mode 100644 index 0000000..35fc5bf --- /dev/null +++ b/elixir/two-fer/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true) diff --git a/elixir/two-fer/test/two_fer_test.exs b/elixir/two-fer/test/two_fer_test.exs new file mode 100644 index 0000000..2c590b2 --- /dev/null +++ b/elixir/two-fer/test/two_fer_test.exs @@ -0,0 +1,38 @@ +defmodule TwoFerTest do + use ExUnit.Case + + test "no name given" do + assert TwoFer.two_fer() == "One for you, one for me." + end + + # @tag :pending + test "a name given" do + assert TwoFer.two_fer("Alice") == "One for Alice, one for me." + end + + # @tag :pending + test "another name given" do + assert TwoFer.two_fer("Bob") == "One for Bob, one for me." + end + + # @tag :pending + test "when the argument is a number" do + assert_raise FunctionClauseError, fn -> + TwoFer.two_fer(10) + end + end + + # @tag :pending + test "when the argument is an atom" do + assert_raise FunctionClauseError, fn -> + TwoFer.two_fer(:bob) + end + end + + # @tag :pending + test "when the argument is a charlist" do + assert_raise FunctionClauseError, fn -> + refute TwoFer.two_fer(~c"Jon Snow") + end + end +end