Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add practice exercise secret-handshake #697

Merged
merged 1 commit into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,20 @@
],
"difficulty": 5
},
{
"slug": "secret-handshake",
"name": "Secret Handshake",
"uuid": "6c7a96ec-0bfb-4284-bd9f-2aa6989c3bb5",
"practices": [],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for the future, good match for #698

"prerequisites": [
"custom-types",
"pattern-matching",
"maybe",
"lists",
"booleans"
],
"difficulty": 4
},
{
"slug": "sieve",
"name": "Sieve",
Expand Down
48 changes: 48 additions & 0 deletions exercises/practice/secret-handshake/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Instructions

Your task is to convert a number between 1 and 31 to a sequence of actions in the secret handshake.

The sequence of actions is chosen by looking at the rightmost five digits of the number once it's been converted to binary.
Start at the right-most digit and move left.

The actions for each number place are:

```plaintext
00001 = wink
00010 = double blink
00100 = close your eyes
01000 = jump
10000 = Reverse the order of the operations in the secret handshake.
```

Let's use the number `9` as an example:

- 9 in binary is `1001`.
- The digit that is farthest to the right is 1, so the first action is `wink`.
- Going left, the next digit is 0, so there is no double-blink.
- Going left again, the next digit is 0, so you leave your eyes open.
- Going left again, the next digit is 1, so you jump.

That was the last digit, so the final code is:

```plaintext
wink, jump
```

Given the number 26, which is `11010` in binary, we get the following actions:

- double blink
- jump
- reverse actions

The secret handshake for 26 is therefore:

```plaintext
jump, double blink
```

~~~~exercism/note
If you aren't sure what binary is or how it works, check out [this binary tutorial][intro-to-binary].

[intro-to-binary]: https://medium.com/basecs/bits-bytes-building-with-binary-13cb4289aafa
~~~~
7 changes: 7 additions & 0 deletions exercises/practice/secret-handshake/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Introduction

You are starting a secret coding club with some friends and friends-of-friends.
Not everyone knows each other, so you and your friends have decided to create a secret handshake that you can use to recognize that someone is a member.
You don't want anyone who isn't in the know to be able to crack the code.

You've designed the code so that one person says a number between 1 and 31, and the other person turns it into a series of actions.
19 changes: 19 additions & 0 deletions exercises/practice/secret-handshake/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"jiegillet"
],
"files": {
"solution": [
"src/SecretHandshake.elm"
],
"test": [
"tests/Tests.elm"
],
"example": [
".meta/src/SecretHandshake.example.elm"
]
},
"blurb": "Given a decimal number, convert it to the appropriate sequence of events for a secret handshake.",
"source": "Bert, in Mary Poppins",
"source_url": "https://www.imdb.com/title/tt0058331/quotes/?item=qt0437047"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module SecretHandshake exposing (Command(..), commands)

import Bitwise


type Command
= Wink
| DoubleBlink
| CloseYourEyes
| Jump
| Reverse


commandToCode : Command -> Int
commandToCode command =
case command of
Wink ->
1

DoubleBlink ->
2

CloseYourEyes ->
4

Jump ->
8

Reverse ->
16


commandApplies : Int -> Command -> Bool
commandApplies number command =
Bitwise.and number (commandToCode command) > 0


commands : Int -> List Command
commands number =
let
maybeAction command =
if commandApplies number command then
Just command

else
Nothing

actions =
List.filterMap maybeAction [ Wink, DoubleBlink, CloseYourEyes, Jump ]
in
if commandApplies number Reverse then
List.reverse actions

else
actions
43 changes: 43 additions & 0 deletions exercises/practice/secret-handshake/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[b8496fbd-6778-468c-8054-648d03c4bb23]
description = "wink for 1"

[83ec6c58-81a9-4fd1-bfaf-0160514fc0e3]
description = "double blink for 10"

[0e20e466-3519-4134-8082-5639d85fef71]
description = "close your eyes for 100"

[b339ddbb-88b7-4b7d-9b19-4134030d9ac0]
description = "jump for 1000"

[40499fb4-e60c-43d7-8b98-0de3ca44e0eb]
description = "combine two actions"

[9730cdd5-ef27-494b-afd3-5c91ad6c3d9d]
description = "reverse two actions"

[0b828205-51ca-45cd-90d5-f2506013f25f]
description = "reversing one action gives the same action"

[9949e2ac-6c9c-4330-b685-2089ab28b05f]
description = "reversing no actions still gives no actions"

[23fdca98-676b-4848-970d-cfed7be39f81]
description = "all possible actions"

[ae8fe006-d910-4d6f-be00-54b7c3799e79]
description = "reverse all possible actions"

[3d36da37-b31f-4cdb-a396-d93a2ee1c4a5]
description = "do nothing for zero"
29 changes: 29 additions & 0 deletions exercises/practice/secret-handshake/elm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"type": "application",
"source-directories": [
"src"
],
"elm-version": "0.19.1",
"dependencies": {
"direct": {
"elm/core": "1.0.5",
"elm/json": "1.1.3",
"elm/parser": "1.1.0",
"elm/random": "1.0.0",
"elm/regex": "1.0.0",
"elm/time": "1.0.0"
},
"indirect": {}
},
"test-dependencies": {
"direct": {
"elm-explorations/test": "2.1.0",
"rtfeldman/elm-iso8601-date-strings": "1.1.4"
},
"indirect": {
"elm/bytes": "1.0.8",
"elm/html": "1.0.0",
"elm/virtual-dom": "1.0.3"
}
}
}
13 changes: 13 additions & 0 deletions exercises/practice/secret-handshake/src/SecretHandshake.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module SecretHandshake exposing (Command(..), commands)


type Command
= Wink
| DoubleBlink
| CloseYourEyes
| Jump


commands : Int -> List Command
commands number =
Debug.todo "Please implement commands"
66 changes: 66 additions & 0 deletions exercises/practice/secret-handshake/tests/Tests.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
module Tests exposing (tests)

import Expect
import SecretHandshake exposing (Command(..))
import Test exposing (Test, describe, skip, test)


tests : Test
tests =
describe "SecretHandshake"
[ -- skip <|
test "wink for 1" <|
\() ->
SecretHandshake.commands 1
|> Expect.equal [ Wink ]
, skip <|
test "double blink for 10" <|
\() ->
SecretHandshake.commands 2
|> Expect.equal [ DoubleBlink ]
, skip <|
test "close your eyes for 100" <|
\() ->
SecretHandshake.commands 4
|> Expect.equal [ CloseYourEyes ]
, skip <|
test "jump for 1000" <|
\() ->
SecretHandshake.commands 8
|> Expect.equal [ Jump ]
, skip <|
test "combine two actions" <|
\() ->
SecretHandshake.commands 3
|> Expect.equal [ Wink, DoubleBlink ]
, skip <|
test "reverse two actions" <|
\() ->
SecretHandshake.commands 19
|> Expect.equal [ DoubleBlink, Wink ]
, skip <|
test "reversing one action gives the same action" <|
\() ->
SecretHandshake.commands 24
|> Expect.equal [ Jump ]
, skip <|
test "reversing no actions still gives no actions" <|
\() ->
SecretHandshake.commands 16
|> Expect.equal []
, skip <|
test "all possible actions" <|
\() ->
SecretHandshake.commands 15
|> Expect.equal [ Wink, DoubleBlink, CloseYourEyes, Jump ]
, skip <|
test "reverse all possible actions" <|
\() ->
SecretHandshake.commands 31
|> Expect.equal [ Jump, CloseYourEyes, DoubleBlink, Wink ]
, skip <|
test "do nothing for zero" <|
\() ->
SecretHandshake.commands 0
|> Expect.equal []
]