diff options
author | Andinus <andinus@nand.sh> | 2021-09-04 23:07:40 +0530 |
---|---|---|
committer | Andinus <andinus@nand.sh> | 2021-09-04 23:07:40 +0530 |
commit | 2f65d04253b50737f2313be5ee88b662c9cde19e (patch) | |
tree | 7fdf44764dfddab43373a21720b0d88175a50312 /javascript | |
parent | 90e12a35bc1814e4d20ce14c84ef7405e15b1e04 (diff) | |
download | exercism-2f65d04253b50737f2313be5ee88b662c9cde19e.tar.gz |
JS: Hamming: Add solution
Diffstat (limited to 'javascript')
-rw-r--r-- | javascript/hamming/.eslintrc | 14 | ||||
-rw-r--r-- | javascript/hamming/.npmrc | 1 | ||||
-rw-r--r-- | javascript/hamming/HELP.md | 73 | ||||
-rw-r--r-- | javascript/hamming/LICENSE | 21 | ||||
-rw-r--r-- | javascript/hamming/README.md | 50 | ||||
-rw-r--r-- | javascript/hamming/babel.config.js | 15 | ||||
-rw-r--r-- | javascript/hamming/hamming.js | 22 | ||||
-rw-r--r-- | javascript/hamming/hamming.spec.js | 47 | ||||
-rw-r--r-- | javascript/hamming/package.json | 31 |
9 files changed, 274 insertions, 0 deletions
diff --git a/javascript/hamming/.eslintrc b/javascript/hamming/.eslintrc new file mode 100644 index 0000000..1d44460 --- /dev/null +++ b/javascript/hamming/.eslintrc @@ -0,0 +1,14 @@ +{ + "root": true, + "extends": "@exercism/eslint-config-javascript", + "env": { + "jest": true + }, + "overrides": [ + { + "files": [".meta/proof.ci.js", ".meta/exemplar.js", "*.spec.js"], + "excludedFiles": ["custom.spec.js"], + "extends": "@exercism/eslint-config-javascript/maintainers" + } + ] +} diff --git a/javascript/hamming/.npmrc b/javascript/hamming/.npmrc new file mode 100644 index 0000000..d26df80 --- /dev/null +++ b/javascript/hamming/.npmrc @@ -0,0 +1 @@ +audit=false diff --git a/javascript/hamming/HELP.md b/javascript/hamming/HELP.md new file mode 100644 index 0000000..8f02e93 --- /dev/null +++ b/javascript/hamming/HELP.md @@ -0,0 +1,73 @@ +# Help + +## Running the tests + +## Setup + +Go through the setup [instructions for JavaScript][docs-exercism-javascript] to install the necessary dependencies. + +## Requirements + +Install assignment dependencies: + +```shell +# Using npm +npm install + +# Alternatively using yarn +yarn +``` + +## Making the test suite pass + +All exercises come with a test suite to help you validate your solution before submitting. +You can execute these tests by opening a command prompt in the exercise's directory, and then running: + +```bash +# Using npm +npm test + +# Alternatively using yarn +yarn test +``` + +In some test suites all tests but the first have been skipped. + +Once you get a test passing, you can enable the next one by changing `xtest` to `test`. + +## Writing custom tests + +If you wish to write additional, custom, tests, create a new file `custom.spec.js`, and submit it with your solution together with the new file: + +```shell +exercism submit numbers.js custom.spec.js +``` + +[docs-exercism-javascript]: https://exercism.org/docs/tracks/javascript/installation + +## Submitting your solution + +You can submit your solution using the `exercism submit hamming.js` 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 [JavaScript track's documentation](https://exercism.org/docs/tracks/javascript) +- [Exercism's support channel on gitter](https://gitter.im/exercism/support) +- 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: + +- [Gitter](https://gitter.im/exercism/support) is Exercism's Gitter room; go here to get support and ask questions if you face any issues with downloading or submitting your exercises. +- [/r/javascript](https://www.reddit.com/r/javascript) is the Javascript subreddit. +- [StackOverflow](https://stackoverflow.com/questions/tagged/javascript+exercism) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions. +- [Github issue tracker](https://github.com/exercism/javascript/issues) is where we track our development and maintainance of Javascript exercises in exercism. But if none of the above links help you, feel free to post an issue here. \ No newline at end of file diff --git a/javascript/hamming/LICENSE b/javascript/hamming/LICENSE new file mode 100644 index 0000000..90e73be --- /dev/null +++ b/javascript/hamming/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Exercism + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/javascript/hamming/README.md b/javascript/hamming/README.md new file mode 100644 index 0000000..35fd96e --- /dev/null +++ b/javascript/hamming/README.md @@ -0,0 +1,50 @@ +# Hamming + +Welcome to Hamming on Exercism's JavaScript Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +Calculate the Hamming Distance between two DNA strands. + +Your body is made up of cells that contain DNA. Those cells regularly wear out and need replacing, which they achieve by dividing into daughter cells. In fact, the average human body experiences about 10 quadrillion cell divisions in a lifetime! + +When cells divide, their DNA replicates too. Sometimes during this process mistakes happen and single pieces of DNA get encoded with the incorrect information. If we compare two strands of DNA and count the differences between them we can see how many mistakes occurred. This is known as the "Hamming Distance". + +We read DNA using the letters C,A,G and T. Two strands might look like this: + + GAGCCTACTAACGGGAT + CATCGTAATGACGGCCT + ^ ^ ^ ^ ^ ^^ + +They have 7 differences, and therefore the Hamming Distance is 7. + +The Hamming Distance is useful for lots of things in science, not just biology, so it's a nice phrase to be familiar with :) + + +The Hamming distance is only defined for sequences of equal length, so +an attempt to calculate it between sequences of different lengths should +not work. The general handling of this situation (e.g., raising an +exception vs returning a special value) may differ between languages. + +## Source + +### Created by + +- @rchavarria + +### Contributed to by + +- @ankorGH +- @draalger +- @gabriel376 +- @kytrinyx +- @matthewmorgan +- @ryanplusplus +- @serixscorpio +- @SleeplessByte +- @tejasbubane + +### Based on + +The Calculating Point Mutations problem at Rosalind - http://rosalind.info/problems/hamm/ \ No newline at end of file diff --git a/javascript/hamming/babel.config.js b/javascript/hamming/babel.config.js new file mode 100644 index 0000000..5f1ec60 --- /dev/null +++ b/javascript/hamming/babel.config.js @@ -0,0 +1,15 @@ +module.exports = { + presets: [ + [ + '@babel/preset-env', + { + targets: { + node: 'current', + }, + useBuiltIns: 'entry', + corejs: '3.17', + }, + ], + ], + plugins: ['@babel/plugin-syntax-bigint'], +}; diff --git a/javascript/hamming/hamming.js b/javascript/hamming/hamming.js new file mode 100644 index 0000000..4d0908d --- /dev/null +++ b/javascript/hamming/hamming.js @@ -0,0 +1,22 @@ +'use strict'; + +export const compute = (strand1, strand2) => { + if (strand1 === "" && strand2 === "") + return 0; + + if (strand1 === "") + throw new Error('left strand must not be empty'); + + if (strand2 === "") + throw new Error('right strand must not be empty'); + + if (strand1.length !== strand2.length) + throw new Error('left and right strands must be of equal length'); + + let distance = 0; + for (let idx = 0; idx < strand1.length; idx++) + if (strand1[idx] !== strand2[idx]) + distance++; + + return distance; +}; diff --git a/javascript/hamming/hamming.spec.js b/javascript/hamming/hamming.spec.js new file mode 100644 index 0000000..de445d3 --- /dev/null +++ b/javascript/hamming/hamming.spec.js @@ -0,0 +1,47 @@ +import { compute } from './hamming'; + +describe('Hamming', () => { + test('empty strands', () => { + expect(compute('', '')).toEqual(0); + }); + + test('single letter identical strands', () => { + expect(compute('A', 'A')).toEqual(0); + }); + + test('single letter different strands', () => { + expect(compute('G', 'T')).toEqual(1); + }); + + test('long identical strands', () => { + expect(compute('GGACTGAAATCTG', 'GGACTGAAATCTG')).toEqual(0); + }); + + test('long different strands', () => { + expect(compute('GGACGGATTCTG', 'AGGACGGATTCT')).toEqual(9); + }); + + test('disallow first strand longer', () => { + expect(() => compute('AATG', 'AAA')).toThrow( + new Error('left and right strands must be of equal length') + ); + }); + + test('disallow second strand longer', () => { + expect(() => compute('ATA', 'AGTG')).toThrow( + new Error('left and right strands must be of equal length') + ); + }); + + test('disallow left empty strand', () => { + expect(() => compute('', 'G')).toThrow( + new Error('left strand must not be empty') + ); + }); + + test('disallow right empty strand', () => { + expect(() => compute('G', '')).toThrow( + new Error('right strand must not be empty') + ); + }); +}); diff --git a/javascript/hamming/package.json b/javascript/hamming/package.json new file mode 100644 index 0000000..5dcca8c --- /dev/null +++ b/javascript/hamming/package.json @@ -0,0 +1,31 @@ +{ + "name": "@exercism/javascript-hamming", + "description": "Exercism exercises in Javascript.", + "author": "Katrina Owen", + "private": true, + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/exercism/javascript", + "directory": "exercises/practice/hamming" + }, + "devDependencies": { + "@babel/cli": "^7.15.4", + "@babel/core": "^7.15.4", + "@babel/plugin-syntax-bigint": "^7.8.3", + "@babel/preset-env": "^7.15.0", + "@exercism/eslint-config-javascript": "^0.4.0", + "@types/jest": "^27.0.1", + "@types/node": "^16.7.10", + "babel-jest": "^27.1.0", + "core-js": "^3.17.2", + "eslint": "^7.32.0", + "jest": "^26.6.3" + }, + "dependencies": {}, + "scripts": { + "test": "jest --no-cache ./*", + "watch": "jest --no-cache --watch ./*", + "lint": "eslint ." + } +} |