Avatar of Nia11

Nia11's solution

to Secret Handshake in the Lua Track

Published at Dec 23 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.

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)
return function(input)
	local actions = {'wink','double blink','close your eyes','jump'}
	local output= {}

	local count = 1

	while input>0 do
		if count<5 then
			if input%2>0 then 
				table.insert(output,actions[count])
			end
		else
			local reversed = {}
			for i = #output,1,-1 do
				table.insert(reversed,output[i] 	)
			end
			output=reversed
		end
		input=math.floor(input/2)
		count=count+1
	end
	return output
end

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?