每日一道算法题——字符交替换位拼接实现字符串加密

每日一道算法题——字符交替换位拼接实现字符串加密

@time: 20180505
@author: [email protected]
参考:https://repl.it/@nivanov1030/Code-Wars-simple-encryption-1

题目大意:
从字符串中依次取位置是2的倍数的字符,和位置不是2的倍数的字符,并将它们组成的字符串拼接,得到一个新的字符串。实现加密

// For building the encrypted string:
// Take every 2nd char from the string, then the other chars, that are not every 2nd char, and concat them as new String.
// Do this n times!

// Examples:

// "This is a test!", 1 -> "hsi  etTi sats!"
// "This is a test!", 2 -> "hsi  etTi sats!" -> "s eT ashi tist!"
// Write two methods:

// function encrypt(text, n)
// function decrypt(encryptedText, n)
// For both methods:
// If the input-string is null or empty return exactly this value!
// If n is <= 0 then return the input text.

function encrypt(text, n) {
  var firstChars = '';
  var secondChars = '';
  var encryptedStr = text;
  if(n <= 0 || text === null) {
    return encryptedStr;
  } else {
    for(var i = 0; i < text.length; i++) {
      if(i % 2) {
        secondChars += text[i];
      } else {
        firstChars += text[i];
      }
    }
    encryptedStr = secondChars + firstChars;
    return encrypt(encryptedStr, n - 1);
  }
}

encrypt(null, 2);

function decrypt(encryptedText, n) {
  var decryptedStr = encryptedText;
  if(n <= 0 || encryptedText === null) {
    return decryptedStr;
  } else {
    var midPoint = Math.floor(encryptedText.length / 2);
    var secondChars = encryptedText.slice(0, midPoint);
    var firstChars = encryptedText.slice(midPoint);
    decryptedStr = '';
    var stopLoop = firstChars.length;
    for(var i = 0; i < stopLoop; i++) {
      if(secondChars[i] === undefined) {
        decryptedStr += firstChars[i];
      } else {
        decryptedStr += firstChars[i] + secondChars[i];
      }
    }
    return decrypt(decryptedStr, n - 1);
  }
}


decrypt("el!hlo", 1);


一些测试用例:

describe("Solution", function(){
  it("EncryptExampleTests", function(){    
    Test.assertEquals(encrypt("This is a test!", 0), "This is a test!");
    Test.assertEquals(encrypt("This is a test!", 1), "hsi  etTi sats!");
    Test.assertEquals(encrypt("This is a test!", 2), "s eT ashi tist!");
    Test.assertEquals(encrypt("This is a test!", 3), " Tah itse sits!");
    Test.assertEquals(encrypt("This is a test!", 4), "This is a test!");
    Test.assertEquals(encrypt("This is a test!", -1), "This is a test!");
    Test.assertEquals(encrypt("This kata is very interesting!", 1), "hskt svr neetn!Ti aai eyitrsig");
  });
});

describe("Solution", function(){
  it("DecryptExampleTests", function(){    
    Test.assertEquals(decrypt("This is a test!", 0), "This is a test!");
    Test.assertEquals(decrypt("hsi  etTi sats!", 1), "This is a test!");
    Test.assertEquals(decrypt("s eT ashi tist!", 2), "This is a test!");
    Test.assertEquals(decrypt(" Tah itse sits!", 3), "This is a test!");
    Test.assertEquals(decrypt("This is a test!", 4), "This is a test!");
    Test.assertEquals(decrypt("This is a test!", -1), "This is a test!");
    Test.assertEquals(decrypt("hskt svr neetn!Ti aai eyitrsig", 1), "This kata is very interesting!");
  });
});

describe("Solution", function(){
  it("Null or Empty", function(){    
    Test.assertEquals(encrypt("", 0), "");
    Test.assertEquals(decrypt("", 0), "");
    Test.assertEquals(encrypt(null, 0), null);
    Test.assertEquals(decrypt(null, 0), null);
  });
});

猜你喜欢

转载自blog.csdn.net/example440982/article/details/80210982