Coder Social home page Coder Social logo

captains_log's Introduction

Captain's Log

Welcome to Captain's Log 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

Randomness

In Elixir, to choose a random element from an enumerable data structure (e.g. list, range), we use Enum.random. This function will pick a single element, with every element having equal probability of being picked.

Elixir does not have its own functions for picking a random float. To do that, we have to use Erlang directly.

Erlang Libraries

Elixir code runs in the BEAM virtual machine. BEAM is part of the Erlang Run-Time System. Being inspired by Erlang, and sharing its run environment, Elixir provides great interoperability with Erlang libraries. This means that Elixir developers can use Erlang libraries from within their Elixir code. In fact, writing Elixir libraries for functionality already provided by Erlang libraries is discouraged in the Elixir community.

As a result, certain functionality, like mathematical operations or timer functions, is only available in Elixir via Erlang.

Erlang's standard library is available for use in our Elixir code without any extra steps necessary.

Erlang functions can be called in the same way we call Elixir functions, with one small difference. Erlang module names are snake_case atoms. For example, to call the Erlang pi/0 function from the math module, one would write:

:math.pi()
# => 3.141592653589793

The most commonly used Erlang functions that do not have an Elixir equivalent are:

  • :timer.sleep/1 which suspends a process for the given amount of milliseconds.
  • :rand.uniform/0 which generates a random float x, where 0.0 <= x < 1.0.
  • :io_lib.format/2 which provides C-style string formatting (using control sequences). Using this function, we could for example print an integer in any base between 2 and 36 or format a float with desired precision. Note that this function, like many Erlang functions, returns a charlist.
  • The math module that provides mathematical functions such as sin/1, cos/1, log2/1, log10/1, pow/2, and more.

To discover Erlang's standard library, explore the STDLIB Reference Manual.

Instructions

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.

CaptainsLog.random_planet_class()
# => "K"

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.

CaptainsLog.random_ship_registry_number()
# => "NCC-1947"

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

The implementation should use an Erlang function.

CaptainsLog.random_stardate()
# => 41458.15721310934

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.

The implementation should use an Erlang function.

CaptainsLog.format_stardate(41458.15721310934)
# => "41458.2"

Source

Created by

  • @angelikatyborska

Contributed to by

  • @neenjaw

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.