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.

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"]

Where Next?

Popular in Chat/Questions Top

davearonson
I am looking for smallish problems to solve, using Elixir concepts beyond the basic syntax and concepts of Elixir as a language, such as ...
New
Yoga
Or at least, in the works? All I can find are bits and pieces but not a single project from start to finish. Including things like i18n,...
New
pdgonzalez872
Do we have a list of academic/research papers: about Elixir/Erlang? that use Elixir/Erlang? about the Beam? If so, could you please po...
New
Fl4m3Ph03n1x
Background Hey guys, recently I bought a book on TDD that I am reading. The books is really nice and has some really juicy things on arch...
New
roshan
Hi everyone, I’m looking for a book on Phoenix server hosting / deployment like the following books for Rails, Docker for Rails Develop...
New
phykos
In Ruby, which is very similar to Elixir I do this: def test yield end test do puts("sup there") end Here, the yield keyword will be...
New
asfand
I already created an Elixir Phoenix app for learning purpose. In this app students of our collage will create profiles, and will chat wit...
New
AstonJ
It’s been a while since we asked this - I’m sure others (especially newcomers to the language) will be interested to hear how you’ve all ...
New
marciol
Hey, I have very restricted resources and time so I’m trying to understand the best way to learn Liveview in terms of cost/time. The Pra...
New
Nopp
Hello there, i have a lot to read and to learn, but are there some books, that are focussing on how i “should” plan the architecture of ...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement