🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of angelikatyborska

angelikatyborska's solution

to Two Fer in the Erlang Track

Published at Oct 14 2019 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

Two-fer or 2-fer is short for two for one. One for you and one for me.

Given a name, return a string with the message:

One for X, one for me.

Where X is the given name.

However, if the name is missing, return the string:

One for you, one for me.

Here are some examples:

Name String to return
Alice One for Alice, one for me.
Bob One for Bob, one for me.
One for you, one for me.
Zaphod One for Zaphod, one for me.

Running tests

In order to run the tests, issue the following command from the exercise directory:

For running the tests provided, rebar3 is used as it is the official build and dependency management tool for erlang now. Please refer to the tracks installation instructions on how to do that.

In order to run the tests, you can issue the following command from the exercise directory.

$ rebar3 eunit

Questions?

For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

Source

https://github.com/exercism/problem-specifications/issues/757

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

two_fer_tests.erl

%% Based on canonical data version 1.2.0-1
%% https://github.com/exercism/problem-specifications/raw/master/exercises/two-fer/canonical-data.json
%% This file is automatically generated from the exercises canonical data.

-module(two_fer_tests).

-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").


-define(assertStringEqual(Expect, Expr),
        begin ((fun () ->
            __X = (Expect),
            __Y = (Expr),
            case string:equal(__X, __Y) of
                true -> ok;
                false -> erlang:error({assertStringEqual,
                    [{module, ?MODULE},
                     {line, ?LINE},
                     {expression, (??Expr)},
                     {expected, unicode:characters_to_list(__X)},
                     {value, unicode:characters_to_list(__Y)}]})
            end
        end)())
    end).

-define(_assertStringEqual(Expect, Expr), ?_test(?assertStringEqual(Expect, Expr))).


'1_no_name_given_test_'() ->
    {"no name given",
     ?_assertStringEqual("One for you, one for me.",
			 two_fer:two_fer())}.

'2_a_name_given_test_'() ->
    {"a name given",
     ?_assertStringEqual("One for Alice, one for me.",
			 two_fer:two_fer("Alice"))}.

'3_another_name_given_test_'() ->
    {"another name given",
     ?_assertStringEqual("One for Bob, one for me.",
			 two_fer:two_fer("Bob"))}.
-module(two_fer).

-export([two_fer/0, two_fer/1]).


two_fer() -> two_fer("you").

two_fer(Name) -> "One for " ++ Name ++ ", one for me.".

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?