Skip to content

Commit facd179

Browse files
authored
Add new practice exercise: secret-handshake (#123)
1 parent 97bd70c commit facd179

File tree

11 files changed

+255
-0
lines changed

11 files changed

+255
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,14 @@
226226
"prerequisites": [],
227227
"difficulty": 5
228228
},
229+
{
230+
"slug": "secret-handshake",
231+
"name": "Secret Handshake",
232+
"uuid": "2b10f220-4a5f-437f-b982-58914baa9ec1",
233+
"practices": [],
234+
"prerequisites": [],
235+
"difficulty": 5
236+
},
229237
{
230238
"slug": "swift-scheduling",
231239
"name": "Swift Scheduling",
Lines changed: 48 additions & 0 deletions
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+
~~~~
Lines changed: 7 additions & 0 deletions
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.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"jimmytty"
4+
],
5+
"files": {
6+
"solution": [
7+
"secret-handshake.sql"
8+
],
9+
"test": [
10+
"secret-handshake_test.sql"
11+
],
12+
"example": [
13+
".meta/example.sql"
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+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
UPDATE "secret-handshake"
2+
SET result = (
3+
SELECT COALESCE(GROUP_CONCAT(j.value, ', '), '')
4+
FROM (
5+
SELECT
6+
JSON_ARRAY(
7+
IIF((number >> 3) & 1, 'jump' , NULL),
8+
IIF((number >> 2) & 1, 'close your eyes', NULL),
9+
IIF((number >> 1) & 1, 'double blink' , NULL),
10+
IIF((number >> 0) & 1, 'wink' , NULL)
11+
) AS commands
12+
), JSON_EACH(commands) j
13+
WHERE j.value NOTNULL
14+
)
15+
WHERE (number >> 4) & 1 = 1
16+
;
17+
18+
UPDATE "secret-handshake"
19+
SET result = (
20+
SELECT COALESCE(GROUP_CONCAT(j.value, ', '), '')
21+
FROM (
22+
SELECT
23+
JSON_ARRAY(
24+
IIF((number >> 0) & 1, 'wink' , NULL),
25+
IIF((number >> 1) & 1, 'double blink' , NULL),
26+
IIF((number >> 2) & 1, 'close your eyes', NULL),
27+
IIF((number >> 3) & 1, 'jump' , NULL)
28+
) AS commands
29+
), JSON_EACH(commands) j
30+
WHERE j.value NOTNULL
31+
)
32+
WHERE (number >> 4) & 1 = 0
33+
;
Lines changed: 43 additions & 0 deletions
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"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
DROP TABLE IF EXISTS "secret-handshake";
2+
CREATE TABLE "secret-handshake" (
3+
number INTEGER NOT NULL,
4+
result TEXT
5+
);
6+
7+
.mode csv
8+
.import ./data.csv "secret-handshake"
9+
10+
UPDATE "secret-handshake" SET result = NULL;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
DROP TABLE IF EXISTS tests;
2+
CREATE TABLE IF NOT EXISTS tests (
3+
-- uuid and description are taken from the test.toml file
4+
uuid TEXT PRIMARY KEY,
5+
description TEXT NOT NULL,
6+
-- The following section is needed by the online test-runner
7+
status TEXT DEFAULT 'fail',
8+
message TEXT,
9+
output TEXT,
10+
test_code TEXT,
11+
task_id INTEGER DEFAULT NULL,
12+
-- Here are columns for the actual tests
13+
number INTEGER NOT NULL,
14+
expected TEXT NOT NULL
15+
);
16+
17+
INSERT INTO tests (uuid, description, number, expected)
18+
VALUES
19+
('b8496fbd-6778-468c-8054-648d03c4bb23', 'wink for 1', 1, 'wink'),
20+
('83ec6c58-81a9-4fd1-bfaf-0160514fc0e3', 'double blink for 10', 2, 'double blink'),
21+
('0e20e466-3519-4134-8082-5639d85fef71', 'close your eyes for 100', 4, 'close your eyes'),
22+
('b339ddbb-88b7-4b7d-9b19-4134030d9ac0', 'jump for 1000', 8, 'jump'),
23+
('40499fb4-e60c-43d7-8b98-0de3ca44e0eb', 'combine two actions', 3, 'wink, double blink'),
24+
('9730cdd5-ef27-494b-afd3-5c91ad6c3d9d', 'reverse two actions', 19, 'double blink, wink'),
25+
('0b828205-51ca-45cd-90d5-f2506013f25f', 'reversing one action gives the same action', 24, 'jump'),
26+
('9949e2ac-6c9c-4330-b685-2089ab28b05f', 'reversing no actions still gives no actions', 16, ''),
27+
('23fdca98-676b-4848-970d-cfed7be39f81', 'all possible actions', 15, 'wink, double blink, close your eyes, jump'),
28+
('ae8fe006-d910-4d6f-be00-54b7c3799e79', 'reverse all possible actions', 31, 'jump, close your eyes, double blink, wink'),
29+
('3d36da37-b31f-4cdb-a396-d93a2ee1c4a5', 'do nothing for zero', 0, '');
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
1,""
2+
2,""
3+
4,""
4+
8,""
5+
3,""
6+
19,""
7+
24,""
8+
16,""
9+
15,""
10+
31,""
11+
0,""
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Schema:
2+
-- CREATE TABLE "secret-handshake" (
3+
-- number INTEGER NOT NULL,
4+
-- result TEXT
5+
-- );
6+
--
7+
-- Task: update secret-handshake table and set result column based on the number.

0 commit comments

Comments
 (0)