exercism/elixir/captains-log/HINTS.md

38 lines
2.3 KiB
Markdown

# Hints
## General
- Read about using Erlang libraries in the [official Getting Started guide][getting-started-erlang-libraries], and in particular about [formatting strings][getting-started-formatted-text-output].
## 1. Generate a random planet
- Use the provided module attribute with a list of letters representing planetary classes.
- There is a [built-in function][enum-random] for choosing an element from a list at random.
## 2. Generate a random starship registry number
- There is a [built-in function][enum-random] for choosing an element from a range at random.
## 3. Generate a random stardate
- There is no Elixir function that would return a random float.
- There is a [built-in Erlang function][erl-rand-uniform] that returns a random float x where `0.0 <= x < 1.0`.
- If `x` belongs to a range `0.0 <= x < 1.0`, but you need a number from a different range `a <= x < b`, you can shift x's range by multiplying it by the range's width (`b - a`) and adding the range's start (`a`). That is: `x * (b - a) + a`.
## 4. Format the stardate
- There is no Elixir function that would be able to, in a single step, format a float as a string with a given precision.
- There is a [built-in Erlang function][erl-io-lib-format] that takes a format string and a list of data, and returns a charlist.
- There is a [built-in function][to-string] that changes a charlist to a string.
- The format string of that function contains control sequences.
- A control sequence starts with `~` and has the pattern `~F.P.PadModC`, where `F` stands for the width of the output, `P` stands for the precision, `Pad` stands for the padding character, `Mod` stands for the control sequence modifier, and `C` is the type of the control sequence.
- To format a float with a desired precision, a control sequence with the pattern `~.PC` will suffice.
- The control sequence type for floats is `f`.
- The exact format string you need is `~.1f`.
[getting-started-erlang-libraries]: https://hexdocs.pm/elixir/erlang-libraries.html
[getting-started-formatted-text-output]: https://hexdocs.pm/elixir/erlang-libraries.html#formatted-text-output
[enum-random]: https://hexdocs.pm/elixir/Enum.html#random/1
[erl-rand-uniform]: https://www.erlang.org/doc/man/rand.html#uniform-0
[erl-io-lib-format]: https://www.erlang.org/doc/man/io_lib.html#format-2
[to-string]: https://hexdocs.pm/elixir/Kernel.html#to_string/1