Skip to content

Commit

Permalink
🎯 feat: secret handshake
Browse files Browse the repository at this point in the history
  • Loading branch information
LuCCoelho committed Oct 20, 2023
1 parent 8e3e45e commit 8e8f43b
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 0 deletions.
22 changes: 22 additions & 0 deletions dart/secret-handshake/.exercism/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"authors": [
"amscotti"
],
"contributors": [
"kytrinyx"
],
"files": {
"solution": [
"lib/secret_handshake.dart"
],
"test": [
"test/secret_handshake_test.dart"
],
"example": [
".meta/lib/example.dart"
]
},
"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"
}
1 change: 1 addition & 0 deletions dart/secret-handshake/.exercism/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"track":"dart","exercise":"secret-handshake","id":"57141d8875f54a0485c53f855253ce6a","url":"https://exercism.org/tracks/dart/exercises/secret-handshake","handle":"LuCCoelho","is_requester":true,"auto_approve":false}
38 changes: 38 additions & 0 deletions dart/secret-handshake/HELP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Help

## Running the tests

To run the tests:

```sh
$ dart test
```

## Submitting your solution

You can submit your solution using the `exercism submit lib/secret_handshake.dart` command.
This command will upload your solution to the Exercism website and print the solution page's URL.

It's possible to submit an incomplete solution which allows you to:

- See how others have completed the exercise
- Request help from a mentor

## Need to get help?

If you'd like help solving the exercise, check the following pages:

- The [Dart track's documentation](https://exercism.org/docs/tracks/dart)
- The [Dart track's programming category on the forum](https://forum.exercism.org/c/programming/dart)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)

Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.

To get help if you're having trouble, you can use one of the following resources:

- [Dart API Documentation](https://api.dart.dev/)
- [Dart Gitter Chat](https://gitter.im/dart-lang/home)
- [Community Information](https://www.dart.dev/community)
- [/r/dartlang](https://www.reddit.com/r/dartlang) is the Dart subreddit.
- [StackOverflow](https://stackoverflow.com/questions/tagged/dart) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.
75 changes: 75 additions & 0 deletions dart/secret-handshake/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Secret Handshake

Welcome to Secret Handshake on Exercism's Dart Track.
If you need help running the tests or submitting your code, check out `HELP.md`.

## 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.

## 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
~~~~

## Source

### Created by

- @amscotti

### Contributed to by

- @kytrinyx

### Based on

Bert, in Mary Poppins - https://www.imdb.com/title/tt0058331/quotes/?item=qt0437047
15 changes: 15 additions & 0 deletions dart/secret-handshake/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
analyzer:
errors:
unused_element: error
unused_import: error
unused_local_variable: error
dead_code: error

linter:
rules:
# Error Rules
- avoid_relative_lib_imports
- avoid_types_as_parameter_names
- literal_only_boolean_expressions
- no_adjacent_strings_in_list
- valid_regexps
36 changes: 36 additions & 0 deletions dart/secret-handshake/lib/secret_handshake.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class SecretHandshake {
List<String> commands(int number) {
List<String> commands = [];
String numberBinary = number.toRadixString(2);

while (numberBinary.length < 5) {
numberBinary = '0' + numberBinary;
}

if (numberBinary[4] == '1') {
commands.add('wink');
}

if (numberBinary[3] == '1') {
commands.add('double blink');
}

if (numberBinary[2] == '1') {
commands.add('close your eyes');
}

if (numberBinary[1] == '1') {
commands.add('jump');
}

if (numberBinary[0] == '1') {
List<String> commandsReverse = [];
for (int i = commands.length - 1; i > -1; i--) {
commandsReverse.add(commands[i]);
}
return commandsReverse;
}

return commands;
}
}
5 changes: 5 additions & 0 deletions dart/secret-handshake/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: 'secret_handshake'
environment:
sdk: '>=2.18.0 <3.0.0'
dev_dependencies:
test: '<2.0.0'
63 changes: 63 additions & 0 deletions dart/secret-handshake/test/secret_handshake_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import 'package:secret_handshake/secret_handshake.dart';
import 'package:test/test.dart';

void main() {
final secretHandshake = SecretHandshake();

group('SecretHandshake', () {
test('wink for 1', () {
final result = secretHandshake.commands(1);
expect(result, equals(<String>['wink']));
}, skip: false);

test('double blink for 10', () {
final result = secretHandshake.commands(2);
expect(result, equals(<String>['double blink']));
}, skip: false);

test('close your eyes for 100', () {
final result = secretHandshake.commands(4);
expect(result, equals(<String>['close your eyes']));
}, skip: false);

test('jump for 1000', () {
final result = secretHandshake.commands(8);
expect(result, equals(<String>['jump']));
}, skip: false);

test('combine two actions', () {
final result = secretHandshake.commands(3);
expect(result, equals(<String>['wink', 'double blink']));
}, skip: false);

test('reverse two actions', () {
final result = secretHandshake.commands(19);
expect(result, equals(<String>['double blink', 'wink']));
}, skip: false);

test('reversing one action gives the same action', () {
final result = secretHandshake.commands(24);
expect(result, equals(<String>['jump']));
}, skip: false);

test('reversing no actions still gives no actions', () {
final result = secretHandshake.commands(16);
expect(result, equals(<String>[]));
}, skip: false);

test('all possible actions', () {
final result = secretHandshake.commands(15);
expect(result, equals(<String>['wink', 'double blink', 'close your eyes', 'jump']));
}, skip: false);

test('reverse all possible actions', () {
final result = secretHandshake.commands(31);
expect(result, equals(<String>['jump', 'close your eyes', 'double blink', 'wink']));
}, skip: false);

test('do nothing for zero', () {
final result = secretHandshake.commands(0);
expect(result, equals(<String>[]));
}, skip: false);
});
}

0 comments on commit 8e8f43b

Please sign in to comment.