-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1024.js
executable file
·43 lines (31 loc) · 1.17 KB
/
1024.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const { readFileSync } = require("fs")
const [numCases, ...texts] = readFileSync("/dev/stdin", "utf8").split("\n")
// Compose functions from left to right
const pipe = (...fns) => (value) => fns.reduce((res, fn) => fn(res), value)
// /** @param {string} text */
// function criptgraph(text) {
// return pipe(criptOne, criptTwo, criptThree)(text)
// }
// Prevent unnecessary re-pipering, but parameter's information has been hidden
const criptgraph = pipe(criptOne, criptTwo, criptThree)
function main() {
const responses = texts.slice(0, +numCases).map(criptgraph)
console.log(responses.join("\n"))
}
main()
/** @param {string} text */
function criptOne(text) {
return text.replace(/[a-zA-Z]/g, (char) => String.fromCharCode(char.charCodeAt(0) + 3))
}
/** @param {string} text */
function criptTwo(text) {
if (text.length < 2) return text
return [...text].reverse().join("")
}
/** @param {string} text */
function criptThree(text) {
const limit = Math.trunc(text.length / 2)
const originalText = text.substr(0, limit)
const processedText = text.substr(limit).replace(/./g, (char) => String.fromCharCode(char.charCodeAt(0) - 1))
return `${originalText}${processedText}`
}