Avatar of glennj

glennj's solution

to Secret Handshake in the Lua Track

Published at Mar 09 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 function would return the array ["wink", "double blink"] because 3 is 11 in binary.

Given the input 19, the function 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 busted from within the exercise directory.

Further information

For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua 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.

secret-handshake_spec.lua

local secret_handshake = require 'secret-handshake'

describe('secret-handshake', function()
  it('should allow empty handshakes', function()
    assert.are.same({}, secret_handshake(0))
  end)

  it('should interpret 0b1 as wink', function()
    assert.are.same({ 'wink' }, secret_handshake(tonumber('1', 2)))
  end)

  it('should interpret 0b10 as double blink', function()
    assert.are.same({ 'double blink' }, secret_handshake(tonumber('10', 2)))
  end)

  it('should interpret 0b100 as close your eyes', function()
    assert.are.same({ 'close your eyes' }, secret_handshake(tonumber('100', 2)))
  end)

  it('should interpret 0b1000 as jump', function()
    assert.are.same({ 'jump' }, secret_handshake(tonumber('1000', 2)))
  end)

  it('should allow multiple handshake primitives to be used together', function()
    assert.are.same({ 'wink', 'double blink' }, secret_handshake(tonumber('11', 2)))
  end)

  it('should reverse the order of the primitives when 0b10000 is used', function()
    assert.are.same({ 'double blink', 'wink' }, secret_handshake(tonumber('10011', 2)))
  end)

  it('should allow all primitives to be used together', function()
    assert.are.same(
      { 'jump', 'close your eyes', 'double blink', 'wink' },
      secret_handshake(tonumber('11111', 2))
    )
  end)
end)
local secrets = {
    {tonumber('00001', 2), 'wink'},
    {tonumber('00010', 2), 'double blink'},
    {tonumber('00100', 2), 'close your eyes'},
    {tonumber('01000', 2), 'jump'},
}
local reverse = tonumber('10000', 2)

local secret_handshake = function(code)
    local reversed = code & reverse > 0
    local handshake = {}
    for _, secret in ipairs(secrets) do
        local mask, action = table.unpack(secret)
        if code & mask > 0 then
            local pos = reversed and 1 or #handshake + 1
            table.insert(handshake, pos, action)
        end
    end
    return handshake
end

return secret_handshake

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?