@@ -28,41 +28,37 @@ module MinCostStringConversion =
28
28
): array < array < int >> * array < array < Operation >> =
29
29
30
30
let costs =
31
- Array.init ( source.Length + 1 ) ( fun _ -> Array.init ( destination.Length + 1 ) ( fun _ -> None ))
31
+ Array.init ( source.Length + 1 ) ( fun _ -> Array.init ( destination.Length + 1 ) ( fun _ -> 0 ))
32
32
33
33
let ops =
34
- Array.init ( source.Length + 1 ) ( fun _ -> Array.init ( destination.Length + 1 ) ( fun _ -> None))
35
-
36
- costs.[ 0 ].[ 0 ] <- Some 0
37
- ops.[ 0 ].[ 0 ] <- Some ( Operation.Copy 'a' ) // There is no operation to perform, assigning dummy operation to satisfy compiler
38
-
34
+ Array.init ( source.Length + 1 ) ( fun _ -> Array.init ( destination.Length + 1 ) ( fun _ -> Operation.Copy 'a' ))
39
35
40
36
for i = 1 to source.Length do
41
- costs.[ i].[ 0 ] <- Some ( i * deleteCost)
42
- ops.[ i].[ 0 ] <- Some ( Operation.Delete source.[ i - 1 ])
37
+ costs.[ i].[ 0 ] <- i * deleteCost
38
+ ops.[ i].[ 0 ] <- Operation.Delete source.[ i - 1 ]
43
39
44
40
for i = 1 to destination.Length do
45
- costs.[ 0 ].[ i] <- Some ( i * insertCost)
46
- ops.[ 0 ].[ i] <- Some ( Operation.Insert destination.[ i - 1 ])
41
+ costs.[ 0 ].[ i] <- i * insertCost
42
+ ops.[ 0 ].[ i] <- Operation.Insert destination.[ i - 1 ]
47
43
48
44
for i in 1 .. source.Length do
49
45
for j in 1 .. destination.Length do
50
46
if source.[ i - 1 ] = destination.[ j - 1 ] then
51
- costs.[ i].[ j] <- Some ( costs.[ i - 1 ].[ j - 1 ]. Value + copyCost)
52
- ops.[ i].[ j] <- Some ( Operation.Copy ( source.[ i - 1 ]) )
47
+ costs.[ i].[ j] <- costs.[ i - 1 ].[ j - 1 ] + copyCost
48
+ ops.[ i].[ j] <- Operation.Copy ( source.[ i - 1 ])
53
49
else
54
- costs.[ i].[ j] <- Some ( costs.[ i - 1 ].[ j - 1 ]. Value + replaceCost)
55
- ops.[ i].[ j] <- Some ( Operation.Replace ( source.[ i - 1 ], destination.[ j - 1 ]) )
50
+ costs.[ i].[ j] <- costs.[ i - 1 ].[ j - 1 ] + replaceCost
51
+ ops.[ i].[ j] <- Operation.Replace ( source.[ i - 1 ], destination.[ j - 1 ])
56
52
57
- if costs.[ i - 1 ].[ j]. Value + deleteCost < costs.[ i].[ j]. Value then
58
- costs.[ i].[ j] <- Some ( costs.[ i - 1 ].[ j]. Value + deleteCost)
59
- ops.[ i].[ j] <- Some ( Operation.Delete ( source.[ i - 1 ]) )
53
+ if costs.[ i - 1 ].[ j] + deleteCost < costs.[ i].[ j] then
54
+ costs.[ i].[ j] <- costs.[ i - 1 ].[ j] + deleteCost
55
+ ops.[ i].[ j] <- Operation.Delete ( source.[ i - 1 ])
60
56
61
- if costs.[ i].[ j - 1 ]. Value + insertCost < costs.[ i].[ j]. Value then
62
- costs.[ i].[ j] <- Some ( costs.[ i].[ j - 1 ]. Value + insertCost)
63
- ops.[ i].[ j] <- Some ( Operation.Insert destination.[ j - 1 ])
57
+ if costs.[ i].[ j - 1 ] + insertCost < costs.[ i].[ j] then
58
+ costs.[ i].[ j] <- costs.[ i].[ j - 1 ] + insertCost
59
+ ops.[ i].[ j] <- Operation.Insert destination.[ j - 1 ]
64
60
65
- costs |> Array.map ( Array.map Option.get ) , ops |> Array.map ( Array.map Option.get )
61
+ costs, ops
66
62
67
63
let rec assembleTransformation ( ops : array < array < Operation >>, i : int , j : int ): array < Operation > =
68
64
printfn $" i={i},j={j},%A {ops}"
0 commit comments