This repository has been archived by the owner on May 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add secret-handshake exercise (#150)
- Loading branch information
1 parent
6d3e232
commit 4ae54aa
Showing
9 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
~~~~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"authors": [ | ||
"ErikSchierboom" | ||
], | ||
"files": { | ||
"solution": [ | ||
"src/secret_handshake.cljs" | ||
], | ||
"test": [ | ||
"test/secret_handshake_test.cljs" | ||
], | ||
"example": [ | ||
".meta/src/example.cljs" | ||
] | ||
}, | ||
"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/qt0437047" | ||
} |
16 changes: 16 additions & 0 deletions
16
exercises/practice/secret-handshake/.meta/src/example.cljs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
(ns secret-handshake) | ||
|
||
(def coded-commands | ||
{ | ||
0 #(conj % "wink") | ||
1 #(conj % "double blink") | ||
2 #(conj % "close your eyes") | ||
3 #(conj % "jump") | ||
4 reverse | ||
}) | ||
|
||
(defn- update-commands [code commands [command-nth-bit command]] | ||
(if (bit-test code command-nth-bit) (command commands) commands)) | ||
|
||
(defn commands [code] | ||
(reduce (partial update-commands code) [] coded-commands)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# This is an auto-generated file. Regular comments will be removed when this | ||
# file is regenerated. Regenerating will not touch any manually added keys, | ||
# so comments can be added in 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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{:deps | ||
{org.clojure/clojure {:mvn/version "1.10.1"} | ||
org.clojure/clojurescript {:mvn/version "1.10.773"}} | ||
|
||
:aliases | ||
{:test | ||
{:extra-paths ["test"] | ||
:extra-deps | ||
{olical/cljs-test-runner {:mvn/version "3.8.0"}} | ||
:main-opts ["-m" "cljs-test-runner.main"]}}} |
5 changes: 5 additions & 0 deletions
5
exercises/practice/secret-handshake/src/secret_handshake.cljs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
(ns secret-handshake) | ||
|
||
(defn commands [] ;; <- arglist goes here | ||
;; your code goes here | ||
) |
55 changes: 55 additions & 0 deletions
55
exercises/practice/secret-handshake/test/secret_handshake_test.cljs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
(ns secret-handshake-test | ||
(:require [clojure.test :refer [deftest testing is]] | ||
secret-handshake)) | ||
|
||
(deftest wink | ||
(testing "a wink is returned for binary 1" | ||
(is (= ["wink"] (secret-handshake/commands 1))))) | ||
|
||
(deftest double-blink | ||
(testing "a double blink is returned for a binary 10" | ||
(is (= ["double blink"] (secret-handshake/commands 2))))) | ||
|
||
(deftest close-your-eyes | ||
(testing "a close your eyes is returned for a binary 100" | ||
(is (= ["close your eyes"] (secret-handshake/commands 4))))) | ||
|
||
(deftest jump | ||
(testing "a jump is returned for a binary 1000" | ||
(is (= ["jump"] (secret-handshake/commands 8))))) | ||
|
||
(deftest two-actions | ||
(testing "commands returns multiple actions" | ||
(is (= ["wink" "double blink"] (secret-handshake/commands 3))))) | ||
|
||
(deftest reversing | ||
(testing "giving a binary 10000 reverses actions" | ||
(is (= ["double blink" "wink"] (secret-handshake/commands 19))))) | ||
|
||
(deftest reversing-one-action | ||
(testing "reversing one action returns the same action" | ||
(is (= ["jump"] (secret-handshake/commands 24))))) | ||
|
||
(deftest reverse-nothing | ||
(testing "reversing nothing still gives nothing" | ||
(is (= [] (secret-handshake/commands 16))))) | ||
|
||
(deftest all-actions | ||
(testing "all actions together" | ||
(is (= | ||
["wink" "double blink" "close your eyes" "jump"] | ||
(secret-handshake/commands 15))))) | ||
|
||
(deftest reverse-all-actions | ||
(testing "reversing all actions together" | ||
(is (= | ||
["jump" "close your eyes" "double blink" "wink"] | ||
(secret-handshake/commands 31))))) | ||
|
||
(deftest nothing | ||
(testing "do nothing for 0" | ||
(is (= [] (secret-handshake/commands 0))))) | ||
|
||
(deftest lower-5-bits-zero | ||
(testing "do nothing for high numbers if lower 5 bits not set" | ||
(is (= [] (secret-handshake/commands 32))))) |