Fl4m3Ph03n1x

Fl4m3Ph03n1x

Exercism.io Secret handshake exercise?

I am doing some exercises while learning Elixir using Exercism.io.
Now, my objective is to do all exercises, extras included. This should give me a good grasp of the power of the language.

However I am having trouble. I don’t quite understand what some exercises want me to do, but I refuse to skip them either. One of these exercises is the following:

Introduction

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.

use Bitwise (or div/rem)

If you use Bitwise, an easy way to see if a particular bit is set is to compare the binary AND ( &&& ) of a set of bits with the particular bit pattern you want to check, and determine if the result is the same as the pattern you’re checking.

Example:

Flags: 0b11011 Check: 0b11010

Flags &&& Check: 0b11010 (All checked bits are set)

Another:

Flags: 0b11011 Check: 0b10110

Flags &&& Check: 0b10010 (Third bit not set)

Question

Could someone help me understand why 11 translates to ["wink", "double blink"] instead of translating to ["wink", "wink"] ? ( it has two 1, right ? )

I know I am missing something rather basic but I just can see what.

First Post!

Nopp

Nopp

11 in “binary” is three.
1 (binary = one) is wink, 01 (binary = two) is double blink.
One and two (blink and double blink) is three (binary = 11).

I hope this i nearly correct. I didn’t do those exercices, but this should be the the answer, if i understand the question right…

Most Liked

peerreynders

peerreynders

since modulo arithmetic applies and is even tested AFAIR.

Was there an allusion to how values greater than 31 are treated? What if I want ["wink","jump","wink"]? Frankly this is where I find these type of questions frustrating as the context seems to be oblivious to a lot of the unstated assumptions/constraints.

kip

kip

ex_cldr Core Team

This is another one that is quite fun with binary pattern matching :slight_smile:

defmodule Secret do
  def handshake(number) when is_integer(number) do
    handshake(<< number :: size(5) >>)
  end

  @code ["jump", "close your eyes", "double blink", "wink"]
  def handshake(<< reverse :: size(1), jump :: size(1), close :: size(1), double :: size(1), wink :: size(1) >>) do
    shake =
      [jump, close, double, wink]
      |> Enum.zip(@code)
      |> Enum.reduce([], fn
          {0, _}, acc -> acc
          {1, item}, acc -> [item | acc]
        end)

    # Since Enum.reduce/3 returns the list in reverse order,
    # we only reverse if the reverse bit is not set :-)
    if reverse == 1, do: shake, else: Enum.reverse(shake)
  end
end

iex> Secret.handshake 3
["wink", "double blink"]
hex> Secret.handshake 0b00111
["close your eyes", "double blink", "wink"]
iex> Secret.handshake 0b10111
["wink", "double blink", "close your eyes"]

Last Post!

NobbZ

NobbZ

It’s fully open source.

Check out Exercism · GitHub

Where Next?

Trending in Chat/Questions Top

krisleech
Hey folks, I’ve been using Elixir for over a couple of years (phx, ecto, broadway, oban). For this kind of work you do not tend, in my e...
New
mhindujadheerajsudan
Hi, I’m Dheeraj Sudan from the UK. I’m a software developer and also run a business with my wife Meenu Hinduja. I’m interested in getting...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
webofbits
Squid Mesh is an open source workflow automation runtime for Elixir applications. It is aimed at Phoenix and OTP apps that want to defin...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
kip
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New

We're in Beta

About us Mission Statement