Oliver Eyton-Williams ee1e8abd87
feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script

* chore(curriculum): remove empty sections' markers

* chore(curriculum): add seed + solution to Chinese

* chore: remove old formatter

* fix: update getChallenges

parse translated challenges separately, without reference to the source

* chore(curriculum): add dashedName to English

* chore(curriculum): add dashedName to Chinese

* refactor: remove unused challenge property 'name'

* fix: relax dashedName requirement

* fix: stray tag

Remove stray `pre` tag from challenge file.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>

Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-12 19:31:00 -07:00

2.4 KiB
Raw Blame History

id, title, challengeType, videoUrl, dashedName
id title challengeType videoUrl dashedName
59f40b17e79dbf1ab720ed7a 部门编号 5 department-numbers

--description--

有一个高度组织化的城市决定为每个部门分配一个号码:

警察局环卫部门消防部门

每个部门的数字可以在1到7之间

这三个部门编号应该是唯一的彼此不同并且必须加起来为12。

警察局长不喜欢奇怪的号码,并希望他的部门有一个偶数。

任务:

编写一个输出所有有效组合的程序:

[2,3,7]

[2,4,6]

[2,6,4]

[2,7,3]

[4,1,7]

[4,2,6]

[4,3,5]

[4,5,3]

[4,6,2]

[4,7,1]

[6,1,5]

[6,2,4]

[6,4,2]

[6,5,1]

--hints--

combinations应该是一个功能。

assert(typeof combinations === 'function');

combinations([1, 2, 3], 6)应该返回一个数组。

assert(Array.isArray(combinations([1, 2, 3], 6)));

combinations([1, 2, 3, 4, 5, 6, 7], 12)应返回长度为14的数组。

assert(combinations(nums, total).length === len);

combinations([1, 2, 3, 4, 5, 6, 7], 12)应返回所有有效组合。

assert.deepEqual(combinations(nums, total), result);

--seed--

--after-user-code--

const nums = [1, 2, 3, 4, 5, 6, 7];
const total = 12;
const len = 14;
const result = [
  [2, 3, 7],
  [2, 4, 6],
  [2, 6, 4],
  [2, 7, 3],
  [4, 1, 7],
  [4, 2, 6],
  [4, 3, 5],
  [4, 5, 3],
  [4, 6, 2],
  [4, 7, 1],
  [6, 1, 5],
  [6, 2, 4],
  [6, 4, 2],
  [6, 5, 1]
];

--seed-contents--

function combinations(possibleNumbers, total) {

  return true;
}

--solutions--

function combinations(possibleNumbers, total) {
  let firstNumber;
  let secondNumber;
  let thridNumber;
  const allCombinations = [];

  for (let i = 0; i < possibleNumbers.length; i += 1) {
    firstNumber = possibleNumbers[i];

    if (firstNumber % 2 === 0) {
      for (let j = 0; j < possibleNumbers.length; j += 1) {
        secondNumber = possibleNumbers[j];

        if (j !== i && firstNumber + secondNumber <= total) {
          thridNumber = total - firstNumber - secondNumber;

          if (thridNumber !== firstNumber && thridNumber !== secondNumber && possibleNumbers.includes(thridNumber)) {
            allCombinations.push([firstNumber, secondNumber, thridNumber]);
          }
        }
      }
    }
  }
  return allCombinations;
}