iT邦幫忙

2022 iThome 鐵人賽

DAY 29
0
Software Development

戀戀 Elixir系列 第 29

二十九. Erlang Libraries & Randomness

  • 分享至 

  • xImage
  •  

Mary is a big fan of the TV series Star Trek: The Next Generation. She often plays pen-and-paper role playing games, where she and her friends pretend to be the crew of the Starship Enterprise.

Mary's character is Captain Picard, which means she has to keep the captain's log.
She loves the creative part of the game, but doesn't like to generate random data on the spot.

Help Mary by creating random generators for data commonly appearing in the captain's log.

1. Generate a random planet

The Starship Enterprise encounters many planets in its travels. Planets in the Star Trek universe are split into categories based on their properties. For example, Earth is a class M planet. All possible planetary classes are: D, H, J, K, L, M, N, R, T, and Y.

Implement the random_planet_class/0 function. It should return one of the planetary classes at random.

2. Generate a random starship registry number

Enterprise (registry number NCC-1701) is not the only starship flying around! When it rendezvous with another starship, Mary needs to log the registry number of that starship.

Registry numbers start with the prefix "NCC-" and then use a number from 1000 to 9999 (inclusive).

Implement the random_ship_registry_number/0 function that returns a random starship registry number.

3. Generate a random stardate

What's the use of a log if it doesn't include dates?

A stardate is a floating point number. The adventures of the Starship Enterprise from the first season of The Next Generation take place between the stardates 41000.0 and 42000.0. The "4" stands for the 24th century, the "1" for the first season.

Implement the function random_stardate/0 that returns a floating point number between 41000.0 (inclusive) and 42000.0 (exclusive).

4. Format the stardate

In the captain's log, stardates are usually rounded to a single decimal place.

Implement the format_stardate/1 function that will take a floating point number and return a string with the number rounded to a single decimal place.


https://exercism.org/tracks/elixir/exercises/captains-log

defmodule CaptainsLog do
  @moduledoc """
  practice random and erlang library usage
  """

  @planetary_classes ["D", "H", "J", "K", "L", "M", "N", "R", "T", "Y"]

  @typedoc """
  string in @planetary_classes
  """
  @type possible_planetary :: String.t()
  @spec random_planet_class() :: possible_planetary()
  def random_planet_class(), do: @planetary_classes |> Enum.random()

  @spec random_ship_registry_number() :: String.t()
  def random_ship_registry_number(), do: "NCC-#{1000..9999 |> Enum.random()}"

  @spec random_stardate() :: float()
  def random_stardate() do
    :rand.uniform() * (42000 - 41000) + 41000
  end

  @spec format_stardate(float()) :: String.t()
  def format_stardate(stardate), do: "~.1f" |> :io_lib.format([stardate]) |> to_string
end

上一篇
二十八. Protocols
下一篇
三十. Exceptions
系列文
戀戀 Elixir30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言