ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

ryanplusplus's solution

to Secret Handshake in the Lua Track

Published at Jul 13 2018 · 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
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(
secret_handshake(tonumber('11111', 2))
)
end)
end)``````
``````local function reverse(t)
local reversed = {}
for i = #t, 1, -1 do
table.insert(reversed, t[i])
end
return reversed
end

return function(n)
local handshake = {}
if n & 1 > 0 then table.insert(handshake, 'wink') end
if n & 2 > 0 then table.insert(handshake, 'double blink') end
if n & 4 > 0 then table.insert(handshake, 'close your eyes') end
if n & 8 > 0 then table.insert(handshake, 'jump') end
if n & 16 > 0 then handshake = reverse(handshake) end
return handshake
end``````