lpvm
Exercism secred_handshake help list
I’ve built the following code (reverse not implemented) to solve the secret_handshake problem at exercism.io.
But the first test fails..
test Create a handshake for a number wink for 1 (SecretHandshakeTest)
secret_handshake_test.exs:13
Assertion with == failed
code: assert SecretHandshake.commands(1) == ["wink"]
left: ["wink", nil]
right: ["wink"]
stacktrace:
secret_handshake_test.exs:14: (test)
There’s a ["wink", nil] instead of a ["wink"] for the 1 case, and I can’t figure out why this is happening.
defmodule SecretHandshake do
@doc """
Determine the actions of a secret handshake based on the binary
representation of the given `code`.
If the following bits are set, include the corresponding action in your list
of commands, in order from lowest to highest.
1 = wink
10 = double blink
100 = close your eyes
1000 = jump
10000 = Reverse the order of the operations in the secret handshake
"""
@spec commands(code :: integer) :: list(String.t())
def commands(code) do
in_binary = decimalToBinary code
to_reverse = true and rem(code, 10000)
commands = []
commands = if div(in_binary, 1000) == 1, do: [getAction(1000)] ++ commands
commands = if div(in_binary, 100) == 1, do: [getAction(100)] ++ commands
commands = if div(in_binary, 10) == 1, do: [getAction(10)] ++ commands
commands = if rem(in_binary, 2) == 1, do: [getAction(1)] ++ commands
end
defp getAction(dec) do
case dec do
1 -> "wink"
10 -> "double blink"
100 -> "close your eyes"
1000 -> "jump"
end
end
defp decimalToBinary(dec) do
dec
|> Integer.to_string(2)
|> String.to_integer
end
end
Marked As Solved
NobbZ
Each of this lines will return nil from its implicit else branch.
I am not sure though, why your result is ["wink", nil], it should be ["wink" | nil].
You should try to do some other approach by creating a list of tuples, where the first element is the bit that has to be set, the other is the word to put in the command. Then you simply fold over that list.
Something like this:
[{0, "wink"}, {1, "double blink"}, {2, "close your eyes"}, {3, "jump"}]
|> Enum.fold([], fn {bit, cmd}, acc ->
if bit_set?(code, bit), do: [cmd | acc], else: acc
end)
|> Enum.reverse()
bit_set?/2 is easily implementable using the Bitwise module.
1
Popular in Questions
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
Other popular topics
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
Hello guys,
I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








