如何使置换码更简洁?

时间:2019-08-01 22:07:40

标签: javascript reactjs

例如,我有A,B,C,D。输入可能是对还是错,每个语句都有其自身的条件,因此我需要编写2 ^ 4 = 16 if-else语句,但看起来太乱了

我只知道强力解决方案...

ABCD

ABC

ABD

ACD

BCD

AB

AC

CD

BC

BD

A

B

C

D

没事


const  Permutations = (A,B,C,D) => {

    if (!A && !B && !C && !D) {
        return '1'
    } else if (!A && B && !C && !D) {
        return '2'
    } else if (!A && !B && C && !D) {
        return '3'
    } else if (!A && !B && !C && D) {
        return '4'
    } else if (A && B && !C && !D) {
        return '5'
    } else if (!A && B && !C && D) {
        return '6'
    } else if (!A && B && C && !D) {
        return '7'
    } else if (A && !B && !C && D) {
        return '8'
    } else if (A && !B && C && !D) {
        return '9'
    } else if (!A && !B && C && !D) {
        return '10'
    } else if (A && !B && C && D) {
        return '11'
    } else if (!A && B && C && D) {
        return '12'
    } else if (A && B && !C && D) {
        return '13'
    } else if (A && B && C && !D) {
        return "14"
    } else if (A && B && C && D) {
        return "15"
    }else{
      return '16'
    }
}



 Permutations(true,true,true,true)


我希望代码可以更简洁,并且不包含16条if-else语句。

6 个答案:

答案 0 :(得分:3)

您可以将参数解析为二进制字符串,并将其转换为唯一的整数值,以获取所有排列:

const Permutations = (A, B, C, D) => {
  A = A ? '1' : '0';
  B = B ? '1' : '0';
  C = C ? '1' : '0';
  D = D ? '1' : '0';

  return parseInt(`${D}${C}${B}${A}`, 2) + 1;
}

console.log(Permutations(false, false, false, false)); // 1
console.log(Permutations(true, false, false, false)); // 2
console.log(Permutations(false, true, false, false)); // 3
console.log(Permutations(true, true, false, false)); // 4
console.log(Permutations(false, false, true, false)); // 5
console.log(Permutations(true, false, true, false)); // 6
console.log(Permutations(false, true, true, false)); // 7
console.log(Permutations(true, true, true, false)); // 8
console.log(Permutations(false, false, false, true)); // 9
console.log(Permutations(true, false, false, true)); // 10
console.log(Permutations(false, true, false, true)); // 11
console.log(Permutations(false, false, true, true)); // 12
console.log(Permutations(true, true, false, true)); // 13
console.log(Permutations(true, false, true, true)); // 14
console.log(Permutations(false, true, true, true)); // 15
console.log(Permutations(true, true, true, true)); // 16

答案 1 :(得分:1)

我会为此使用标志,但是这将要求您使用不同的返回值:

const permutations = (A,B,C,D) => {
    let flags = 0;

    if (A) { flags ^= 0x1; }
    if (B) { flags ^= 0x2; }
    if (C) { flags ^= 0x4; }
    if (D) { flags ^= 0x8; }

    return flags;
}

permutations(false, false, false, false); // 0

permutations(true, false, false, false);  // 1
permutations(false, true, false, false);  // 2
permutations(true, true, false, false);   // 3
permutations(false, false, true, false);  // 4
permutations(true, false, true, false);   // 5
permutations(false, true, true, false);   // 6
permutations(true, true, true, false);    // 7
permutations(false, false, false, true);  // 8
permutations(true, false, false, true);   // 9
permutations(false, true, false, true);   // 10
permutations(true, true, false, true);    // 11
permutations(false, false, true, true);   // 12
permutations(true, false, true, true);    // 13
permutations(false, true, true, true);    // 14
permutations(true, true, true, true);     // 15

答案 2 :(得分:0)

这是我想出的最漂亮的方法。但是,由您决定将results中的值以正确的顺序放置,并且并不能很清楚地显示预期的结果。

function permutations(...bools) {
    let results = [
        'result 1',
        'result 2',
        'result 3',
        'result 4',
        'result 5',
        'result 6',
        'result 7',
        'result 8',
        'result 9',
        'result 10',
        'result 11',
        'result 12',
        'result 13',
        'result 14',
        'result 15',
        'result 16',
    ];

    return results[Number('0b' + bools.map(bool => Number(bool)).join(''))];
}

答案 3 :(得分:0)

有了这个,您可以根据需要拥有任意数量的参数。

const Permutations = (...args) =>
    parseInt(args.map(a => a ? '1': '0').join(''), 2);

console.log(Permutations(false, true, false));
console.log(Permutations(true, false, true, true));
console.log(Permutations(false, true, true, false, true));
console.log(Permutations(true, true, true, true, false, false));

答案 4 :(得分:0)

这不会降低程序的复杂性,但可能通过将逻辑视为真正的逻辑来使其更具可读性:一个查找表:

var map = {
    '0000': 1,
    '0100': 2,
    '0010': 3,
    '0001': 4,
    '1100': 5,
    '0101': 6,
    '0110': 7,
    '1001': 8,
    '1010': 9,
    '0010': 10,
    '1011': 11,
    '0111': 12,
    '1101': 13,
    '1110': 14,
    '1111': 15
}

var key = (A ? '1' : '0') +
          (B ? '1' : '0') +
          (C ? '1' : '0') +
          (D ? '1' : '0');

var result = map[key];

return result === undefined ? 16 : result;

现在,仅查看此map,就更容易看到您的原始代码有错误:

'0010': 3,
'0010': 10,

因此,在使错误更明显的方面,它已经比原始代码更好。通过以二进制顺序重新排列键,我们可以找出错误所在:

var map = {
    '0000': 1,
    '0001': 4,
    '0010': 3,
    '0010': 10, // duplicate
                // 0011 is missing
    '0100': 2,
    '0101': 6,
    '0110': 7,
    '0111': 12,
                // 1000 is missing
    '1001': 8,
    '1010': 9,
    '1011': 11,
    '1100': 5,
    '1101': 13,
    '1110': 14,
    '1111': 15
}

所以事实证明您有两个错误。就我个人而言,这使这种编码方式明显更好,因为它使您可以更轻松地调试逻辑


奖金把戏

如果您不在乎每个键产生的数字,而只需要数字是唯一的(例如:如果!A,!B,!C,!D返回0而不是1的话就可以了)这是将布尔值转换为数字的简单技巧:

var key = (A ? '1' : '0') +
          (B ? '1' : '0') +
          (C ? '1' : '0') +
          (D ? '1' : '0');

return parseInt(key, 2); // the 2 makes it treat the number as binary

答案 5 :(得分:0)

人们经常使用数组来解决这类问题。如果使用多维数组,请确保正确初始化。

results[0][0][0][0] = 'result 1'
results[0][0][0][1] = 'result 2'
results[0][0][1][0] = 'result 3'


return results[A][B][C][D];

或者您也可以使用分隔符并连接键以构成主键。

return results[ A + '|' + B + '|' + C + '|' + D]