@@ -28,41 +28,37 @@ module MinCostStringConversion =
2828 ): array < array < int >> * array < array < Operation >> =
2929
3030 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 ))
3232
3333 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' ))
3935
4036 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 ]
4339
4440 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 ]
4743
4844 for i in 1 .. source.Length do
4945 for j in 1 .. destination.Length do
5046 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 ])
5349 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 ])
5652
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 ])
6056
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 ]
6460
65- costs |> Array.map ( Array.map Option.get ) , ops |> Array.map ( Array.map Option.get )
61+ costs, ops
6662
6763 let rec assembleTransformation ( ops : array < array < Operation >>, i : int , j : int ): array < Operation > =
6864 printfn $" i={i},j={j},%A {ops}"
0 commit comments