File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ using System ;
2+
3+ public class Hamming
4+ {
5+ public static int Compute ( string firstStrand , string secondStrand )
6+ {
7+ return new Hamming ( firstStrand , secondStrand ) . Distance ( ) ;
8+ }
9+
10+ private string firstStrand ;
11+ private string secondStrand ;
12+
13+ public Hamming ( string firstStrand , string secondStrand )
14+ {
15+ this . firstStrand = firstStrand ;
16+ this . secondStrand = secondStrand ;
17+ }
18+
19+ public int Distance ( )
20+ {
21+ char [ ] firstStrandChars = firstStrand . ToCharArray ( ) ;
22+ char [ ] secondStrandChars = secondStrand . ToCharArray ( ) ;
23+
24+ int difference = 0 ;
25+
26+ for ( int i = 0 ; i < CommonDistance ( ) ; i ++ )
27+ {
28+ if ( firstStrandChars [ i ] != secondStrandChars [ i ] ) { difference ++ ; }
29+ }
30+
31+ return difference ;
32+ }
33+
34+ private int CommonDistance ( )
35+ {
36+ return Math . Min ( firstStrand . Length , secondStrand . Length ) ;
37+ }
38+ }
Original file line number Diff line number Diff line change 1+ using NUnit . Framework ;
2+
3+ [ TestFixture ]
4+ public class HammingTest
5+ {
6+ [ Test ]
7+ public void NoDifferenceBetweenEmptyStrands ( )
8+ {
9+ Assert . AreEqual ( 0 , Hamming . Compute ( "" , "" ) ) ;
10+ }
11+
12+ [ Test ]
13+ public void NoDifferenceBetweenIdenticalStrands ( )
14+ {
15+ Assert . AreEqual ( 0 , Hamming . Compute ( "GGACTGA" , "GGACTGA" ) ) ;
16+ }
17+
18+ [ Test ]
19+ public void CompleteHammingDistanceInSmallStrand ( )
20+ {
21+ Assert . AreEqual ( 3 , Hamming . Compute ( "ACT" , "GGA" ) ) ;
22+ }
23+
24+ [ Test ]
25+ public void HammingDistanceInOffByOneStrand ( )
26+ {
27+ Assert . AreEqual ( 9 , Hamming . Compute ( "GGACGGATTCTG" , "AGGACGGATTCT" ) ) ;
28+ }
29+
30+ [ Test ]
31+ public void SmallingHammingDistanceInMiddleSomewhere ( )
32+ {
33+ Assert . AreEqual ( 1 , Hamming . Compute ( "GGACG" , "GGTCG" ) ) ;
34+ }
35+
36+ [ Test ]
37+ public void LargerDistance ( )
38+ {
39+ Assert . AreEqual ( 2 , Hamming . Compute ( "ACCAGGG" , "ACTATGG" ) ) ;
40+ }
41+
42+ [ Test ]
43+ public void IgnoresExtraLengthOnOtherStrandWhenLonger ( )
44+ {
45+ Assert . AreEqual ( 3 , Hamming . Compute ( "AAACTAGGGG" , "AGGCTAGCGGTAGGAC" ) ) ;
46+ }
47+
48+ [ Test ]
49+ public void IgnoresExtraLengthOnOriginalStrandWhenLonger ( )
50+ {
51+ Assert . AreEqual ( 5 , Hamming . Compute ( "GACTACGGACAGGGTAGGGAAT" , "GACATCGCACACC" ) ) ;
52+ }
53+ }
You can’t perform that action at this time.
0 commit comments