-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge-5.js
172 lines (150 loc) · 4.1 KB
/
challenge-5.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const EventEmitter = require('events');
const counterVal = require('needy-place');
/**
* Return the given string with the characters ordered
* returning the numbers first, then the consonants and
* finally the vowels of the given string concatenated
* in that order.
* Each group must be sorted ascending in alphabetical order.
* Example: "8qra15psc4odi" => "1458cdpqrsaio"
*
* @param val
* @returns {string}
*/
function f1(val) {
const numbers = Array.from(val.replace(/[^0-9]/g, '')).sort();
const vowels = Array.from(val.replace(/[^aeiou]/gi, '')).sort();
const consonants = Array.from(val.replace(/[^bcdfghjklmnpqrstvwxyz]/gi, '')).sort();
return [...numbers, ...consonants, ...vowels].join('');
}
/**
* Get the salt number necessary to encrypt a text.
*
* @param n
*/
function getSalt(n) {
switch (n) {
case 2:
return n + (2 << 4); // 34
case 3:
return n + (2 << 5); // 67
case 4:
return n + (3 << 4); // 52
default:
return n + (5 << 2);
}
}
/**
* Cipher a given string with an old encryption method.
* Only alphabetical characters are allowed in the encyption.
*
* @param val
* @returns The encrypted text
*/
function f2(val) {
const match = /\d/.exec(val);
const salt = getSalt(match ? parseInt(match[0]) : 2 << 2);
// console.log(salt)
return val
.replace(/\d/g, '')
.split('')
.map(c => {
let code = c.charCodeAt(0);
code += salt % 26;
return String.fromCharCode(code);
})
.join('');
}
/**
* Create a sequence of numbers based on a given
* input and arrange the sequence entries in a
* data structure. After traversing the data
* structure, return the first, the 10th and the
* last element concatenated.
*/
function f3(val) {
const seq = val.split('').map(v => v.charCodeAt(0)).sort((a,b) => a-b);
return seq[0] + String(seq[9]) + seq[seq.length-1];
}
// class SeqEmitter extends EventEmitter {}
/**
* Return the first number generated by a
* sequence which is greater or equal to
* the number given as argument.
*/
function f4(n) {
function helper(i,a,b) {
if (a >= n) return a;
return helper(++i, b, a + b);
}
return helper(2,1,1)
}
/**
* Transform the given input into another text value.
*/
function main(val) {
const [a, b, c, d, e] = val.split('-');
return [
f1(e),
f2(val.replace(/-/g, '')),
f3(a + b + e),
counterVal(parseInt(c + d, 16)),
f4(parseInt(c, 16)),
].join('-');
}
console.log(main('da9e62ea-7155-4f0b-ba56-e22ade1bc333'));
// --- Validation ---
function assert(f, val, res, expected) {
if (res != expected) {
console.log(`---- Testing ${f} ----`);
console.log('On: ', val);
console.log('Got: ', res);
console.log('Expected: ', expected);
process.exit(1);
}
}
function test() {
const tests = {
'b3b4f97e-e101-4ea1-95de-5e6f6794feab': '456679bffaee-qqutttpsttuutpq-4855102-291400224-28657',
'1c4dad12-7c09-4085-8347-472b3e6a66ce': '2346667bcaee-xyvyxwzvxz-4854101-528121015-17711',
'9d4059bc-ab22-4ad2-8e0b-be49ad2c38b2': '223489bbcdae-gefdedgheehdgfe-4856101-355299827-28657',
'b73f6101-de80-43e3-9755-112455c20f50': '0011224555cf-cgeffdg-4850102-471623849-17711',
}
for (let input of Object.keys(tests)) {
const expectedMain = tests[input];
const [expF1, expoF2, expF3, expCounter, expF4] = expectedMain.split('-');
const [a, b, c, d, e] = input.split('-')
// Test f1:
{
const res = f1(e);
assert('f1', e, res, expF1);
}
// Test f2:
{
const val = input.replace(/-/g, '');
const res = f2(val);
assert('f2', val, res, expoF2);
}
// Test f3
{
const inp = a + b + e;
const res = f3(inp);
assert('f3', inp, res, expF3);
}
// Test counterVal
{
const inp = parseInt(c + d, 16);
const res = counterVal(inp);
assert('counterVal', inp, res, expCounter);
}
// Test f4
{
const res = f4(parseInt(c, 16));
assert('f4', c, res, expF4);
}
const res = main(input);
assert('main', input, res, expectedMain);
}
console.log('passed!');
}
test();