german-sysadmin
This commit is contained in:
parent
5f3aa01e4d
commit
a4a5085d00
|
@ -0,0 +1,21 @@
|
|||
{
|
||||
"authors": [
|
||||
"angelikatyborska"
|
||||
],
|
||||
"contributors": [
|
||||
"neenjaw"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"lib/username.ex"
|
||||
],
|
||||
"test": [
|
||||
"test/username_test.exs"
|
||||
],
|
||||
"exemplar": [
|
||||
".meta/exemplar.ex"
|
||||
]
|
||||
},
|
||||
"language_versions": ">=1.10",
|
||||
"blurb": "Learn about charlists and the case conditional expression by sanitizing usernames of employees in a German company."
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
{"track":"elixir","exercise":"german-sysadmin","id":"34f6e3c04d204dacb138cbd61297fa50","url":"https://exercism.org/tracks/elixir/exercises/german-sysadmin","handle":"negrienko","is_requester":true,"auto_approve":false}
|
|
@ -0,0 +1,4 @@
|
|||
# Used by "mix format"
|
||||
[
|
||||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
|
||||
]
|
|
@ -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").
|
||||
basic_strings-*.tar
|
||||
|
|
@ -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/username.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.
|
|
@ -0,0 +1,36 @@
|
|||
# Hints
|
||||
|
||||
## General
|
||||
|
||||
- Read about [charlists][getting-started-charlists], [Unicode and code points][getting-started-code-points], and [`case`][getting-started-case] in the official Getting Started guide.
|
||||
- Read about [charlists][elixirschool-charlists] and [`case`][elixirschool-case] on elixirschool.com.
|
||||
|
||||
## 1. Sanitize existing usernames by removing everything but lowercase letters
|
||||
|
||||
- Use recursion to iterate over a charlist.
|
||||
- There is a [built-in function][kernel-case] that allows us to compare a given value against many patterns.
|
||||
- There is a [special syntax][syntax-reference-code-points] for getting a character's code point. It can be used in guards.
|
||||
- Charlists are lists of code points, and code points, being numbers, can be [compared][kernel-greater-than] to one another.
|
||||
- Lowercase Latin letters all have [consecutive code points][unicode-character-reference], in alphabetical order.
|
||||
- There is [a built-in function][kernel-concat-list] that allows you to concatenate two lists.
|
||||
|
||||
## 2. Allow underscores
|
||||
|
||||
- There is a [special syntax][syntax-reference-code-points] for getting a character's code point. It can be used in guards and it works for punctuation characters too.
|
||||
- There is [a built-in function][kernel-concat-list] that allows you to concatenate two lists.
|
||||
|
||||
## 3. Substitute German characters
|
||||
|
||||
- There is a [special syntax][syntax-reference-code-points] for getting a character's code point. It can be used in guards and it works for non-Latin letters too.
|
||||
- There is [a built-in function][kernel-concat-list] that allows you to concatenate two lists.
|
||||
|
||||
[syntax-reference-code-points]: https://hexdocs.pm/elixir/syntax-reference.html#integers-in-other-bases-and-unicode-code-points
|
||||
[getting-started-code-points]: https://elixir-lang.org/getting-started/binaries-strings-and-char-lists.html#unicode-and-code-points
|
||||
[getting-started-charlists]: https://elixir-lang.org/getting-started/binaries-strings-and-char-lists.html#charlists
|
||||
[getting-started-case]: https://elixir-lang.org/getting-started/case-cond-and-if.html#case
|
||||
[elixirschool-charlists]: https://elixirschool.com/en/lessons/basics/strings/#charlists
|
||||
[elixirschool-case]: https://elixirschool.com/en/lessons/basics/control-structures/#case
|
||||
[unicode-character-reference]: https://en.wikibooks.org/wiki/Unicode/Character_reference/0000-0FFF
|
||||
[kernel-concat-list]: https://hexdocs.pm/elixir/Kernel.html#++/2
|
||||
[kernel-greater-than]: https://hexdocs.pm/elixir/Kernel.html#%3E/2
|
||||
[kernel-case]: https://hexdocs.pm/elixir/Kernel.SpecialForms.html#case/2
|
|
@ -0,0 +1,125 @@
|
|||
# German Sysadmin
|
||||
|
||||
Welcome to German Sysadmin 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
|
||||
|
||||
## Charlists
|
||||
|
||||
Charlists are created using the `~c` Sigil.
|
||||
|
||||
```elixir
|
||||
~c"hello"
|
||||
```
|
||||
|
||||
~~~~exercism/note
|
||||
Note that in older versions of Elixir, charlists are represented as `'hello'` with single quotes.
|
||||
~~~~
|
||||
|
||||
Although they look very similar to strings, the two data types are quite different from one another. A charlist is a list of integers. The integers represent the Unicode values of a given character — also known as code points.
|
||||
|
||||
```elixir
|
||||
[65, 66, 67]
|
||||
# => ~c"ABC"
|
||||
```
|
||||
|
||||
You can prepend a character with `?` to get its code point.
|
||||
|
||||
```elixir
|
||||
?A
|
||||
# => 65
|
||||
|
||||
[?:, ?)]
|
||||
# => ~c":)"
|
||||
```
|
||||
|
||||
Because charlist are lists, you can work with them just like with any other list - using recursion and pattern matching.
|
||||
|
||||
```elixir
|
||||
[first_letter | _] = ~c"cat"
|
||||
first_letter
|
||||
# => 99
|
||||
```
|
||||
|
||||
You can concatenate two lists using `++`.
|
||||
|
||||
```elixir
|
||||
~c"hi" ++ ~c"!"
|
||||
# => ~c"hi!"
|
||||
```
|
||||
|
||||
The longer the first list is, the slower the concatenation, so avoid repeatedly appending to lists of arbitrary length.
|
||||
|
||||
## Case
|
||||
|
||||
`case` is a control flow structure that allows us to compare a given value against many patterns. Clauses in a `case` expression are evaluated from top to bottom, until a match is found.
|
||||
|
||||
```elixir
|
||||
age = 15
|
||||
|
||||
case age do
|
||||
0 -> ~c"infant"
|
||||
age when age < 4 -> ~c"baby"
|
||||
age when age < 13 -> ~c"child"
|
||||
age when age < 18 -> ~c"teenager"
|
||||
_ -> ~c"adult"
|
||||
end
|
||||
|
||||
# => ~c"teenager"
|
||||
```
|
||||
|
||||
## Instructions
|
||||
|
||||
You are working as a system administrator for a big company in Munich, Germany. One of your responsibilities is managing email accounts.
|
||||
|
||||
You have been hearing complaints from people saying they are unable to write emails to Mr. Müller. You quickly realize that most of the company uses an old email client that doesn't recognize `müller@firma.de` as a valid email address because of the non-Latin character.
|
||||
|
||||
Telling people to give up their favorite old email client is a lost battle, so you decide to create sanitized aliases for all email accounts.
|
||||
|
||||
## 1. Sanitize existing usernames by removing everything but lowercase letters
|
||||
|
||||
Implement the `sanitize/1` function. It should accept a username as a charlist and return the username with all characters but lowercase letters removed.
|
||||
|
||||
```elixir
|
||||
Username.sanitize(~c"schmidt1985")
|
||||
# => ~c"schmidt"
|
||||
```
|
||||
|
||||
## 2. Allow underscores
|
||||
|
||||
Extend the `sanitize/1` function. It should not remove underscores from the username.
|
||||
|
||||
```elixir
|
||||
Username.sanitize(~c"mark_fischer$$$")
|
||||
# => ~c"mark_fischer"
|
||||
```
|
||||
|
||||
## 3. Substitute German characters
|
||||
|
||||
There are 4 non-Latin characters in the German alphabet, and all of them have commonly-recognized Latin substitutes.
|
||||
|
||||
| German character | Latin substitute |
|
||||
| ---------------- | ---------------- |
|
||||
| ä | ae |
|
||||
| ö | oe |
|
||||
| ü | ue |
|
||||
| ß | ss |
|
||||
|
||||
Extend the `sanitize/1` function. It should substitute German characters according to the table. You can safely assume all usernames are already downcase.
|
||||
|
||||
```elixir
|
||||
Username.sanitize(~c"cäcilie_weiß")
|
||||
# => ~c"caecilie_weiss"
|
||||
```
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @angelikatyborska
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @neenjaw
|
|
@ -0,0 +1,17 @@
|
|||
defmodule Username do
|
||||
def sanitize([]), do: []
|
||||
|
||||
def sanitize([first | other]) do
|
||||
sanitized =
|
||||
case first do
|
||||
?ä -> ~c"ae"
|
||||
?ö -> ~c"oe"
|
||||
?ü -> ~c"ue"
|
||||
?ß -> ~c"ss"
|
||||
ok when ok in ?a..?z or ok == ?_ -> [ok]
|
||||
_ -> []
|
||||
end
|
||||
|
||||
sanitized ++ sanitize(other)
|
||||
end
|
||||
end
|
|
@ -0,0 +1,28 @@
|
|||
defmodule Username.MixProject do
|
||||
use Mix.Project
|
||||
|
||||
def project do
|
||||
[
|
||||
app: :german_sysadmin,
|
||||
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
|
|
@ -0,0 +1,2 @@
|
|||
ExUnit.start()
|
||||
ExUnit.configure(exclude: :pending, trace: true, seed: 0)
|
|
@ -0,0 +1,63 @@
|
|||
defmodule UsernameTest do
|
||||
use ExUnit.Case
|
||||
|
||||
describe "sanitize/1" do
|
||||
@tag task_id: 1
|
||||
test "works for an empty charlist" do
|
||||
assert Username.sanitize(~c"") == ~c""
|
||||
end
|
||||
|
||||
@tag task_id: 1
|
||||
test "it allows lowercase Latin letters" do
|
||||
assert Username.sanitize(~c"anne") == ~c"anne"
|
||||
end
|
||||
|
||||
@tag task_id: 1
|
||||
test "it allows the whole lowercase Latin alphabet" do
|
||||
lowercase_latin_letters = ~c"abcdefghijklmnopqrstuvwxyz"
|
||||
|
||||
assert Username.sanitize(lowercase_latin_letters) == lowercase_latin_letters
|
||||
end
|
||||
|
||||
@tag task_id: 1
|
||||
test "it removes numbers" do
|
||||
assert Username.sanitize(~c"schmidt1985") == ~c"schmidt"
|
||||
end
|
||||
|
||||
@tag task_id: 1
|
||||
test "it removes punctuation" do
|
||||
assert Username.sanitize(~c"*fritz*!$%") == ~c"fritz"
|
||||
end
|
||||
|
||||
@tag task_id: 1
|
||||
test "it removes whitespace" do
|
||||
assert Username.sanitize(~c" olaf ") == ~c"olaf"
|
||||
end
|
||||
|
||||
@tag task_id: 1
|
||||
test "it removes all disallowed characters" do
|
||||
allowed_characters = ~c"abcdefghijklmnopqrstuvwxyz_ßäöü"
|
||||
input = Enum.to_list(0..0x10FFFF) -- allowed_characters
|
||||
|
||||
assert Username.sanitize(input) == ~c""
|
||||
end
|
||||
|
||||
@tag task_id: 2
|
||||
test "it allows underscores" do
|
||||
assert Username.sanitize(~c"marcel_huber") == ~c"marcel_huber"
|
||||
end
|
||||
|
||||
@tag task_id: 3
|
||||
test "it substitutes German letters" do
|
||||
assert Username.sanitize(~c"krüger") == ~c"krueger"
|
||||
assert Username.sanitize(~c"köhler") == ~c"koehler"
|
||||
assert Username.sanitize(~c"jäger") == ~c"jaeger"
|
||||
assert Username.sanitize(~c"groß") == ~c"gross"
|
||||
end
|
||||
|
||||
@tag task_id: 3
|
||||
test "it substitutes German letters and removes disallowed characters" do
|
||||
assert Username.sanitize(~c"köhler_jäger42") == ~c"koehler_jaeger"
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue