# PatrickMcSweeny's solution

## to Hamming in the Ruby Track

Published at Jul 18 2018 · 0 comments
Instructions
Test suite
Solution

Calculate the Hamming difference between two DNA strands.

A mutation is simply a mistake that occurs during the creation or copying of a nucleic acid, in particular DNA. Because nucleic acids are vital to cellular functions, mutations tend to cause a ripple effect throughout the cell. Although mutations are technically mistakes, a very rare mutation may equip the cell with a beneficial attribute. In fact, the macro effects of evolution are attributable by the accumulated result of beneficial microscopic mutations over many generations.

The simplest and most common type of nucleic acid mutation is a point mutation, which replaces one base with another at a single nucleotide.

By counting the number of differences between two homologous DNA strands taken from different genomes with a common ancestor, we get a measure of the minimum number of point mutations that could have occurred on the evolutionary path between the two strands.

This is called the 'Hamming distance'.

It is found by comparing two DNA strands and counting how many of the nucleotides are different from their equivalent in the other string.

``````GAGCCTACTAACGGGAT
CATCGTAATGACGGCCT
^ ^ ^  ^ ^    ^^
``````

The Hamming distance between these two DNA strands is 7.

# Implementation notes

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.

For installation and learning resources, refer to the Ruby resources page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

``````gem install minitest
``````

If you would like color output, you can `require 'minitest/pride'` in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

``````ruby hamming_test.rb
``````

To include color from the command line:

``````ruby -r minitest/pride hamming_test.rb
``````

## Source

The Calculating Point Mutations problem at Rosalind http://rosalind.info/problems/hamm/

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### hamming_test.rb

``````require 'minitest/autorun'
require_relative 'hamming'

# Common test data version: 2.2.0 4c453c8
class HammingTest < Minitest::Test
def test_empty_strands
# skip
assert_equal 0, Hamming.compute('', '')
end

def test_single_letter_identical_strands
skip
assert_equal 0, Hamming.compute('A', 'A')
end

def test_single_letter_different_strands
skip
assert_equal 1, Hamming.compute('G', 'T')
end

def test_long_identical_strands
skip
assert_equal 0, Hamming.compute('GGACTGAAATCTG', 'GGACTGAAATCTG')
end

def test_long_different_strands
skip
assert_equal 9, Hamming.compute('GGACGGATTCTG', 'AGGACGGATTCT')
end

def test_disallow_first_strand_longer
skip
assert_raises(ArgumentError) do
Hamming.compute('AATG', 'AAA')
end
end

def test_disallow_second_strand_longer
skip
assert_raises(ArgumentError) do
Hamming.compute('ATA', 'AGTG')
end
end
end``````
``````class Hamming
def self.compute(strand1, strand2)
raise ArgumentError unless strand1.length == strand2.length
(0..strand1.length).count do |i|
strand1[i] != strand2[i]
end
end
end``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.8342029,"math_prob":0.6624784,"size":3726,"snap":"2021-31-2021-39","text_gpt3_token_len":867,"char_repetition_ratio":0.12251478,"word_repetition_ratio":0.0,"special_character_ratio":0.20316693,"punctuation_ratio":0.1251981,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95152223,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-07-26T19:14:37Z\",\"WARC-Record-ID\":\"<urn:uuid:f7db36d1-2ece-422b-95c7-02990c6bb202>\",\"Content-Length\":\"18541\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:8f65e962-0076-460d-bef2-9d9caca9aef1>\",\"WARC-Concurrent-To\":\"<urn:uuid:d509c5f9-3498-4256-92e1-801ca856b0a2>\",\"WARC-IP-Address\":\"99.80.111.177\",\"WARC-Target-URI\":\"https://exercism.io/tracks/ruby/exercises/hamming/solutions/119a1185c8b140c2a236eb7624bc7897\",\"WARC-Payload-Digest\":\"sha1:RY5RAIDDUCTECJ6CZDM23HBCCW67AVVF\",\"WARC-Block-Digest\":\"sha1:HAR7WXZUI44FWGLWC73YYECENLQRUQYX\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-31/CC-MAIN-2021-31_segments_1627046152144.92_warc_CC-MAIN-20210726183622-20210726213622-00014.warc.gz\"}"}