Secret handshake

This commit is contained in:
2022-11-09 17:22:32 +01:00
parent 1c52c91e7f
commit d438eb37b9
13 changed files with 16351 additions and 0 deletions
+13
View File
@@ -0,0 +1,13 @@
!.meta
# Protected or generated
.git
.vscode
# When using npm
node_modules/*
# Configuration files
.eslintrc.cjs
babel.config.cjs
jest.config.cjs
+41
View File
@@ -0,0 +1,41 @@
module.exports = {
root: true,
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json'],
},
overrides: [
// Student provided files
{
files: ['*.ts'],
excludedFiles: ['.meta/proof.ci.ts', '.meta/exemplar.ts', '*.test.ts'],
extends: '@exercism/eslint-config-typescript',
},
// Exercism given tests
{
files: ['*.test.ts'],
excludedFiles: ['custom.test.ts'],
env: {
jest: true,
},
extends: '@exercism/eslint-config-typescript/maintainers',
},
// Student provided tests
{
files: ['custom.test.ts'],
env: {
jest: true,
},
extends: '@exercism/eslint-config-typescript',
},
// Exercism provided files
{
files: ['.meta/proof.ci.ts', '.meta/exemplar.ts', '*.test.ts'],
excludedFiles: ['custom.test.ts'],
extends: '@exercism/eslint-config-typescript/maintainers',
rules: {
"@typescript-eslint/semi": 0,
},
},
],
}
@@ -0,0 +1,12 @@
{
"blurb": "Given a decimal number, convert it to the appropriate sequence of events for a secret handshake.",
"authors": ["CRivasGomez"],
"contributors": ["masters3d", "SleeplessByte"],
"files": {
"solution": ["secret-handshake.ts"],
"test": ["secret-handshake.test.ts"],
"example": [".meta/proof.ci.ts"]
},
"source": "Bert, in Mary Poppins",
"source_url": "http://www.imdb.com/title/tt0058331/quotes/qt0437047"
}
@@ -0,0 +1 @@
{"track":"typescript","exercise":"secret-handshake","id":"d699480d1ea844e484c6c9527851b3b7","url":"https://exercism.org/tracks/typescript/exercises/secret-handshake","handle":"briemens","is_requester":true,"auto_approve":false}
+44
View File
@@ -0,0 +1,44 @@
# Help
## Running the tests
Execute the tests with:
```bash
$ yarn test
```
## Skipped tests
In the test suites all tests but the first have been skipped.
Once you get a test passing, you can enable the next one by changing `xit` to
`it`.
## Submitting your solution
You can submit your solution using the `exercism submit secret-handshake.ts` 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 [TypeScript track's documentation](https://exercism.org/docs/tracks/typescript)
- [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:
- [TypeScript QuickStart](https://www.typescriptlang.org/docs/handbook/release-notes/overview.html)
- [ECMAScript 2015 Language Specification](https://www.ecma-international.org/wp-content/uploads/ECMA-262_6th_edition_june_2015.pdf) (pdf)
- [Mozilla JavaScript Reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference)
- [/r/typescript](https://www.reddit.com/r/typescript) is the TypeScript subreddit.
- [StackOverflow](https://stackoverflow.com/questions/tagged/typescript) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.
+49
View File
@@ -0,0 +1,49 @@
# Secret Handshake
Welcome to Secret Handshake on Exercism's TypeScript Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
> There are 10 types of people in the world: Those who understand
> binary, and those who don't.
You and your fellow cohort of those in the "know" when it comes to
binary decide to come up with a secret "handshake".
```text
1 = wink
10 = double blink
100 = close your eyes
1000 = jump
10000 = Reverse the order of the operations in the secret handshake.
```
Given a decimal number, convert it to the appropriate sequence of events for a secret handshake.
Here's a couple of examples:
Given the input 3, the function would return the array
["wink", "double blink"] because 3 is 11 in binary.
Given the input 19, the function would return the array
["double blink", "wink"] because 19 is 10011 in binary.
Notice that the addition of 16 (10000 in binary)
has caused the array to be reversed.
## Source
### Created by
- @CRivasGomez
### Contributed to by
- @masters3d
- @SleeplessByte
### Based on
Bert, in Mary Poppins - http://www.imdb.com/title/tt0058331/quotes/qt0437047
@@ -0,0 +1,4 @@
module.exports = {
presets: ['@exercism/babel-preset-typescript'],
plugins: [],
}
@@ -0,0 +1,19 @@
module.exports = {
verbose: true,
projects: ['<rootDir>'],
testMatch: [
'**/__tests__/**/*.[jt]s?(x)',
'**/test/**/*.[jt]s?(x)',
'**/?(*.)+(spec|test).[jt]s?(x)',
],
testPathIgnorePatterns: [
'/(?:production_)?node_modules/',
'.d.ts$',
'<rootDir>/test/fixtures',
'<rootDir>/test/helpers',
'__mocks__',
],
transform: {
'^.+\\.[jt]sx?$': 'babel-jest',
},
}
File diff suppressed because it is too large Load Diff
+31
View File
@@ -0,0 +1,31 @@
{
"name": "@exercism/typescript-secret-handshake",
"version": "1.0.0",
"description": "Exercism exercises in Typescript.",
"private": true,
"repository": {
"type": "git",
"url": "https://github.com/exercism/typescript"
},
"type": "module",
"engines": {
"node": "^14.13.1 || >=16.0.0"
},
"devDependencies": {
"@exercism/babel-preset-typescript": "^0.1.0",
"@exercism/eslint-config-typescript": "^0.4.1",
"@types/jest": "^27.4.0",
"@types/node": "^16.11.24",
"babel-jest": "^27.5.1",
"core-js": "^3.21.0",
"eslint": "^8.9.0",
"jest": "^27.5.1",
"typescript": "^4.5.4"
},
"scripts": {
"test": "yarn lint:types && jest --no-cache",
"lint": "yarn lint:types && yarn lint:ci",
"lint:types": "yarn tsc --noEmit -p .",
"lint:ci": "eslint . --ext .tsx,.ts"
}
}
@@ -0,0 +1,59 @@
import { commands } from "./secret-handshake";
describe("Secret Handshake", () => {
describe("Create A Handshake For A Number", () => {
it("wink for 1", () => {
expect(commands(1)).toEqual(["wink"]);
});
it("double blink for 10", () => {
expect(commands(2)).toEqual(["double blink"]);
});
it("close your eyes for 100", () => {
expect(commands(4)).toEqual(["close your eyes"]);
});
it("jump for 1000", () => {
expect(commands(8)).toEqual(["jump"]);
});
it("combine two actions", () => {
expect(commands(3)).toEqual(["wink", "double blink"]);
});
it("reverse two actions", () => {
expect(commands(19)).toEqual(["double blink", "wink"]);
});
it("reversing one action gives the same action", () => {
expect(commands(24)).toEqual(["jump"]);
});
it("reversing no actions still gives no actions", () => {
expect(commands(16)).toEqual([]);
});
it("all possible actions", () => {
expect(commands(15)).toEqual([
"wink",
"double blink",
"close your eyes",
"jump",
]);
});
it("reverse all possible actions", () => {
expect(commands(31)).toEqual([
"jump",
"close your eyes",
"double blink",
"wink",
]);
});
it("do nothing for zero", () => {
expect(commands(0 as 1)).toEqual([]);
});
});
});
@@ -0,0 +1,54 @@
const responses = ["wink", "double blink", "close your eyes", "jump"] as const;
export function commands(secret: Secret): Response[] {
if (secret < 1 || secret > 31) {
return [];
}
const result = secret
.toString(2)
.split("")
.reverse()
.map((s, i) => (s === "1" ? responses[i] : null))
.filter(Boolean);
if (secret >= 16) {
return result.reverse() as Response[];
}
return result as Response[];
}
type Secret =
| 1
| 2
| 3
| 4
| 5
| 6
| 7
| 8
| 9
| 10
| 11
| 12
| 13
| 14
| 15
| 16
| 17
| 18
| 19
| 20
| 21
| 22
| 23
| 24
| 25
| 26
| 27
| 28
| 29
| 30
| 31;
type Response = typeof responses[number];
+28
View File
@@ -0,0 +1,28 @@
{
"display": "Configuration for Exercism TypeScript Exercises",
"compilerOptions": {
// Allows you to use the newest syntax, and have access to console.log
// https://www.typescriptlang.org/tsconfig#lib
"lib": ["ESNEXT", "dom"],
// Make sure typescript is configured to output ESM
// https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c#how-can-i-make-my-typescript-project-output-esm
"module": "ES2020",
// Since this project is using babel, TypeScript may target something very
// high, and babel will make sure it runs on your local Node version.
// https://babeljs.io/docs/en/
"target": "ESNext", // ESLint doesn't support this yet: "es2022",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
// Because we'll be using babel: ensure that Babel can safely transpile
// files in the TypeScript project.
//
// https://babeljs.io/docs/en/babel-plugin-transform-typescript/#caveats
"isolatedModules": true
},
"include": ["*.ts", "*.tsx", ".meta/*.ts", ".meta/*.tsx"],
"exclude": ["node_modules"]
}