Advertisement
Guest User

Subseq function from video

a guest
Feb 19th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. // r/ihadatime
  3. //
  4. //
  5. //
  6. // Sorry for my English xd
  7. //
  8. //
  9. // Made by @ncon
  10. //
  11. // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  12. // WARNING: Letters are probably case sensetive
  13. // (depends on how javascript operator == works on characters)
  14. // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  15. //
  16. // HOWTO: How to solve this kind of problems
  17. // 1. Try to understand the problem
  18. // 2. DON'T start from code, never, you will get the same thing as this guy did, analyze the problem
  19. // 3. Find how this can be solved, figure out algorithm, you can try
  20. //             to play with this on paper, for example:
  21. //                  ABCADA
  22. //                  CBACD
  23. //
  24. //                  Iteration 1:
  25. //                    --result is ""--
  26. //                    Remove:
  27. //                    ABCADA
  28. //                    CBACD
  29. //                    Equal ? No;
  30. //                    Closest indexes:
  31. //                    [0, 2]
  32. //                    Remove:
  33. //                    BCADA
  34. //                    CD
  35. //
  36. //                  Iteration 2:
  37. //                    --result is "A"--
  38. //                    Remove:
  39. //                    BCADA
  40. //                    CD
  41. //                    Equal ? No;
  42. //                    Closest indexes:
  43. //                    [1, 0]
  44. //                    Remove:
  45. //                    ADA
  46. //                    D
  47. //
  48. //                  Iteration 3:
  49. //                    --result is "AC"--
  50. //                    Remove:
  51. //                    D
  52. //                    D
  53. //                    Equal ? Yes - add & return;
  54. //
  55. //                  The result is: ACD
  56. //                  
  57. //
  58. //
  59. // MY APPROACH:
  60. //
  61. //
  62. // While string aren't empty: <------ (repeat) -------------------.
  63. //                                                                 \
  64. // Filter unmatching characters                                    |
  65. // If strings are equal, push one of them to result and return it  |
  66. // Find closest letter indexes                                     |
  67. // Check if closest array (in this case, tuple) isn't empty,       |
  68. //       otherwise javascript will throw exception                 /
  69. // Remove characters behind & on indexes then add it into result  /
  70. //---------------------------------------------------------------'
  71.  
  72.  
  73.  
  74. //Filter characters that doesn't match in strings
  75. function filterUnmatching(s1, s2)
  76. {
  77.     var result = ["", ""];
  78.  
  79.     s1.split('').forEach(function(item){
  80.         // Check if items from first string is in the second.
  81.         if (s2.indexOf(item) != -1) result[0] += item;
  82.     });
  83.  
  84.     // Removed to optimize, result: -1ms/100reqests;
  85.     // REMOVED: s2.split('').forEach(function(item){
  86.     // REMOVED:     if (s1.indexOf(item) != -1) result[1] += item;
  87.     // REMOVED: });
  88.  
  89.     result[1] = s2;
  90.     return result;
  91. }
  92.  
  93. function closestIndex(s1, s2) {
  94.     var idxs = {};
  95.  
  96.     s1.split('').forEach(function(item, i){
  97.         // Figure out what letter from first string is closest to second
  98.         var iof = s2.indexOf(item);
  99.         if (iof != -1)
  100.         {
  101.             idxs[i + iof] = [i, iof, item];
  102.         }
  103.     });
  104.     s2.split('').forEach(function(item, i){
  105.         // Figure out what letter from second string is closest to first
  106.         var iof = s1.indexOf(item);
  107.         if (iof != -1)
  108.         {
  109.             // `i + iof` is the price of the index, if it's less, it's better ;)
  110.             idxs[i + iof] = [iof, i, item];
  111.         }
  112.     });
  113.  
  114.     for (var key in idxs)
  115.     {
  116.         // Since objects sort automatically
  117.         // It returns us the 'lowest price index' (see mention above)
  118.         // I didn't know any other solution because provider turned my internet off, lol
  119.         return idxs[key];
  120.     }
  121. }
  122. function removeBehind(indexes, s1, s2) {
  123.     // Removes all characters behind + the current character (indexed)
  124.     return [s1.substr(indexes[0]+1), s2.substr(indexes[1]+1)];
  125. }
  126.  
  127.  
  128. //The main function
  129. function subseq(s1, s2)
  130. {
  131.     var result = "";
  132.     while (s1 != "" || s2 != "")
  133.     {
  134.         // The main code that assembles everything above, see 'MY APPROACH' section above for more info
  135.         var filtered = filterUnmatching(s1, s2);
  136.         s1 = filtered[0];
  137.         s2 = filtered[1];
  138.         if (s1 == s2)
  139.         {
  140.             result += s1;
  141.             return result;
  142.         }
  143.         var closest = closestIndex(s1, s2);
  144.         if (closest == null) return result;
  145.         result += closest[2];
  146.  
  147.         var removed = removeBehind(closest, s1, s2);
  148.         s1 = removed[0];
  149.         s2 = removed[1];
  150.     }
  151.     return result;
  152. }
  153.  
  154. // Generates rand string for test and benchmark
  155. function randstr() {
  156.     var res = "";
  157.     var chars = "UIOPASDFCV"; // Here you can put your own characters
  158.     for (var i = 0; i < 7; i++)
  159.     {
  160.         res += chars[parseInt(Math.random()*chars.length)]
  161.     }
  162.     return res;
  163. }
  164.  
  165. // Get UNIX timestamp now (in milliseconds)
  166. var a = Date.now()
  167. var iters = 10; // Here you can set your arbitrary value of iterations
  168. for (var i = 0; i < iters; i++)
  169. {
  170.     var s1 = randstr();
  171.     var s2 = randstr();
  172.  
  173.     console.log(s1+", "+s2+": "+subseq(s1, s2));
  174. }
  175. // Get UNIX timestamp now again (in milliseconds) and
  176. // Subtract from previous time, thus, we get how much
  177. // Did this operation took time.
  178. console.log(Date.now() - a+"ms")
  179. //
  180. // My personal benchmarks btw (without console.log, on nodejs):
  181. //
  182. // 1 iteration:
  183. //   ~ 1ms
  184. // 10 iterations:
  185. //   ~ 0ms (it's faster for some reason lmao)
  186. // 100 iterations:
  187. //   ~ 1ms
  188. // 1000 iterations:
  189. //   ~ 14ms
  190. // 10000 iterations:
  191. //   ~ 69ms (oh yeah)
  192. // 100000 iterations:
  193. //   ~ 578ms
  194. // 1000000 iterations:
  195. //   ~ 5522ms
  196. // 10000000 iterations:
  197. //   ~ 59483ms
  198. // 100000000 iterations:
  199. //   ~ I have to go sleep rn, will add tomorrow
  200. //
  201. //
  202. //
  203. // Made by @ncon and his 2 hours of commenting
  204. // Bye
  205. //
  206. //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement