Tracks
/
Elixir
Elixir
/
Exercises
/
German Sysadmin
German Sysadmin

German Sysadmin

Learning Exercise

Introduction

Charlists

Charlists are created using the ~c Sigil.

~c"hello"
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.

[65, 66, 67]
# => ~c"ABC"

You can prepend a character with ? to get its code point.

?A
# => 65

[?:, ?)]
# => ~c":)"

Because charlist are lists, you can work with them just like with any other list - using recursion and pattern matching.

[first_letter | _] = ~c"cat"
first_letter
# => 99

You can concatenate two lists using ++.

~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.

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.

Username.sanitize(~c"schmidt1985")
# => ~c"schmidt"

2. Allow underscores

Extend the sanitize/1 function. It should not remove underscores from the username.

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.

Username.sanitize(~c"cäcilie_weiß")
# => ~c"caecilie_weiss"
Edit via GitHub The link opens in a new window or tab
Elixir Exercism

Ready to start German Sysadmin?

Sign up to Exercism to learn and master Elixir with 57 concepts, 158 exercises, and real human mentoring, all for free.