From 7332230435dbf1a1fa1d85b3af6f008b84661001 Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Mon, 18 Dec 2023 15:02:33 -0500 Subject: [PATCH] name-badge --- elixir/name-badge/.exercism/config.json | 25 +++++ elixir/name-badge/.exercism/metadata.json | 1 + elixir/name-badge/.formatter.exs | 4 + elixir/name-badge/.gitignore | 24 +++++ elixir/name-badge/HELP.md | 75 +++++++++++++++ elixir/name-badge/HINTS.md | 25 +++++ elixir/name-badge/README.md | 104 +++++++++++++++++++++ elixir/name-badge/lib/name_badge.ex | 10 ++ elixir/name-badge/mix.exs | 28 ++++++ elixir/name-badge/test/name_badge_test.exs | 33 +++++++ elixir/name-badge/test/test_helper.exs | 2 + 11 files changed, 331 insertions(+) create mode 100644 elixir/name-badge/.exercism/config.json create mode 100644 elixir/name-badge/.exercism/metadata.json create mode 100644 elixir/name-badge/.formatter.exs create mode 100644 elixir/name-badge/.gitignore create mode 100644 elixir/name-badge/HELP.md create mode 100644 elixir/name-badge/HINTS.md create mode 100644 elixir/name-badge/README.md create mode 100644 elixir/name-badge/lib/name_badge.ex create mode 100644 elixir/name-badge/mix.exs create mode 100644 elixir/name-badge/test/name_badge_test.exs create mode 100644 elixir/name-badge/test/test_helper.exs diff --git a/elixir/name-badge/.exercism/config.json b/elixir/name-badge/.exercism/config.json new file mode 100644 index 0000000..d4f1cbe --- /dev/null +++ b/elixir/name-badge/.exercism/config.json @@ -0,0 +1,25 @@ +{ + "authors": [ + "angelikatyborska" + ], + "contributors": [ + "neenjaw" + ], + "files": { + "solution": [ + "lib/name_badge.ex" + ], + "test": [ + "test/name_badge_test.exs" + ], + "exemplar": [ + ".meta/exemplar.ex" + ] + }, + "language_versions": ">=1.10", + "forked_from": [ + "csharp/tim-from-marketing" + ], + "icon": "tim-from-marketing", + "blurb": "Learn about nil and the if conditional expression by printing name badges for factory employees." +} diff --git a/elixir/name-badge/.exercism/metadata.json b/elixir/name-badge/.exercism/metadata.json new file mode 100644 index 0000000..c463b72 --- /dev/null +++ b/elixir/name-badge/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"name-badge","id":"c0cda396046043c99b8b51ee9773dc08","url":"https://exercism.org/tracks/elixir/exercises/name-badge","handle":"negrienko","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/elixir/name-badge/.formatter.exs b/elixir/name-badge/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/name-badge/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/name-badge/.gitignore b/elixir/name-badge/.gitignore new file mode 100644 index 0000000..cb54963 --- /dev/null +++ b/elixir/name-badge/.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"). +nil-*.tar + diff --git a/elixir/name-badge/HELP.md b/elixir/name-badge/HELP.md new file mode 100644 index 0000000..a5d0db1 --- /dev/null +++ b/elixir/name-badge/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/name_badge.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/name-badge/HINTS.md b/elixir/name-badge/HINTS.md new file mode 100644 index 0000000..19f264b --- /dev/null +++ b/elixir/name-badge/HINTS.md @@ -0,0 +1,25 @@ +# Hints + +## General + +- Read about `if` in the official [Getting Started guide][getting-started-if-unless] or on [elixirschool.com][elixirschool-if-unless]. + +## 1. Print a badge for an employee + +- This is a base case where we can assume that none of the input data is `nil`. +- If you need to refresh your memory about working with strings, read about them in the official [Getting Started guide][getting-started-basic-strings]. + +## 2. Print a badge for a new employee + +- Strings are always _truthy_ and `nil` is _falsy_. +- There is a [macro][kernel-if] that returns one of the two options, depending of whether it was given a _truthy_ or a _falsy_ value. + +## 3. Print a badge for the owner + +- Strings are always _truthy_ and `nil` is _falsy_. +- There is a [macro][kernel-if] that returns one of the two options, depending of whether it was given a _truthy_ or a _falsy_ value. + +[kernel-if]: https://hexdocs.pm/elixir/Kernel.html#if/2 +[getting-started-basic-strings]: https://elixir-lang.org/getting-started/basic-types.html#strings +[getting-started-if-unless]: https://elixir-lang.org/getting-started/case-cond-and-if.html#if-and-unless +[elixir-school-if-unless]: https://elixirschool.com/en/lessons/basics/control-structures/#if-and-unless \ No newline at end of file diff --git a/elixir/name-badge/README.md b/elixir/name-badge/README.md new file mode 100644 index 0000000..f3c6588 --- /dev/null +++ b/elixir/name-badge/README.md @@ -0,0 +1,104 @@ +# Name Badge + +Welcome to Name Badge on Exercism's Elixir Track. +If you need help running the tests or submitting your code, check out `HELP.md`. +If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :) + +## Introduction + +## Nil + +[Nil][nil-dictionary] is an English word meaning "nothing" or "zero". In Elixir, `nil` is a special value that means an _absence_ of a value. + +```elixir +# I do not have a favorite color +favorite_color = nil +``` + +In other programming languages, `null` or `none` values might play a similar role. + +## If + +Besides `cond`, Elixir also provides the macro [`if/2`][getting-started-if-unless] which is useful when you need to check for only one condition. + +[`if/2`][kernel-if] accepts a condition and two options. It returns the first option if the condition is _truthy_, and the second option if the condition is _falsy_. + +```elixir +age = 15 + +if age >= 16 do + "You are allowed to drink beer in Germany." +else + "No beer for you!" +end + +# => "No beer for you!" +``` + +It is also possible to write an `if` expression on a single line. Note the comma after the condition. + +```elixir +if age > 16, do: "beer", else: "no beer" +``` + +This syntax is helpful for very short expressions, but should be avoided if the expression won't fit on a single line. + +### _Truthy_ and _falsy_ + +In Elixir, all datatypes evaluate to a _truthy_ or _falsy_ value when they are encountered in a boolean context (like an `if` expression). All data is considered _truthy_ **except** for `false` and `nil`. In particular, empty strings, the integer `0`, and empty lists are all considered _truthy_ in Elixir. + +[nil-dictionary]: https://www.merriam-webster.com/dictionary/nil +[getting-started-if-unless]: https://elixir-lang.org/getting-started/case-cond-and-if.html#if-and-unless +[kernel-if]: https://hexdocs.pm/elixir/Kernel.html#if/2 + +## Instructions + +In this exercise you'll be writing code to print name badges for factory employees. Employees have an ID, name, and department name. Employee badge labels are formatted as follows: `"[id] - name - DEPARTMENT"`. + +## 1. Print a badge for an employee + +Implement the `NameBadge.print/3` function. It should take an id, name, and a department. It should return the badge label, with the department name in uppercase. + +```elixir +NameBadge.print(67, "Katherine Williams", "Strategic Communication") +# => "[67] - Katherine Williams - STRATEGIC COMMUNICATION" +``` + +## 2. Print a badge for a new employee + +Due to a quirk in the computer system, new employees occasionally don't yet have an ID when they start working at the factory. As badges are required, they will receive a temporary badge without the ID prefix. + +Extend the `NameBadge.print/3` function. When the id is missing, it should print a badge without it. + +```elixir +NameBadge.print(nil, "Robert Johnson", "Procurement") +# => "Robert Johnson - PROCUREMENT" +``` + +## 3. Print a badge for the owner + +Even the factory's owner has to wear a badge at all times. However, an owner does not have a department. In this case, the label should print `"OWNER"` instead of the department name. + +Extend the `NameBadge.print/3` function. When the department is missing, assume the badge belongs to the company owner. + +```elixir +NameBadge.print(204, "Rachel Miller", nil) +# => "[204] - Rachel Miller - OWNER" +``` + +Note that it is possible for the owner to also be a new employee. + +```elixir +NameBadge.print(nil, "Rachel Miller", nil) +# => "Rachel Miller - OWNER" +``` + +## Source + +### Created by + +- @angelikatyborska + +### Contributed to by + +- @neenjaw \ No newline at end of file diff --git a/elixir/name-badge/lib/name_badge.ex b/elixir/name-badge/lib/name_badge.ex new file mode 100644 index 0000000..db204e3 --- /dev/null +++ b/elixir/name-badge/lib/name_badge.ex @@ -0,0 +1,10 @@ +defmodule NameBadge do + def print(id, name, department) do + d = if department, do: String.upcase(department), else: "OWNER" + if id do + "[#{id}] - #{name} - #{d}" + else + "#{name} - #{d}" + end + end +end diff --git a/elixir/name-badge/mix.exs b/elixir/name-badge/mix.exs new file mode 100644 index 0000000..2709c17 --- /dev/null +++ b/elixir/name-badge/mix.exs @@ -0,0 +1,28 @@ +defmodule NameBadge.MixProject do + use Mix.Project + + def project do + [ + app: :name_badge, + version: "0.1.0", + # elixir: "~> 1.10", + 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/name-badge/test/name_badge_test.exs b/elixir/name-badge/test/name_badge_test.exs new file mode 100644 index 0000000..3201483 --- /dev/null +++ b/elixir/name-badge/test/name_badge_test.exs @@ -0,0 +1,33 @@ +defmodule NameBadgeTest do + use ExUnit.Case + doctest NameBadge + + describe "print/3" do + @tag task_id: 1 + test "prints the employee badge with full data" do + assert NameBadge.print(455, "Mary M. Brown", "MARKETING") == + "[455] - Mary M. Brown - MARKETING" + end + + @tag task_id: 1 + test "uppercases the department" do + assert NameBadge.print(89, "Jack McGregor", "Procurement") == + "[89] - Jack McGregor - PROCUREMENT" + end + + @tag task_id: 2 + test "prints the employee badge without id" do + assert NameBadge.print(nil, "Barbara White", "Security") == "Barbara White - SECURITY" + end + + @tag task_id: 3 + test "prints the owner badge" do + assert NameBadge.print(1, "Anna Johnson", nil) == "[1] - Anna Johnson - OWNER" + end + + @tag task_id: 3 + test "prints the owner badge without id" do + assert NameBadge.print(nil, "Stephen Dann", nil) == "Stephen Dann - OWNER" + end + end +end diff --git a/elixir/name-badge/test/test_helper.exs b/elixir/name-badge/test/test_helper.exs new file mode 100644 index 0000000..e8677a3 --- /dev/null +++ b/elixir/name-badge/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true, seed: 0)