Skip to content

Commit 439711f

Browse files
committed
Working on the hamming challenge
1 parent 4583c38 commit 439711f

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed

hamming/.exercism/metadata.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"track":"python","exercise":"hamming","id":"4fc966f7d0b24cce92d5c34337dc0cb6","url":"https://exercism.io/my/solutions/4fc966f7d0b24cce92d5c34337dc0cb6","handle":"sinanata","is_requester":true,"auto_approve":false}

hamming/README.md

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
27+
## Exception messages
28+
29+
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to
30+
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not
31+
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
32+
a message.
33+
34+
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
35+
`raise Exception`, you should write:
36+
37+
```python
38+
raise Exception("Meaningful message indicating the source of the error")
39+
```
40+
41+
## Running the tests
42+
43+
To run the tests, run `pytest hamming_test.py`
44+
45+
Alternatively, you can tell Python to run the pytest module:
46+
`python -m pytest hamming_test.py`
47+
48+
### Common `pytest` options
49+
50+
- `-v` : enable verbose output
51+
- `-x` : stop running tests on first failure
52+
- `--ff` : run failures from previous test before running other test cases
53+
54+
For other options, see `python -m pytest -h`
55+
56+
## Submitting Exercises
57+
58+
Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/hamming` directory.
59+
60+
You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`.
61+
62+
For more detailed information about running tests, code style and linting,
63+
please see [Running the Tests](http://exercism.io/tracks/python/tests).
64+
65+
## Source
66+
67+
The Calculating Point Mutations problem at Rosalind [http://rosalind.info/problems/hamm/](http://rosalind.info/problems/hamm/)
68+
69+
## Submitting Incomplete Solutions
70+
71+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

hamming/hamming.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def distance(strand_a, strand_b):
2+
3+
if (len(strand_a) == len(strand_b)):
4+
return sum ( strand_a[i] != strand_b[i] for i in range(len(strand_a)) )
5+
else:
6+
self.exception = r".+"
7+
8+
pass

hamming/hamming.pyc

640 Bytes
Binary file not shown.

hamming/hamming_test.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import unittest
2+
3+
from hamming import distance
4+
5+
# Tests adapted from `problem-specifications//canonical-data.json` @ v2.3.0
6+
7+
8+
class HammingTest(unittest.TestCase):
9+
def test_empty_strands(self):
10+
self.assertEqual(distance("", ""), 0)
11+
12+
def test_single_letter_identical_strands(self):
13+
self.assertEqual(distance("A", "A"), 0)
14+
15+
def test_single_letter_different_strands(self):
16+
self.assertEqual(distance("G", "T"), 1)
17+
18+
def test_long_identical_strands(self):
19+
self.assertEqual(distance("GGACTGAAATCTG", "GGACTGAAATCTG"), 0)
20+
21+
def test_long_different_strands(self):
22+
self.assertEqual(distance("GGACGGATTCTG", "AGGACGGATTCT"), 9)
23+
24+
def test_disallow_first_strand_longer(self):
25+
with self.assertRaisesWithMessage(ValueError):
26+
distance("AATG", "AAA")
27+
28+
def test_disallow_second_strand_longer(self):
29+
with self.assertRaisesWithMessage(ValueError):
30+
distance("ATA", "AGTG")
31+
32+
def test_disallow_left_empty_strand(self):
33+
with self.assertRaisesWithMessage(ValueError):
34+
distance("", "G")
35+
36+
def test_disallow_right_empty_strand(self):
37+
with self.assertRaisesWithMessage(ValueError):
38+
distance("G", "")
39+
40+
# Utility functions
41+
def assertRaisesWithMessage(self, exception):
42+
return self.assertRaisesRegex(exception, r".+")
43+
44+
45+
if __name__ == "__main__":
46+
unittest.main()

0 commit comments

Comments
 (0)