Created
February 6, 2022 15:13
-
-
Save louwers/6bc66c817088ba5f70ea9e0c8b0ad0e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var readline = require("readline"); | |
var rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout, | |
terminal: false, | |
}); | |
async function* getLineGen() { | |
for await (const line of rl) { | |
yield line; | |
} | |
} | |
const getLine = async () => { | |
return (await getLineGen().next()).value; | |
}; | |
const getNumArr = async () => | |
(await getLine()) | |
.trim() | |
.split(" ") | |
.map((val) => parseInt(val)); | |
const sumArr = (arr) => arr.reduce((prev, curr) => prev + curr, 0); | |
const main = async () => { | |
const [n, t] = await getNumArr(); | |
const arr = await getNumArr(); | |
switch (t) { | |
case 1: | |
console.log(7); | |
break; | |
case 2: | |
if (arr[0] > arr[1]) console.log("Bigger"); | |
else if (arr[0] === arr[1]) console.log("Equal"); | |
else console.log("Smaller"); | |
break; | |
case 3: | |
console.log(arr[1]); | |
break; | |
case 4: | |
const sum = sumArr(arr); | |
console.log(sum); | |
break; | |
case 5: | |
const evenArr = arr.filter((val) => val % 2 == 0); | |
console.log(sumArr(evenArr)); | |
break; | |
case 6: | |
const seq = arr | |
.map((val) => val % 26) | |
.map((val) => "a".charCodeAt(0) + val) | |
.map((val) => String.fromCharCode(val)) | |
.join(""); | |
console.log(seq); | |
break; | |
case 7: | |
let index = 0; | |
const seen = new Set([0]); | |
while (true) { | |
index = arr[index]; | |
if (index < 0 || index > n - 1) { | |
console.log("Out"); | |
break; | |
} | |
if (index === n - 1) { | |
console.log("Done"); | |
break; | |
} | |
if (seen.has(index)) { | |
console.log("Cyclic"); | |
break; | |
} | |
seen.add(index); | |
} | |
break; | |
} | |
process.exit(0); | |
}; | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment