Fl4m3Ph03n1x

Fl4m3Ph03n1x

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.

Showing Posts 1 to 10

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…

NobbZ

NobbZ

Yes, its binary/base2.

astory

astory

In the example look at the position of the one, not it’s existence. 11 is not two ones. It is the far right 1, and the second farthest right 1. You do something for every 1 in the binary, depending on it’s position. So you should never have anything doubled.

peerreynders

peerreynders

As far as I can tell

Given a decimal number, convert it

should be “integer 0 <= n <= 31”

should return the array

should be “list”

iex(1)> import Bitwise 
Bitwise
iex(2)> wink = 0b1
1
iex(3)> blink = 0b10
2
iex(4)> close = 0b100
4
iex(5)> jump = 0b1000
8
iex(6)> reverse = 0b10000
1
iex(7)> 
nil
iex(8)> 
nil
iex(9)> (0b11 &&& wink) > 0
true
iex(10)> (0b11 &&& blink) > 0
true
iex(11)> (0b11 &&& close) > 0
false
iex(12)>
NobbZ

NobbZ

Integer? yes. Positive? Yes. Less than 32? No, since modulo arithmetic applies and is even tested AFAIR.

Yes, but the word array comes from a canonical description of the exercise, identical over all languages on exercism that implement this exercise.

peerreynders

peerreynders

My nose got out of joint at “decimal” which I interpret as a numeric value with a fractional portion.

In C it’s easy enough to simply look at everything as a bit pattern for better or worse. But I think a more constrained description would have been helpful.

NobbZ

NobbZ

Well, a decimal number is a number to the base of 10, as opposed to a binary or hexadecimal number which are to the base 2 or 16 respectively. But yes, wording is sub par in that description. I’ll try to find my beginners hat and revise it this week.

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.

NobbZ

NobbZ

None of the actions can happen twice. An action either happens or happens not, and they either happen from highest to lowest or from lowest to highest.

peerreynders

peerreynders

Those are the type of constraints that I like to see explicitly stated, otherwise more time is spent on trying to figure out what the intended problem is rather than solving the problem.

(I realize I’m in no position to complain given this is a volunteer effort - I just find the same to apply in other avenues.)

Where Next? Top

Trending in Chat/Questions Top

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews