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

ryanplusplus's solution

to Secret Handshake in the Lua Track

Published at Jul 13 2018 · 0 comments
Test suite

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.


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.


local secret_handshake = require 'secret-handshake'

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

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

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

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

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

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

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

  it('should allow all primitives to be used together', function()
      { 'jump', 'close your eyes', 'double blink', 'wink' },
      secret_handshake(tonumber('11111', 2))
local function reverse(t)
  local reversed = {}
  for i = #t, 1, -1 do
    table.insert(reversed, t[i])
  return reversed

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

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?