Skip to content

Commit a79ce42

Browse files
authored
add secret-handshake (#271)
1 parent 4938204 commit a79ce42

File tree

8 files changed

+211
-0
lines changed

8 files changed

+211
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,14 @@
243243
"transforming"
244244
]
245245
},
246+
{
247+
"slug": "secret-handshake",
248+
"name": "Secret Handshake",
249+
"uuid": "7d762bca-0aac-4ba9-8035-157a93ab5df5",
250+
"practices": [],
251+
"prerequisites": [],
252+
"difficulty": 2
253+
},
246254
{
247255
"slug": "square-root",
248256
"name": "Square Root",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Instructions
2+
3+
Your task is to convert a number between 1 and 31 to a sequence of actions in the secret handshake.
4+
5+
The sequence of actions is chosen by looking at the rightmost five digits of the number once it's been converted to binary.
6+
Start at the right-most digit and move left.
7+
8+
The actions for each number place are:
9+
10+
```plaintext
11+
00001 = wink
12+
00010 = double blink
13+
00100 = close your eyes
14+
01000 = jump
15+
10000 = Reverse the order of the operations in the secret handshake.
16+
```
17+
18+
Let's use the number `9` as an example:
19+
20+
- 9 in binary is `1001`.
21+
- The digit that is farthest to the right is 1, so the first action is `wink`.
22+
- Going left, the next digit is 0, so there is no double-blink.
23+
- Going left again, the next digit is 0, so you leave your eyes open.
24+
- Going left again, the next digit is 1, so you jump.
25+
26+
That was the last digit, so the final code is:
27+
28+
```plaintext
29+
wink, jump
30+
```
31+
32+
Given the number 26, which is `11010` in binary, we get the following actions:
33+
34+
- double blink
35+
- jump
36+
- reverse actions
37+
38+
The secret handshake for 26 is therefore:
39+
40+
```plaintext
41+
jump, double blink
42+
```
43+
44+
~~~~exercism/note
45+
If you aren't sure what binary is or how it works, check out [this binary tutorial][intro-to-binary].
46+
47+
[intro-to-binary]: https://medium.com/basecs/bits-bytes-building-with-binary-13cb4289aafa
48+
~~~~
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Introduction
2+
3+
You are starting a secret coding club with some friends and friends-of-friends.
4+
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.
5+
You don't want anyone who isn't in the know to be able to crack the code.
6+
7+
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"source/secret_handshake.d"
8+
],
9+
"test": [
10+
"source/secret_handshake.d"
11+
],
12+
"example": [
13+
"example/secret_handshake.d"
14+
]
15+
},
16+
"blurb": "Given a decimal number, convert it to the appropriate sequence of events for a secret handshake.",
17+
"source": "Bert, in Mary Poppins",
18+
"source_url": "https://www.imdb.com/title/tt0058331/quotes/?item=qt0437047"
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[b8496fbd-6778-468c-8054-648d03c4bb23]
13+
description = "wink for 1"
14+
15+
[83ec6c58-81a9-4fd1-bfaf-0160514fc0e3]
16+
description = "double blink for 10"
17+
18+
[0e20e466-3519-4134-8082-5639d85fef71]
19+
description = "close your eyes for 100"
20+
21+
[b339ddbb-88b7-4b7d-9b19-4134030d9ac0]
22+
description = "jump for 1000"
23+
24+
[40499fb4-e60c-43d7-8b98-0de3ca44e0eb]
25+
description = "combine two actions"
26+
27+
[9730cdd5-ef27-494b-afd3-5c91ad6c3d9d]
28+
description = "reverse two actions"
29+
30+
[0b828205-51ca-45cd-90d5-f2506013f25f]
31+
description = "reversing one action gives the same action"
32+
33+
[9949e2ac-6c9c-4330-b685-2089ab28b05f]
34+
description = "reversing no actions still gives no actions"
35+
36+
[23fdca98-676b-4848-970d-cfed7be39f81]
37+
description = "all possible actions"
38+
39+
[ae8fe006-d910-4d6f-be00-54b7c3799e79]
40+
description = "reverse all possible actions"
41+
42+
[3d36da37-b31f-4cdb-a396-d93a2ee1c4a5]
43+
description = "do nothing for zero"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name "secret-handshake"
2+
buildRequirements "disallowDeprecations"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module secret_handshake;
2+
3+
import std.algorithm;
4+
5+
pure string[] commands(immutable int number)
6+
{
7+
string[] actions;
8+
9+
if (number & 1)
10+
{
11+
actions ~= "wink";
12+
}
13+
14+
if (number & 2)
15+
{
16+
actions ~= "double blink";
17+
}
18+
19+
if (number & 4)
20+
{
21+
actions ~= "close your eyes";
22+
}
23+
24+
if (number & 8)
25+
{
26+
actions ~= "jump";
27+
}
28+
29+
if (number & 16)
30+
{
31+
actions = actions.reverse;
32+
}
33+
34+
return actions;
35+
}
36+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
module secret_handshake;
2+
3+
pure string[] commands(immutable int number)
4+
{
5+
// implement this function
6+
}
7+
8+
unittest
9+
{
10+
immutable int allTestsEnabled = 0;
11+
12+
// Wink for 1
13+
assert(commands(1) == ["wink"]);
14+
15+
static if (allTestsEnabled)
16+
{
17+
// Double blink for 10
18+
assert(commands(2) == ["double blink"]);
19+
20+
// Close your eyes for 100
21+
assert(commands(4) == ["close your eyes"]);
22+
23+
// Jump for 1000
24+
assert(commands(8) == ["jump"]);
25+
26+
// Combine two actions
27+
assert(commands(3) == ["wink", "double blink"]);
28+
29+
// Reverse two actions
30+
assert(commands(19) == ["double blink", "wink"]);
31+
32+
// Reversing one action gives the same action
33+
assert(commands(24) == ["jump"]);
34+
35+
// Reversing no actions still gives no actions
36+
assert(commands(16) == []);
37+
38+
// All possible actions
39+
assert(commands(15) == ["wink", "double blink", "close your eyes", "jump"]);
40+
41+
// Reverse all possible actions
42+
assert(commands(31) == ["jump", "close your eyes", "double blink", "wink"]);
43+
44+
// Do nothing for zero
45+
assert(commands(0) == []);
46+
}
47+
}
48+

0 commit comments

Comments
 (0)