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

To run the tests, run the command `busted`

from within the exercise directory.

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.

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

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

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

```
return function(secret)
local decode = {
"wink", "double blink", "close your eyes", "jump"
}
local is_reversed = secret & 16 > 0
local result = {}
for n, code in pairs(decode) do
if secret & 2^(n - 1) > 0 then
table.insert(result, is_reversed and 1 or #result + 1, code)
end
end
return result
end
```

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?

Level up your programming skills with 3,092 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments