16 lines
583 B
Plaintext
16 lines
583 B
Plaintext
// HammingDistance: how many positions of two sequences of the same length
|
|
// hold different letters. The INVARIANT is what makes the ENSURE provable:
|
|
// after I positions, at most I mismatches can have been counted.
|
|
FUNCTION HammingDistance(S : STRING, T : STRING) RETURNS INTEGER
|
|
REQUIRE LENGTH(S) == LENGTH(T)
|
|
ENSURE 0 <= RESULT AND RESULT <= LENGTH(S)
|
|
DECLARE Count : INTEGER <- 0
|
|
FOR I <- 0 TO LENGTH(S)
|
|
INVARIANT 0 <= Count AND Count <= I
|
|
IF S[I] != T[I] THEN
|
|
Count <- Count + 1
|
|
ENDIF
|
|
NEXT I
|
|
RETURN Count
|
|
ENDFUNCTION
|