【问题标题】:ROT13 cipher in as few lines of code possible尽可能少的代码行中的 ROT13 密码
【发布时间】:2016-11-22 15:54:31
【问题描述】:

我编写了一个函数,将接收到的字符串中的字母值移动 13 位。

我的解决方案效率极低,如果换档因子发生变化,则需要完全重写。

这是我的解决方案:

function rot13(str) {
  var charArray = str.split("");
  var myArray = [];

  for (var i = 0; i < charArray.length; i++) {
    switch (charArray[i]) {

      case "A":
        myArray.push("N");
        break;

      case "B":
        myArray.push("O");
        break;

      case "C":
        myArray.push("P");
        break;

      case "D":
        myArray.push("Q");
        break;

      case "E":
        myArray.push("R");
        break;

      case "F":
        myArray.push("S");
        break;

      case "G":
        myArray.push("T");
        break;

      case "H":
        myArray.push("U");
        break;

      case "I":
        myArray.push("V");
        break;

      case "J":
        myArray.push("W");
        break;

      case "K":
        myArray.push("X");
        break;

      case "L":
        myArray.push("Y");
        break;

      case "M":
        myArray.push("Z");
        break;

      case "N":
        myArray.push("A");
        break;

      case "O":
        myArray.push("B");
        break;

      case "P":
        myArray.push("C");
        break;

      case "Q":
        myArray.push("D");
        break;

      case "R":
        myArray.push("E");
        break;

      case "S":
        myArray.push("F");
        break;

      case "T":
        myArray.push("G");
        break;

      case "U":
        myArray.push("H");
        break;

      case "V":
        myArray.push("I");
        break;

      case "W":
        myArray.push("J");
        break;

      case "X":
        myArray.push("K");
        break;

      case "Y":
        myArray.push("L");
        break;

      case "Z":
        myArray.push("M");
        break;

      case " ":
        myArray.push(" ");
        break;

      case ",":
        myArray.push(",");
        break;

      case "!":
        myArray.push("!");
        break;

      case "?":
        myArray.push("?");
        break;

      case ".":
        myArray.push(".");
        break;
    }
  }

 

  console.log(myArray.join(""));

}
rot13("GUR DHVPX OEBJA QBT WHZCRQ BIRE GUR YNML SBK.");

你能告诉我一个更有效、更简单的解决方案吗?

【问题讨论】:

  • 提示:var map = {A: 'N', ...} 提示 2:String.fromCharCode(str.charCodeAt(...) + 13)...
  • 这个问题可能适合Code Review,只要 (a) 您的代码按预期工作,(b) 您的代码是真实代码,而不是示例代码,并且 (c) 您的代码包含在问题的正文中。如果您希望通过同行评审来改进代码的各个方面,请将其发布在 Code Review 上。
  • 也许使用它们的 ASCII 值,然后将它们移动任意数字,在本例中为 13?

标签: javascript


【解决方案1】:

这是一种可能的实现方式,它能够传入任何(正)旋转值和其他替换值表。用 ES6 编写。

function rotn(str, rotation = 13, map = {}) {
    const table = {};  // New table, to avoid mutating the parameter passed in
    // Establish mappings for the characters passed in initially
    for (var key in map) {
        table[map[key]] = key;
        table[key] = map[key];
    }
    // Then build the rotation map.
    // 65 and 97 are the character codes for A and a, respectively.
    for (var i = 0; i < 26; i++) {
        table[String.fromCharCode(65 + i)] = String.fromCharCode(65 + (i + rotation) % 26);
        table[String.fromCharCode(97 + i)] = String.fromCharCode(97 + (i + rotation) % 26);
    }

    return str.split('').map((c) => table[c] || c).join('');
}


console.log(rotn("Gur dhvpx oebja Qbt whzcrq bire gur ynml Sbk.", 13, {'.': '!'}));

【讨论】:

    【解决方案2】:

    这里是一个使用reduce函数的例子:

    function rot13(str) {
      chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
      return str.split("").reduce(function(a, b) {
        if (chars.indexOf(b) == -1) {
          return a + b;
        }
        return a + chars[(chars.indexOf(b)+13) % chars.length]
      }, "");
    }
    
    console.log(rot13("GUR DHVPX OEBJA QBT WHZCRQ BIRE GUR YNML SBK."));

    【讨论】:

    • 你为什么不检查b是否在chars而不是extra_chars?那么你就不需要指定什么是额外的字符了。
    最近更新 更多