Skip to content

Instantly share code, notes, and snippets.

@naomilwx
naomilwx / gist:d83a4982b1aa4f8ba89b
Created March 24, 2015 17:55
String alignment lab written in OCaml
(*Background: Given 2 strings, compute an alignment that maximises their similarity score.
Each of the two sequences being compared, x and y, is modified by inserting spaces at arbitrary
locations, including at either end, so that the resulting sequences ,x0 and y0,
are of the same length k but do not have a space in the
same position i.e. for no position j are both x0[j] and y0[j] a space
*)
(* Task 1 *)
(*
Helper function to calculate the alignment score of two strings, starting from a given position
def extended_gcd(aa, bb):
lastremainder, remainder = abs(aa), abs(bb)
x, lastx, y, lasty = 0, 1, 1, 0
while remainder:
lastremainder, (quotient, remainder) = remainder, divmod(lastremainder, remainder)
x, lastx = lastx - quotient*x, x
y, lasty = lasty - quotient*y, y
return lastremainder, lastx * (-1 if aa < 0 else 1), lasty * (-1 if bb < 0 else 1)
def modinv(a, m):