Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of nadiar429

nadiar429's solution

to Hamming in the Objective-C Track

Published at Dec 27 2020 · 0 comments
Test suite

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.

^ ^ ^  ^ ^    ^^

The Hamming distance between these two DNA strands is 7.

Implementation notes

The Hamming distance is only defined for sequences of equal length. This means that based on the definition, each language could deal with getting sequences of equal length differently.


There are two different methods of getting set up to run the tests with Objective-C:

  • Create an Xcode project with a test target which will run the tests.
  • Use the ruby gem objc as a test runner utility.

Both are described in more detail here: http://exercism.io/languages/objective-c

Submitting Exercises

When submitting an exercise, make sure your solution file is in the same directory as the test code.

The submit command will look something like:

exercism submit <path-to-exercism-workspace>/objective-c/hamming/Hamming.m

You can find the Exercism workspace by running exercism debug and looking for the line beginning with Workspace.


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.


#import <XCTest/XCTest.h>

#if __has_include("HammingExample.h")
# import "HammingExample.h"
# else
# import "Hamming.h"


@interface HammingTest : XCTestCase


@implementation HammingTest

- (void)testNoDifferenceBetweenEmptyStrands {
    NSUInteger result = [Hamming compute:@"" against:@""];
    NSUInteger expected = 0;

- (void)testNoDifferenceBetweenIdenticalStrands {
    NSUInteger result = [Hamming compute:@"GGACTGA" against:@"GGACTGA"];
    NSUInteger expected = 0;

- (void)testCompleteHammingDistanceInSmallStrand {
    NSUInteger result = [Hamming compute:@"ACT" against:@"GGA"];
    NSUInteger expected = 3;

- (void)testHammingDistanceInOffByOneStrand {
    NSUInteger result = [Hamming compute:@"GGACGGATTCTG" against:@"AGGACGGATTCT"];
    NSUInteger expected = 9;

- (void)testSmallHammingDistanceInMiddleSomewhere {
    NSUInteger result = [Hamming compute:@"GGACG" against:@"GGTCG"];
    NSUInteger expected = 1;

- (void)testLargerDistance {
    NSUInteger result = [Hamming compute:@"ACCAGGG" against:@"ACTATGG"];
    NSUInteger expected = 2;


Hamming/Hamming Tests/Hamming.h

//  Hamming.h
//  Hamming
//  Created by Nadiar Syaripul on 27/12/20.

#import <Foundation/Foundation.h>


@interface Hamming : NSObject

+ (unsigned int) compute:(NSString *)strand1 against:(NSString *)strand2;



Hamming/Hamming Tests/Hamming.m

//  Hamming.m
//  Hamming
//  Created by Nadiar Syaripul on 27/12/20.

#import "Hamming.h"

@implementation Hamming

+ (unsigned int) compute:(NSString *)strand1 against:(NSString *)strand2 {
    unsigned int diff = 0;
    for (int i = 0; i<[strand1 length]; i++) {
        NSString *a = [strand1 substringWithRange:NSMakeRange(i, 1)];
        NSString *b = [strand2 substringWithRange:NSMakeRange(i, 1)];
        if ([a isNotEqualTo:b]) {
    return diff;


Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other peopleโ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?