Skip to content

Commit 0516ebb

Browse files
authored
Add secret handshake exercise (#1626)
1 parent 89459bf commit 0516ebb

File tree

9 files changed

+199
-0
lines changed

9 files changed

+199
-0
lines changed

config.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,6 +1466,15 @@
14661466
"unsafe"
14671467
]
14681468
},
1469+
{
1470+
"slug": "secret-handshake",
1471+
"name": "Secret Handshake",
1472+
"uuid": "8c044530-9deb-4ff7-a638-98d6c05ebbb8",
1473+
"practices": [],
1474+
"prerequisites": [],
1475+
"difficulty": 4,
1476+
"topics": []
1477+
},
14691478
{
14701479
"slug": "hexadecimal",
14711480
"name": "Hexadecimal",
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Instructions
2+
3+
> There are 10 types of people in the world: Those who understand
4+
> binary, and those who don't.
5+
6+
You and your fellow cohort of those in the "know" when it comes to binary decide to come up with a secret "handshake".
7+
8+
```text
9+
00001 = wink
10+
00010 = double blink
11+
00100 = close your eyes
12+
01000 = jump
13+
14+
10000 = Reverse the order of the operations in the secret handshake.
15+
```
16+
17+
Given a decimal number, convert it to the appropriate sequence of events for a secret handshake.
18+
19+
Here's a couple of examples:
20+
21+
Given the decimal input 3, the function would return the array ["wink", "double blink"] because the decimal number 3 is 2+1 in powers of two and thus `11` in binary.
22+
23+
Let's now examine the input 19 which is 16+2+1 in powers of two and thus `10011` in binary.
24+
Recalling that the addition of 16 (`10000` in binary) reverses an array and that we already know what array is returned given input 3, the array returned for input 19 is ["double blink", "wink"].
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Generated by Cargo
2+
# will have compiled files and executables
3+
/target/
4+
**/*.rs.bk
5+
6+
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
7+
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
8+
Cargo.lock
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"authors": [
3+
"dem4ron"
4+
],
5+
"files": {
6+
"solution": [
7+
"src/lib.rs",
8+
"Cargo.toml"
9+
],
10+
"test": [
11+
"tests/secret-handshake.rs"
12+
],
13+
"example": [
14+
".meta/example.rs"
15+
]
16+
},
17+
"blurb": "Given a decimal number, convert it to the appropriate sequence of events for a secret handshake.",
18+
"source": "Bert, in Mary Poppins",
19+
"source_url": "https://www.imdb.com/title/tt0058331/quotes/qt0437047"
20+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const ACTIONS: [&str; 4] = ["wink", "double blink", "close your eyes", "jump"];
2+
3+
pub fn actions(n: u8) -> Vec<&'static str> {
4+
let result: Vec<&str> = ACTIONS
5+
.iter()
6+
.enumerate()
7+
.filter(|(i, _)| (1u8 << *i) & n != 0)
8+
.map(|(_, &c)| c)
9+
.collect();
10+
11+
if n & 16 != 0 {
12+
result.into_iter().rev().collect()
13+
} else {
14+
result
15+
}
16+
}
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
edition = "2021"
3+
name = "secret-handshake"
4+
version = "1.1.0"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub fn actions(n: u8) -> Vec<&'static str> {
2+
unimplemented!("What is the secret handshake for {n}?")
3+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
use secret_handshake::*;
2+
3+
#[test]
4+
fn wink_for_1() {
5+
assert_eq!(actions(1), vec!["wink"])
6+
}
7+
8+
#[test]
9+
#[ignore]
10+
fn double_blink_for_10() {
11+
assert_eq!(actions(2), vec!["double blink"])
12+
}
13+
14+
#[test]
15+
#[ignore]
16+
fn close_your_eyes_for_100() {
17+
assert_eq!(actions(4), vec!["close your eyes"])
18+
}
19+
20+
#[test]
21+
#[ignore]
22+
fn jump_for_1000() {
23+
assert_eq!(actions(8), vec!["jump"])
24+
}
25+
26+
#[test]
27+
#[ignore]
28+
fn combine_two_actions() {
29+
assert_eq!(actions(3), vec!["wink", "double blink"])
30+
}
31+
32+
#[test]
33+
#[ignore]
34+
fn reverse_two_actions() {
35+
assert_eq!(actions(19), vec!["double blink", "wink"])
36+
}
37+
38+
#[test]
39+
#[ignore]
40+
fn reversing_one_action_gives_the_same_action() {
41+
assert_eq!(actions(24), vec!["jump"])
42+
}
43+
44+
#[test]
45+
#[ignore]
46+
fn reversing_no_actions_still_gives_no_actions() {
47+
assert_eq!(actions(16), Vec::<&'static str>::new())
48+
}
49+
50+
#[test]
51+
#[ignore]
52+
fn all_possible_actions() {
53+
assert_eq!(
54+
actions(15),
55+
vec!["wink", "double blink", "close your eyes", "jump"]
56+
)
57+
}
58+
59+
#[test]
60+
#[ignore]
61+
fn reverse_all_possible_actions() {
62+
assert_eq!(
63+
actions(31),
64+
vec!["jump", "close your eyes", "double blink", "wink"]
65+
)
66+
}
67+
68+
#[test]
69+
#[ignore]
70+
fn do_nothing_for_zero() {
71+
assert_eq!(actions(0), Vec::<&'static str>::new())
72+
}

0 commit comments

Comments
 (0)