Created
January 15, 2016 13:07
-
-
Save hugowetterberg/9013830ad0eb19e3074c to your computer and use it in GitHub Desktop.
Revisions
-
hugowetterberg created this gist
Jan 15, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,63 @@ function fixup(a, b) { console.log(a, '=>', b); var add = []; var remove = []; for (var i = 0; i < a.length; i++) { if (b.indexOf(a[i]) == -1) { remove.push(a[i]); } } for (var i = 0; i < b.length; i++) { if (a.indexOf(b[i]) == -1) { add.push(b[i]); } } var fin = a; remove.forEach((c) => { fin = drop(fin, fin.indexOf(c)); }) add.forEach((c) => { fin = insert(fin, b.indexOf(c), c); }) for (var i = 0; i < fin.length; i++) { if (fin[i] !== b[i]) { fin = insert(fin, i, b[i]); } } console.log('---'); return fin; } function insert(s, idx, c) { var p = s.indexOf(c); if (p !== -1) { s = s.substring(0, p) + s.substring(p+1); } console.log('insert ->', c, idx); return s.substring(0, idx) + c + s.substring(idx); } function drop(s, idx) { console.log('drop ->', s[idx], idx); return s.substring(0, idx) + s.substring(idx+1); } var mutations = [ ["abcdefgh", "bcdefghi"], ["abcdefgh", "bdcefghi"], ["abcdefgh", "abcidefgh"], ["abcdefgh", "defghabc"] ]; mutations.forEach((m) => { var fin = fixup(m[0], m[1]); if (fin === m[1]) { console.log('OK\n---\n'); } else { console.log('FAIL\n---\n'); } }); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ abcdefgh => bcdefghi drop -> a 0 insert -> i 7 --- OK --- abcdefgh => bdcefghi drop -> a 0 insert -> i 7 insert -> d 1 --- OK --- abcdefgh => abcidefgh insert -> i 3 --- OK --- abcdefgh => defghabc insert -> d 0 insert -> e 1 insert -> f 2 insert -> g 3 insert -> h 4 --- OK ---