SHARE
TWEET

Untitled

a guest Oct 17th, 2016 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function palindromeHunter([length, text]) {
  2.     length = Number(length);
  3.  
  4.     let tokens = text.split(/[ \t_]+/g);
  5.     let textToProcess = tokens.join("");
  6.     let nonDelimiters = new Set();
  7.     for(let ch of textToProcess) {
  8.         nonDelimiters.add(ch);
  9.     }
  10.  
  11.  
  12.     let delimiterRegex = ("[" + Array.from(nonDelimiters).join("") + "]+").replace(/-/g, "\\-");
  13.     let delimiters = text.split(new RegExp(delimiterRegex)).filter(d => d != "");
  14.  
  15.     for(let i =0; i<textToProcess.length; i++){
  16.         let piece = textToProcess.substr(i,length);
  17.         let validPieceRegex = /^[A-Za-z]+$/;
  18.         let match = null;
  19.         if(match = validPieceRegex.exec(piece)){
  20.             if(match[0].length == length){
  21.                 if(match[0] == match[0].split("").reverse().join("")){
  22.                     textToProcess = textToProcess.replace(match[0], "*".repeat(match[0].length));
  23.                 }
  24.             }
  25.         }
  26.     }
  27.  
  28.     let processedText = [];
  29.  
  30.     for(let token of tokens){
  31.         processedText.push(textToProcess.substr(0, token.length));
  32.         textToProcess = textToProcess.substr(token.length);
  33.     }
  34.  
  35.     let output = "";
  36.     if(processedText.length > delimiters.length){
  37.         for(let i=0; i<delimiters.length; i++){
  38.             output += processedText[i] + delimiters[i];
  39.         }
  40.         output += processedText[processedText.length - 1];
  41.     } else if (processedText.length < delimiters.length){
  42.         for(let i=0; i<processedText.length; i++){
  43.             output += delimiters[i] + processedText[i];
  44.         }
  45.         output += delimiters[delimiters.length - 1];
  46.     } else {
  47.         for(let i=0; i<delimiters.length; i++){
  48.             if(text[0] == " " || text[0] == "\t" || text[0] == "_") {
  49.                 output += delimiters[i] + processedText[i];
  50.             } else {
  51.                 output += processedText[i] + delimiters[i];
  52.             }
  53.         }
  54.     }
  55.  
  56.     console.log(output);
  57. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top