Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2eba87b

Browse files
author
programmiri
committed
Add excercise Hamming with solution for it
1 parent eca0d75 commit 2eba87b

File tree

7 files changed

+4427
-0
lines changed

7 files changed

+4427
-0
lines changed

‎hamming/.eslintrc‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"root": true,
3+
"parser": "babel-eslint",
4+
"parserOptions": {
5+
"ecmaVersion": 7,
6+
"sourceType": "module"
7+
},
8+
"env": {
9+
"es6": true,
10+
"node": true,
11+
"jest": true
12+
},
13+
"extends": [
14+
"eslint:recommended",
15+
"plugin:import/errors",
16+
"plugin:import/warnings"
17+
],
18+
"rules": {
19+
"linebreak-style": "off",
20+
21+
"import/extensions": "off",
22+
"import/no-default-export": "off",
23+
"import/no-unresolved": "off",
24+
"import/prefer-default-export": "off"
25+
}
26+
}

‎hamming/README.md‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Hamming
2+
3+
Calculate the Hamming Distance between two DNA strands.
4+
5+
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!
6+
7+
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".
8+
9+
We read DNA using the letters C,A,G and T. Two strands might look like this:
10+
11+
GAGCCTACTAACGGGAT
12+
CATCGTAATGACGGCCT
13+
^ ^ ^ ^ ^ ^^
14+
15+
They have 7 differences, and therefore the Hamming Distance is 7.
16+
17+
The Hamming Distance is useful for lots of things in science, not just biology, so it's a nice phrase to be familiar with :)
18+
19+
# Implementation notes
20+
21+
The Hamming distance is only defined for sequences of equal length, so
22+
an attempt to calculate it between sequences of different lengths should
23+
not work. The general handling of this situation (e.g., raising an
24+
exception vs returning a special value) may differ between languages.
25+
26+
## Setup
27+
28+
Go through the setup instructions for Javascript to install the necessary
29+
dependencies:
30+
31+
[https://exercism.io/tracks/javascript/installation](https://exercism.io/tracks/javascript/installation)
32+
33+
## Requirements
34+
35+
Install assignment dependencies:
36+
37+
```bash
38+
$ npm install
39+
```
40+
41+
## Making the test suite pass
42+
43+
Execute the tests with:
44+
45+
```bash
46+
$ npm test
47+
```
48+
49+
In the test suites all tests but the first have been skipped.
50+
51+
Once you get a test passing, you can enable the next one by changing `xtest` to
52+
`test`.
53+
54+
## Source
55+
56+
The Calculating Point Mutations problem at Rosalind [http://rosalind.info/problems/hamm/](http://rosalind.info/problems/hamm/)
57+
58+
## Submitting Incomplete Solutions
59+
60+
It's possible to submit an incomplete solution so you can see how others have
61+
completed the exercise.

‎hamming/babel.config.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
presets: [
3+
[
4+
'@babel/env',
5+
{
6+
targets: {
7+
node: 'current',
8+
},
9+
useBuiltIns: false,
10+
},
11+
12+
],
13+
],
14+
};

‎hamming/hamming.js‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function checkForErrors(leftStrand, rightStrand) {
2+
if (leftStrand.length !== rightStrand.length) {
3+
if (leftStrand.length === 0) {
4+
throw Error('left strand must not be empty');
5+
}
6+
if (rightStrand.length === 0) {
7+
throw Error('right strand must not be empty');
8+
}
9+
throw Error('left and right strands must be of equal length');
10+
}
11+
}
12+
13+
export const compute = (leftStrand, rightStrand) => {
14+
const dnaStrand = leftStrand.split('');
15+
const dnaStrandToCompare = rightStrand.split('');
16+
17+
checkForErrors(dnaStrand, dnaStrandToCompare);
18+
19+
return dnaStrand.reduce((acc, curr, index) => {
20+
if (curr !== dnaStrandToCompare[index]) {
21+
acc++;
22+
}
23+
return acc;
24+
}, 0);
25+
};

‎hamming/hamming.spec.js‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { compute } from './hamming';
2+
3+
describe('Hamming', () => {
4+
test('empty strands', () => {
5+
expect(compute('', '')).toEqual(0);
6+
});
7+
8+
test('single letter identical strands', () => {
9+
expect(compute('A', 'A')).toEqual(0);
10+
});
11+
12+
test('single letter different strands', () => {
13+
expect(compute('G', 'T')).toEqual(1);
14+
});
15+
16+
test('long identical strands', () => {
17+
expect(compute('GGACTGAAATCTG', 'GGACTGAAATCTG')).toEqual(0);
18+
});
19+
20+
test('long different strands', () => {
21+
expect(compute('GGACGGATTCTG', 'AGGACGGATTCT')).toEqual(9);
22+
});
23+
24+
test('disallow first strand longer', () => {
25+
expect(() => compute('AATG', 'AAA')).toThrow(
26+
new Error('left and right strands must be of equal length')
27+
);
28+
});
29+
30+
test('disallow second strand longer', () => {
31+
expect(() => compute('ATA', 'AGTG')).toThrow(
32+
new Error('left and right strands must be of equal length')
33+
);
34+
});
35+
36+
test('disallow left empty strand', () => {
37+
expect(() => compute('', 'G')).toThrow(
38+
new Error('left strand must not be empty')
39+
);
40+
});
41+
42+
test('disallow right empty strand', () => {
43+
expect(() => compute('G', '')).toThrow(
44+
new Error('right strand must not be empty')
45+
);
46+
});
47+
});

‎hamming/package.json‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "exercism-javascript",
3+
"description": "Exercism exercises in Javascript.",
4+
"author": "Katrina Owen",
5+
"private": true,
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/exercism/javascript"
9+
},
10+
"devDependencies": {
11+
"@babel/cli": "^7.5.5",
12+
"@babel/core": "^7.5.5",
13+
"@babel/preset-env": "^7.5.5",
14+
"@types/jest": "^24.0.16",
15+
"@types/node": "^12.6.8",
16+
"babel-eslint": "^10.0.2",
17+
"babel-jest": "^24.8.0",
18+
"eslint": "^6.1.0",
19+
"eslint-plugin-import": "^2.18.2",
20+
"jest": "^24.8.0"
21+
},
22+
"jest": {
23+
"modulePathIgnorePatterns": [
24+
"package.json"
25+
]
26+
},
27+
"scripts": {
28+
"test": "jest --no-cache ./*",
29+
"watch": "jest --no-cache --watch ./*",
30+
"lint": "eslint .",
31+
"lint-test": "eslint . && jest --no-cache ./* "
32+
},
33+
"license": "MIT",
34+
"dependencies": {}
35+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /