From 013a624eda013c4666edb9c6c4323173f3b6b1be Mon Sep 17 00:00:00 2001 From: Tomas Norre Mikkelsen Date: Thu, 22 Feb 2024 19:54:37 +0100 Subject: [PATCH 1/3] Add Secret Handshake Exercise --- config.json | 8 ++ .../secret-handshake/.docs/instructions.md | 48 ++++++++ .../secret-handshake/.docs/introduction.md | 7 ++ .../secret-handshake/.meta/config.json | 19 +++ .../secret-handshake/.meta/example.php | 24 ++++ .../secret-handshake/.meta/tests.toml | 43 +++++++ .../secret-handshake/SecretHandshake.php | 33 ++++++ .../secret-handshake/SecretHandshakeTest.php | 112 ++++++++++++++++++ 8 files changed, 294 insertions(+) create mode 100644 exercises/practice/secret-handshake/.docs/instructions.md create mode 100644 exercises/practice/secret-handshake/.docs/introduction.md create mode 100644 exercises/practice/secret-handshake/.meta/config.json create mode 100644 exercises/practice/secret-handshake/.meta/example.php create mode 100644 exercises/practice/secret-handshake/.meta/tests.toml create mode 100644 exercises/practice/secret-handshake/SecretHandshake.php create mode 100644 exercises/practice/secret-handshake/SecretHandshakeTest.php diff --git a/config.json b/config.json index c452b416..15c5ce3f 100644 --- a/config.json +++ b/config.json @@ -1180,6 +1180,14 @@ "prerequisites": [], "difficulty": 5 }, + { + "slug": "secret-handshake", + "name": "Secret Handshake", + "uuid": "c0721b15-5bfb-4a2e-92d3-b2e32c096810", + "practices": [], + "prerequisites": [], + "difficulty": 4 + }, { "slug": "circular-buffer", "name": "Circular Buffer", diff --git a/exercises/practice/secret-handshake/.docs/instructions.md b/exercises/practice/secret-handshake/.docs/instructions.md new file mode 100644 index 00000000..d2120b9b --- /dev/null +++ b/exercises/practice/secret-handshake/.docs/instructions.md @@ -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 +~~~~ diff --git a/exercises/practice/secret-handshake/.docs/introduction.md b/exercises/practice/secret-handshake/.docs/introduction.md new file mode 100644 index 00000000..176b92e8 --- /dev/null +++ b/exercises/practice/secret-handshake/.docs/introduction.md @@ -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. diff --git a/exercises/practice/secret-handshake/.meta/config.json b/exercises/practice/secret-handshake/.meta/config.json new file mode 100644 index 00000000..a826a7d3 --- /dev/null +++ b/exercises/practice/secret-handshake/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "tomasnorre" + ], + "files": { + "solution": [ + "SecretHandshake.php" + ], + "test": [ + "SecretHandshakeTest.php" + ], + "example": [ + ".meta/example.php" + ] + }, + "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" +} diff --git a/exercises/practice/secret-handshake/.meta/example.php b/exercises/practice/secret-handshake/.meta/example.php new file mode 100644 index 00000000..322679b7 --- /dev/null +++ b/exercises/practice/secret-handshake/.meta/example.php @@ -0,0 +1,24 @@ +. + * + * To disable strict typing, comment out the directive below. + */ + +declare(strict_types=1); + +class SecretHandshake +{ + public function commands(int $handshake): array + { + throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__)); + } +} diff --git a/exercises/practice/secret-handshake/SecretHandshakeTest.php b/exercises/practice/secret-handshake/SecretHandshakeTest.php new file mode 100644 index 00000000..2a0812d6 --- /dev/null +++ b/exercises/practice/secret-handshake/SecretHandshakeTest.php @@ -0,0 +1,112 @@ +secretHandshake = new SecretHandshake(); + } + + /** + * uuid: b8496fbd-6778-468c-8054-648d03c4bb23 + */ + public function testWinkForOne(): void + { + $this->assertEquals(['wink'], $this->secretHandshake->commands(1)); + } + + /** + * uuid: 83ec6c58-81a9-4fd1-bfaf-0160514fc0e3 + */ + public function testDoubleBlinkForTen(): void + { + $this->assertEquals(['double blink'], $this->secretHandshake->commands(2)); + } + + /** + * uuid: 0e20e466-3519-4134-8082-5639d85fef71 + */ + public function testCloseYourEyesForHundred(): void + { + $this->assertEquals(['close your eyes'], $this->secretHandshake->commands(4)); + } + + /** + * uuid: b339ddbb-88b7-4b7d-9b19-4134030d9ac0 + */ + public function testJumpForThousand(): void + { + $this->assertEquals(['jump'], $this->secretHandshake->commands(8)); + } + + /** + * uuid: 40499fb4-e60c-43d7-8b98-0de3ca44e0eb + */ + public function testCombineTwoActions(): void + { + $this->assertEquals(['wink', 'double blink'], $this->secretHandshake->commands(3)); + } + + /** + * uuid: 9730cdd5-ef27-494b-afd3-5c91ad6c3d9d + */ + public function testReverseTwoActions(): void + { + $this->assertEquals(['double blink', 'wink'], $this->secretHandshake->commands(19)); + } + + /** + * uuid: 0b828205-51ca-45cd-90d5-f2506013f25f + */ + public function testReversingOneActionGivesTheSameAction(): void + { + $this->assertEquals(['jump'], $this->secretHandshake->commands(24)); + } + + /** + * uuid: 9949e2ac-6c9c-4330-b685-2089ab28b05f + */ + public function testReversingNoActionsStillGivesNoActions(): void + { + $this->assertEquals([], $this->secretHandshake->commands(16)); + } + + /** + * uuid: 23fdca98-676b-4848-970d-cfed7be39f81 + */ + public function testAllPossibleActions(): void + { + $this->assertEquals( + ['wink', 'double blink', 'close your eyes', 'jump'], + $this->secretHandshake->commands(15) + ); + } + + /** + * uuid: ae8fe006-d910-4d6f-be00-54b7c3799e79 + */ + public function testReverseAllPossibleActions(): void + { + $this->assertEquals( + ['jump', 'close your eyes', 'double blink', 'wink'], + $this->secretHandshake->commands(31) + ); + } + + /** + * uuid: 3d36da37-b31f-4cdb-a396-d93a2ee1c4a5 + */ + public function testDoNothingForZero(): void + { + $this->assertEquals([], $this->secretHandshake->commands(0)); + } +} From 343833a19437accce6841ca82d538a2288897dee Mon Sep 17 00:00:00 2001 From: Tomas Norre Mikkelsen Date: Fri, 23 Feb 2024 09:49:37 +0100 Subject: [PATCH 2/3] Update exercises/practice/secret-handshake/SecretHandshakeTest.php Co-authored-by: mk-mxp <55182845+mk-mxp@users.noreply.github.com> --- exercises/practice/secret-handshake/SecretHandshakeTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/secret-handshake/SecretHandshakeTest.php b/exercises/practice/secret-handshake/SecretHandshakeTest.php index 2a0812d6..d9224415 100644 --- a/exercises/practice/secret-handshake/SecretHandshakeTest.php +++ b/exercises/practice/secret-handshake/SecretHandshakeTest.php @@ -29,7 +29,7 @@ public function testWinkForOne(): void */ public function testDoubleBlinkForTen(): void { - $this->assertEquals(['double blink'], $this->secretHandshake->commands(2)); + $this->assertEquals(['double blink'], $this->secretHandshake->commands(0b0_0010)); } /** From b8c10df15452fe86b620f53187310fa838e7bdc6 Mon Sep 17 00:00:00 2001 From: Tomas Norre Mikkelsen Date: Fri, 23 Feb 2024 13:57:06 +0100 Subject: [PATCH 3/3] Add binary notations for test input --- exercises/practice/secret-handshake/SecretHandshakeTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/exercises/practice/secret-handshake/SecretHandshakeTest.php b/exercises/practice/secret-handshake/SecretHandshakeTest.php index d9224415..91b32c8c 100644 --- a/exercises/practice/secret-handshake/SecretHandshakeTest.php +++ b/exercises/practice/secret-handshake/SecretHandshakeTest.php @@ -37,7 +37,7 @@ public function testDoubleBlinkForTen(): void */ public function testCloseYourEyesForHundred(): void { - $this->assertEquals(['close your eyes'], $this->secretHandshake->commands(4)); + $this->assertEquals(['close your eyes'], $this->secretHandshake->commands(0b100)); } /** @@ -61,7 +61,7 @@ public function testCombineTwoActions(): void */ public function testReverseTwoActions(): void { - $this->assertEquals(['double blink', 'wink'], $this->secretHandshake->commands(19)); + $this->assertEquals(['double blink', 'wink'], $this->secretHandshake->commands(0b10011)); } /** @@ -107,6 +107,6 @@ public function testReverseAllPossibleActions(): void */ public function testDoNothingForZero(): void { - $this->assertEquals([], $this->secretHandshake->commands(0)); + $this->assertEquals([], $this->secretHandshake->commands(0b0)); } }