Avatar of artemkorsakov

artemkorsakov's solution

to Secret Handshake in the C# Track

Published at Feb 27 2019 · 0 comments
Instructions
Test suite
Solution

There are 10 types of people in the world: Those who understand binary, and those who don't.

You and your fellow cohort of those in the "know" when it comes to binary decide to come up with a secret "handshake".

1 = wink
10 = double blink
100 = close your eyes
1000 = jump


10000 = Reverse the order of the operations in the secret handshake.

Given a decimal number, convert it to the appropriate sequence of events for a secret handshake.

Here's a couple of examples:

Given the input 3, the method would return the array ["wink", "double blink"] because 3 is 11 in binary.

Given the input 19, the method would return the array ["double blink", "wink"] because 19 is 10011 in binary. Notice that the addition of 16 (10000 in binary) has caused the array to be reversed.

Running the tests

To run the tests, run the command dotnet test from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the Skip property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using exercism submit SecretHandshake.cs

Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

Source

Bert, in Mary Poppins http://www.imdb.com/title/tt0058331/quotes/qt0437047

Submitting Incomplete Solutions

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

SecretHandshakeTest.cs

// This file was auto-generated based on version 1.2.0 of the canonical data.

using Xunit;

public class SecretHandshakeTest
{
    [Fact]
    public void Wink_for_1()
    {
        Assert.Equal(new[] { "wink" }, SecretHandshake.Commands(1));
    }

    [Fact(Skip = "Remove to run test")]
    public void Double_blink_for_10()
    {
        Assert.Equal(new[] { "double blink" }, SecretHandshake.Commands(2));
    }

    [Fact(Skip = "Remove to run test")]
    public void Close_your_eyes_for_100()
    {
        Assert.Equal(new[] { "close your eyes" }, SecretHandshake.Commands(4));
    }

    [Fact(Skip = "Remove to run test")]
    public void Jump_for_1000()
    {
        Assert.Equal(new[] { "jump" }, SecretHandshake.Commands(8));
    }

    [Fact(Skip = "Remove to run test")]
    public void Combine_two_actions()
    {
        Assert.Equal(new[] { "wink", "double blink" }, SecretHandshake.Commands(3));
    }

    [Fact(Skip = "Remove to run test")]
    public void Reverse_two_actions()
    {
        Assert.Equal(new[] { "double blink", "wink" }, SecretHandshake.Commands(19));
    }

    [Fact(Skip = "Remove to run test")]
    public void Reversing_one_action_gives_the_same_action()
    {
        Assert.Equal(new[] { "jump" }, SecretHandshake.Commands(24));
    }

    [Fact(Skip = "Remove to run test")]
    public void Reversing_no_actions_still_gives_no_actions()
    {
        Assert.Empty(SecretHandshake.Commands(16));
    }

    [Fact(Skip = "Remove to run test")]
    public void All_possible_actions()
    {
        Assert.Equal(new[] { "wink", "double blink", "close your eyes", "jump" }, SecretHandshake.Commands(15));
    }

    [Fact(Skip = "Remove to run test")]
    public void Reverse_all_possible_actions()
    {
        Assert.Equal(new[] { "jump", "close your eyes", "double blink", "wink" }, SecretHandshake.Commands(31));
    }

    [Fact(Skip = "Remove to run test")]
    public void Do_nothing_for_zero()
    {
        Assert.Empty(SecretHandshake.Commands(0));
    }
}
using System;
using System.Collections.Generic;

public static class SecretHandshake
{
    private static readonly string[] handshakes = { "wink", "double blink", "close your eyes", "jump" };

    public static string[] Commands(int commandValue)
    {
        if (commandValue > 32 || commandValue < 1)
        {
            return new string[] { };
        }

        var result = new List<string>();
        var binary = Convert.ToString(commandValue, 2);
        for (var i = 0; i < Math.Min(4, binary.Length); i++)
        {
            if (binary[binary.Length - 1 - i] == '1')
            {
                result.Add(handshakes[i]);
            }
        }

        if (binary.Length == 5)
        {
            result.Reverse();
        }

        return result.ToArray();
    }
}

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?