Skip to content

Commit

Permalink
modified complex code
Browse files Browse the repository at this point in the history
  • Loading branch information
moratorium08 committed Dec 8, 2016
1 parent 74f59c4 commit 16d7616
Showing 1 changed file with 5 additions and 48 deletions.
53 changes: 5 additions & 48 deletions stages/conditional02.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,58 +24,15 @@ module.exports = {
input: [5, 6, null, null],
output: [10, 18, null, null],
ioGenerator: (random) => {
const lowerbound = (array, value) => {
if (array[0] > value) {
return -1;
}
const candidates = Array.from({length: 200}, (item, index) => index + 1)
.filter((val) => val !== 5 && val !== 6);

let lb = 0;
let ub = array.length;

while (lb + 1 < ub) {
const middle = Math.floor((ub + lb) / 2);
if (array[middle] > value) {
ub = middle;
} else {
lb = middle;
}
}
return lb;
};

const generateRandom = (excludes, maximum = 200) => {
excludes.sort();
const modifiedMax = maximum - excludes.length;
const number = Math.floor(random() * modifiedMax);

let before = -1;
let offset = 0;
while (before !== offset) {
const tmp = lowerbound(excludes, number + offset) + 1;
before = offset;
offset = tmp;
}
return number + offset;
};

const generateAnswer = (input) => {
if (input === 5) {
return 10;
} else if (input === 6) {
return 18;
}
return input;
};

const input1 = generateRandom([5, 6], 198);
const output1 = generateAnswer(input1);

const input2 = generateRandom([5, 6], 198);
const output2 = generateAnswer(input2);
const input1 = candidates[Math.floor(random() * candidates.length)];
const input2 = candidates[Math.floor(random() * candidates.length)];

return {
input: [5, 6, input1, input2],
output: [10, 18, output1, output2],
output: [10, 18, input1, input2],
};
},
width: 11,
Expand Down

0 comments on commit 16d7616

Please sign in to comment.