mirror of
https://github.com/rjNemo/exercism-elixir
synced 2026-06-06 02:16:48 +00:00
bob
This commit is contained in:
parent
0ab1fcaa3e
commit
ddd3b8ed39
21 changed files with 705 additions and 0 deletions
46
bob/.exercism/config.json
Normal file
46
bob/.exercism/config.json
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
{
|
||||
"blurb": "Bob is a lackadaisical teenager. In conversation, his responses are very limited.",
|
||||
"authors": [
|
||||
"rubysolo"
|
||||
],
|
||||
"contributors": [
|
||||
"andrewsardone",
|
||||
"angelikatyborska",
|
||||
"austinlyons",
|
||||
"cbliard",
|
||||
"Cohen-Carlisle",
|
||||
"dalexj",
|
||||
"devonestes",
|
||||
"digitalronin",
|
||||
"doncruse",
|
||||
"etrepum",
|
||||
"ghajba",
|
||||
"kytrinyx",
|
||||
"lpil",
|
||||
"neenjaw",
|
||||
"parkerl",
|
||||
"pminten",
|
||||
"rsslldnphy",
|
||||
"seeflanigan",
|
||||
"sotojuan",
|
||||
"Teapane",
|
||||
"tjcelaya",
|
||||
"Tonkpils",
|
||||
"victorpre",
|
||||
"vladimir-tikhonov",
|
||||
"waiting-for-dev"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"lib/bob.ex"
|
||||
],
|
||||
"test": [
|
||||
"test/bob_test.exs"
|
||||
],
|
||||
"example": [
|
||||
".meta/example.ex"
|
||||
]
|
||||
},
|
||||
"source": "Inspired by the 'Deaf Grandma' exercise in Chris Pine's Learn to Program tutorial.",
|
||||
"source_url": "http://pine.fm/LearnToProgram/?Chapter=06"
|
||||
}
|
||||
1
bob/.exercism/metadata.json
Normal file
1
bob/.exercism/metadata.json
Normal file
|
|
@ -0,0 +1 @@
|
|||
{"track":"elixir","exercise":"bob","id":"d402d783bcca42a59df2a01c8d291ebe","url":"https://exercism.org/tracks/elixir/exercises/bob","handle":"rjNemo","is_requester":true,"auto_approve":false}
|
||||
4
bob/.formatter.exs
Normal file
4
bob/.formatter.exs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
# Used by "mix format"
|
||||
[
|
||||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
|
||||
]
|
||||
24
bob/.gitignore
vendored
Normal file
24
bob/.gitignore
vendored
Normal 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").
|
||||
bob-*.tar
|
||||
|
||||
75
bob/HELP.md
Normal file
75
bob/HELP.md
Normal 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/bob.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)
|
||||
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
|
||||
- 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.
|
||||
If you can't find what you're looking for in the documentation, feel free to ask help in the Exercism's BEAM [gitter channel](https://gitter.im/exercism/xerlang).
|
||||
59
bob/README.md
Normal file
59
bob/README.md
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
# Bob
|
||||
|
||||
Welcome to Bob on Exercism's Elixir Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
|
||||
## Instructions
|
||||
|
||||
Bob is a lackadaisical teenager. In conversation, his responses are very limited.
|
||||
|
||||
Bob answers 'Sure.' if you ask him a question, such as "How are you?".
|
||||
|
||||
He answers 'Whoa, chill out!' if you YELL AT HIM (in all capitals).
|
||||
|
||||
He answers 'Calm down, I know what I'm doing!' if you yell a question at him.
|
||||
|
||||
He says 'Fine. Be that way!' if you address him without actually saying
|
||||
anything.
|
||||
|
||||
He answers 'Whatever.' to anything else.
|
||||
|
||||
Bob's conversational partner is a purist when it comes to written communication and always follows normal rules regarding sentence punctuation in English.
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @rubysolo
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @andrewsardone
|
||||
- @angelikatyborska
|
||||
- @austinlyons
|
||||
- @cbliard
|
||||
- @Cohen-Carlisle
|
||||
- @dalexj
|
||||
- @devonestes
|
||||
- @digitalronin
|
||||
- @doncruse
|
||||
- @etrepum
|
||||
- @ghajba
|
||||
- @kytrinyx
|
||||
- @lpil
|
||||
- @neenjaw
|
||||
- @parkerl
|
||||
- @pminten
|
||||
- @rsslldnphy
|
||||
- @seeflanigan
|
||||
- @sotojuan
|
||||
- @Teapane
|
||||
- @tjcelaya
|
||||
- @Tonkpils
|
||||
- @victorpre
|
||||
- @vladimir-tikhonov
|
||||
- @waiting-for-dev
|
||||
|
||||
### Based on
|
||||
|
||||
Inspired by the 'Deaf Grandma' exercise in Chris Pine's Learn to Program tutorial. - http://pine.fm/LearnToProgram/?Chapter=06
|
||||
17
bob/lib/bob.ex
Normal file
17
bob/lib/bob.ex
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
defmodule Bob do
|
||||
@spec hey(String.t()) :: String.t()
|
||||
def hey(input) do
|
||||
letter? = &(String.upcase(&1) != String.downcase(&1))
|
||||
shouting? = &(&1 == String.upcase(&1) and letter?.(&1))
|
||||
question? = &(String.trim(&1) |> String.ends_with?("?"))
|
||||
blank? = &(String.trim(&1) == "")
|
||||
|
||||
cond do
|
||||
blank?.(input) -> "Fine. Be that way!"
|
||||
shouting?.(input) and question?.(input) -> "Calm down, I know what I'm doing!"
|
||||
shouting?.(input) -> "Whoa, chill out!"
|
||||
question?.(input) -> "Sure."
|
||||
true -> "Whatever."
|
||||
end
|
||||
end
|
||||
end
|
||||
28
bob/mix.exs
Normal file
28
bob/mix.exs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
defmodule Bob.MixProject do
|
||||
use Mix.Project
|
||||
|
||||
def project do
|
||||
[
|
||||
app: :bob,
|
||||
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
|
||||
115
bob/test/bob_test.exs
Normal file
115
bob/test/bob_test.exs
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
defmodule BobTest do
|
||||
use ExUnit.Case
|
||||
|
||||
test "stating something" do
|
||||
assert Bob.hey("Tom-ay-to, tom-aaaah-to.") == "Whatever."
|
||||
end
|
||||
|
||||
test "shouting" do
|
||||
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
|
||||
end
|
||||
|
||||
test "shouting gibberish" do
|
||||
assert Bob.hey("FCECDFCAAB") == "Whoa, chill out!"
|
||||
end
|
||||
|
||||
test "asking a question" do
|
||||
assert Bob.hey("Does this cryogenic chamber make me look fat?") == "Sure."
|
||||
end
|
||||
|
||||
test "asking a numeric question" do
|
||||
assert Bob.hey("You are, what, like 15?") == "Sure."
|
||||
end
|
||||
|
||||
test "asking gibberish" do
|
||||
assert Bob.hey("fffbbcbeab?") == "Sure."
|
||||
end
|
||||
|
||||
test "talking forcefully" do
|
||||
assert Bob.hey("Hi there!") == "Whatever."
|
||||
end
|
||||
|
||||
test "using acronyms in regular speech" do
|
||||
assert Bob.hey("It's OK if you don't want to go to the DMV.") == "Whatever."
|
||||
end
|
||||
|
||||
test "talking in capitals" do
|
||||
assert Bob.hey("This Isn't Shouting!") == "Whatever."
|
||||
end
|
||||
|
||||
test "forceful question" do
|
||||
assert Bob.hey("WHAT'S GOING ON?") == "Calm down, I know what I'm doing!"
|
||||
end
|
||||
|
||||
test "asking in capitals" do
|
||||
assert Bob.hey("THIS ISN'T SHOUTING?") == "Calm down, I know what I'm doing!"
|
||||
end
|
||||
|
||||
test "shouting numbers" do
|
||||
assert Bob.hey("1, 2, 3 GO!") == "Whoa, chill out!"
|
||||
end
|
||||
|
||||
test "shouting with special characters" do
|
||||
assert Bob.hey("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!") == "Whoa, chill out!"
|
||||
end
|
||||
|
||||
test "shouting with no exclamation mark" do
|
||||
assert Bob.hey("I HATE THE DENTIST") == "Whoa, chill out!"
|
||||
end
|
||||
|
||||
test "statement containing question mark" do
|
||||
assert Bob.hey("Ending with ? means a question.") == "Whatever."
|
||||
end
|
||||
|
||||
test "silence" do
|
||||
assert Bob.hey("") == "Fine. Be that way!"
|
||||
end
|
||||
|
||||
test "prolonged silence" do
|
||||
assert Bob.hey(" ") == "Fine. Be that way!"
|
||||
end
|
||||
|
||||
test "alternate silence" do
|
||||
assert Bob.hey("\t\t\t\t\t\t\t\t\t\t") == "Fine. Be that way!"
|
||||
end
|
||||
|
||||
test "only numbers" do
|
||||
assert Bob.hey("1, 2, 3") == "Whatever."
|
||||
end
|
||||
|
||||
test "multiple line question" do
|
||||
assert Bob.hey("\nDoes this cryogenic chamber make me look fat?\nNo.") == "Whatever."
|
||||
end
|
||||
|
||||
test "question with numbers" do
|
||||
assert Bob.hey("4?") == "Sure."
|
||||
end
|
||||
|
||||
test "non-letters with question" do
|
||||
assert Bob.hey(":) ?") == "Sure."
|
||||
end
|
||||
|
||||
test "prattling on" do
|
||||
assert Bob.hey("Wait! Hang on. Are you going to be OK?") == "Sure."
|
||||
end
|
||||
|
||||
test "starting with whitespace" do
|
||||
assert Bob.hey(" hmmmmmmm...") == "Whatever."
|
||||
end
|
||||
|
||||
test "ending with whitespace" do
|
||||
assert Bob.hey("Okay if like my spacebar quite a bit? ") == "Sure."
|
||||
end
|
||||
|
||||
test "other whitespace" do
|
||||
assert Bob.hey("\n\r \t") == "Fine. Be that way!"
|
||||
end
|
||||
|
||||
test "non-question ending with whitespace" do
|
||||
assert Bob.hey("This is a statement ending with whitespace ") == "Whatever."
|
||||
end
|
||||
|
||||
test "shouting in Russian" do
|
||||
assert Bob.hey("УХОДИ") == "Whoa, chill out!"
|
||||
end
|
||||
end
|
||||
2
bob/test/test_helper.exs
Normal file
2
bob/test/test_helper.exs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
ExUnit.start()
|
||||
ExUnit.configure(exclude: :pending, trace: true)
|
||||
25
name-badge/.exercism/config.json
Normal file
25
name-badge/.exercism/config.json
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
{
|
||||
"blurb": "Learn about nil and the if conditional expression by printing name badges for factory employees.",
|
||||
"icon": "tim-from-marketing",
|
||||
"authors": [
|
||||
"angelikatyborska"
|
||||
],
|
||||
"contributors": [
|
||||
"neenjaw"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"lib/name_badge.ex"
|
||||
],
|
||||
"test": [
|
||||
"test/name_badge_test.exs"
|
||||
],
|
||||
"exemplar": [
|
||||
".meta/exemplar.ex"
|
||||
]
|
||||
},
|
||||
"forked_from": [
|
||||
"csharp/tim-from-marketing"
|
||||
],
|
||||
"language_versions": ">=1.10"
|
||||
}
|
||||
1
name-badge/.exercism/metadata.json
Normal file
1
name-badge/.exercism/metadata.json
Normal file
|
|
@ -0,0 +1 @@
|
|||
{"track":"elixir","exercise":"name-badge","id":"f85b910f260242008c01c4b1e66fe785","url":"https://exercism.org/tracks/elixir/exercises/name-badge","handle":"rjNemo","is_requester":true,"auto_approve":false}
|
||||
4
name-badge/.formatter.exs
Normal file
4
name-badge/.formatter.exs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
# Used by "mix format"
|
||||
[
|
||||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
|
||||
]
|
||||
24
name-badge/.gitignore
vendored
Normal file
24
name-badge/.gitignore
vendored
Normal 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").
|
||||
nil-*.tar
|
||||
|
||||
75
name-badge/HELP.md
Normal file
75
name-badge/HELP.md
Normal 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/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)
|
||||
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
|
||||
- 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.
|
||||
If you can't find what you're looking for in the documentation, feel free to ask help in the Exercism's BEAM [gitter channel](https://gitter.im/exercism/xerlang).
|
||||
25
name-badge/HINTS.md
Normal file
25
name-badge/HINTS.md
Normal file
|
|
@ -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
|
||||
104
name-badge/README.md
Normal file
104
name-badge/README.md
Normal file
|
|
@ -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 data types 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
|
||||
[kernel-if]: https://hexdocs.pm/elixir/Kernel.html#if/2
|
||||
[getting-started-if-unless]: https://elixir-lang.org/getting-started/case-cond-and-if.html#if-and-unless
|
||||
|
||||
## 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
|
||||
13
name-badge/lib/name_badge.ex
Normal file
13
name-badge/lib/name_badge.ex
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
defmodule NameBadge do
|
||||
def print(id, name, nil) do
|
||||
if id do
|
||||
"[#{id}] - #{name} - OWNER"
|
||||
else
|
||||
"#{name} - OWNER"
|
||||
end
|
||||
end
|
||||
|
||||
def print(nil, name, department), do: "#{name} - #{String.upcase(department)}"
|
||||
|
||||
def print(id, name, department), do: "[#{id}] - #{name} - #{String.upcase(department)}"
|
||||
end
|
||||
28
name-badge/mix.exs
Normal file
28
name-badge/mix.exs
Normal file
|
|
@ -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
|
||||
33
name-badge/test/name_badge_test.exs
Normal file
33
name-badge/test/name_badge_test.exs
Normal file
|
|
@ -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
|
||||
2
name-badge/test/test_helper.exs
Normal file
2
name-badge/test/test_helper.exs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
ExUnit.start()
|
||||
ExUnit.configure(exclude: :pending, trace: true, seed: 0)
|
||||
Loading…
Reference in a new issue