-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7kyuu.js
386 lines (317 loc) · 15 KB
/
7kyuu.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// 001: Get the Middle Character
const getMiddle = str => str.subst(Math.ceil(str.length / 2 - 1 ), ~~(str.length/2+1));
function getMiddle(str){
return str.slice((str.length-1)/2, str.length/2+1);}
// 002: Disemvowel Trolls
const disemvowel = str =>str.replace(/[aiueo]/gi,'');
function disemvowel(str) {
let vowel = ['a','i','u','e','o','A','I','U','E','O'];
return [...str].filter(el=> vowel.indexOf(el)== -1).join``;
}
// 003: Square Every Digit
// String(num),(num+'')
const squareDigits = num => +([...`${num}`].reduce((acc, val)=> acc + val*val,''));
const squareDigits = num => +([...(num.toString())].map(val=> val*val).join``);
// 004: Mumbling
const accum = str => [...str].map(
(char,i)=> ( char.toUpperCase() + char.toLowerCase().repeat(i))).join`-`;
// 005: Highest and Lowest
const highAndLow=num=> `${Math.max(...(num.split` `))} ${Math.min(...(num.split` `))}`;
// 006: Exes and Ohs
const XO = str => (str.match(/o/gi)||0).length == (str.match(/x/gi)||0).length;
const XO = str => str.replace(/o/ig,'').length == str.replace(/x/ig,'').length;
const XO = str => str.toLowerCase().split('o').length == str.toLowerCase().split('x').length;
// 007: Descending Order
const descendingOrder = num => +([...`${num}`].sort((a,b)=> b-a).join``);
const descendingOrder = num => Number(String(num).split``.sort().reverse().join``);
// 008: Complementary DNA // replace using object/array
const DNAStrand=dna=> dna.replace(/./g, el=>{A:'T', T:'A', C:'G', G:'C'}[el]);
const DNAStrand=dna=> dna.replace(/./g, el=> 'CGAT'['GCTA'.indexOf(el)]);
// 009:Shortest Word
const findShort=str=> Math.min(...str.split` `.map(str => str.length));
const findShort=str=> str.split` `.reduce((acc, val)=> Math.min(acc, val.length), Infinity);
// 010: Binary Addition
const addBinary=(a,b)=> ((a+b) >>> 0).toString(2); // >>>0 it's a good practice
const friend=arr=> arr.filter(el => el.length == 4);
// 011: Make a function that does arithmetic! // object/array
const arithmetic=(a,b,op)=> ({'add': a+b, 'subtract': a-b, 'multiply': a*b, 'divide': a/b}[op]);
const arithmetic=(a,b,op)=> eval(a + '+-*/'['asmd'.search(op[0])] + b);
// 012: Summing a number's digits
const sumDigits = num => eval([...String(Math.abs(num))].join('+'));
const sumDigits= num => Math.abs(num).toString().split``.reduce((acc, el)=> acc + +el, 0);
// x|0 'OR' returns the true part or the integer, when false|false, returns the last false
const sumDigits = num=> [...(num + '')].reduce((acc,el)=> acc + (el | 0), 0);
// 013: The Coupon Code
const checkCoupon = (enteredCode, correctCode, currentDate, expirationDate)=> enteredCode === correctCode && new Date(currentDate) <= new Date(expirationDate);
// 014: Gauß needs help! (Sums of a lot of numbers).
f = n=> n !== (n | 0) || n < 1 ? false : n * (n + 1) / 2; // n !== (n|0) questions: is 'n' integer?
// 015: Beginner Series #3 Sum of Numbers
const getSum = (a,b)=> (Math.max(a,b)-Math.min(a,b)+1) * (Math.max(a,b)+Math.min(a,b)) / 2;
const getSum = (a,b)=> a==b? a : a<b ? a+getSum(a+1,b) : a+getSum(a-1,b);
const getSum = (a,b)=> Math.abs(a-b) * (a+b) / 2;
// 016: Round up to the next multiple of 5
const roundToNext5 = n => Math.ceil(n/5)*5;
const roundToNext5 = n => (n%5)? roundToNext5(n+1) : n;// recursiveness
// 017: Find the nth Digit of a Number
const findDigit = (num, nth) => nth > 0 ? nth != 1 ? findDigit(Math.abs(~~(num/10)), --nth) : num%10 : -1;
const findDigit = (num, nth)=> --nth >= 0? +[...(''+Math.abs(num))].reverse()[nth] || 0 : -1;
// 018: Moves in squared strings (I)
const vertMirror = s => s.map(el=> [...el].reverse().join``);
const horMirror = s => s.reverse();
const oper = (fct, s) => fct(s.split('\n')).join('\n');
// 019: List Filtering
const filter_list = l => l.filter(el => (Number));
// 020: Isograms
const isIsogram=str=> !/(/w).*\1/i.test(str);// '\1' match the exact same variable again
const isIsogram=str=> str.length == new Set (str.toLowerCase()).size;
const isIsogram=str=> [...str.toLowerCase()].every((val, i, arr)=> arr.indexOf(val) == i) || !str;
// 021: Sum of two lowest positive integers
const sumTwoSmallestNumbers = numb => (n = numb.sort((a,b)=> a-b))[0]+n[1];
const sumTwoSmallestNumbers = num => num.sort((a,b)=>a-b).slice(0,2).reduce((acc, val)=> acc + val);
// 022: Growth of a Population
const nbYear = (p0, percent, aug, p, years = 0) => p0 < p? nbYear(p0 + p0*percent/100 + aug, percent, aug, p, years+1) : years;
// 023: Credit Card Mask
const maskify = cc => cc.slice(-4).padStart(cc.length,'#');
const maskify = cc => cc.slice(0, -4).replace(/./g, '#')+ cc.slice(-4);
const maskify = cc => cc.replace(/.(?=.{4})/g, '#');
const maskify = cc => '#'.repeat(Math.max(0, cc.length-4)) + cc.substr(-4);
// 024: Find the next perfect square!
const findNextSquare= sq=> Number.isInteger(s = Math.sqrt(sq))? ++s*s : -1;
// 025: Two to One
const longest=(s1, s2)=> [...new Set(s1+s2)].sort().join``;
const longest=(s1, s2)=> Array.from(new Set(s1+s2)).sort().join``;
const longest=(s1, s2)=> 'abcdefghijklmnopqrstuvwxyz'.replace(new RegExp(`[^${s1+s2}]`, 'g'),'');
// 026: Printer Errors
const printerError = s => `${s.match(/[n-z]/g)||0.length}/${s.length}`;
// 027: Sum of odd numbers
const rowSumOddNumbers = n => n * n * n;
// 028: Categorize New Member
const openOrSenior= data => data.map(([age, lvl]) => (age > 54 && lvl > 7) ? 'Senior' : 'Open');
// 029: Ones and Zeros
const binaryArrayToNumber = arr => parseInt(arr.join(''), 2);
//OR is used to avoid first 0 case cancel all the operation
const binaryArrayToNumber = arr => arr.reduce((acc, val) => acc << 1 | val);
const binaryArrayToNumber = arr => arr.reduce((acc, val) => (acc = acc * 2 + val),0);
// 030: Find the divisors!
function divisors(int) {
for ( var res= [], i = 2; i<= int/2; ++i ) if ( !int%i ) res.push(i);
return res.length ? res : `${int} is prime`;}
const divisors = int => (res = [...Array(int).keys()].slice(2, int/2+1).filter(el => !(int%el)), res.length ? res : `${int} is prime`);
// 031: Number of People in the Bus
const number = busStops => [...busStops].reduce((acc, [on, off])=> acc + on - off,0);
// 032: Remove the minimum (without changing the original array)
const removeSmallest = nums => {
let min = Math.min.apply(this, nums);
return [...nums.slice(0, min), ...nums.slice(++min)]};
const removeSmallest=nums=> nums.filter((el, i)=> i!= nums.indexOf(Math.min(...nums)));
// 033: String ends with?
const solution=(str, end)=> str.match(end+'$') == end;
const solution=(str, end)=> str.endsWith(end);
const solution=(str, end)=> new RegExp(end+'$', 'i').test(str);
const solution=(str, end)=> str.substr(-end.length) == end;
// 034: Breaking chocolate problem
const breakChocolate = (n,m) => Math.max(0, m*n-1);
const breakChocolate = (n,m) => (n*=m) ? n-1 : n;
// 035: Reverse words
const reverseWords = str => str.split` `.map(word=> [...word].reverse().join``).join` `;
const reverseWords = str => str.replace(/\S+/g, word=> [...word].reverse().join` `);
// 036: The highest profit wins!
const minMax= arr => [Math.min(...arr), Math.max(...arr)];
// 037: Odd or Even?
const oddOrEven = arr => arr.reduce((acc, val)=> acc+val, 0)&1? 'odd' : 'even';
const oddOrEven = arr => ['even', 'odd'][arr.reduce((acc, el)=> acc + el, 0) & 1];
const oddOrEven = arr => arr.filter(el=> el & 1).length & 1 ? 'odd' : 'even';
// 038: Money, Money, Money
function calculateYears(principal, interest, tax, desired, y=0) {
while (principal < desired) {
principal+= principal*interest*(1-tax)
y++;
} return y;}
// Math.log(8)/Math.log(2) = logarithm of 8 with base 2 = 4
const calculateYears=(P, interest, tax, desired)=> Math.ceil(Math.log(desired / P) / Math.log(1 + interest * (1 - tax)));
// 039: Find the stray number
function stray(nums) {
for (let i in nums) {
if (nums.indexOf(nums[i] == nums.lastIndexOf(nums[i]))) return nums[i];
}}
const stray = nums => nums.reduce((acc, el)=> acc^el);
const stray=nums=> nums.filter( x => x == Math.max(...nums)).length == 1 ?
Math.max(...nums): Math.min(...nums);
// 040: Largest 5 digit number in a series
function solution(dgt) {
let largest = i =0;
for (i; i < dgt.length-4; i++) {
let buffer = dgt.slice(i, i+5)
if (buffer > largest) {
largest = buffer;
}
} return +largest;
}
// taking array in map
const solution = dgt => dgt.split``.map((_,i,arr)=> arr.slice(i, i+5).join``).sort((a,b)=>b-a)[0];
const solution = dgt => Math.max(~~dgt.substr(0,5), solution(dgt.slice(1)));
const solution = dgt => +dgt.split``.reduce((acc,_,i)=> ((buff = +dgt.slice(i, i+5)) > acc? buff:acc),0);
// 041: Don't give me five!
function dontGiveMeFive(start, end) {
for (var count =0; start <= end; start++) /5/.test(start) || count ++;
return count;}
// 042: Halving Sum
const halvingSum = n => n > 1 ? n + halvingSum(n/2|0) : n;// |0: OR, returns only the integer part
const halvingSum = H = n => n && n + H(n>>1)// n &&.. activates false/true test
// 043: Simple remove duplicates
const solve = arr => arr.filter((val, i)=> arr.lastIndexOf(val) == i);
const solve = arr => [... new Set(arr.reverse())].reverse();
// 'add' method append a 'value' to the end of a 'Set' object
const solve = arr => [...arr.reduceRight((acc, val)=> acc.add(val), new Set)].reverse();
// 044: Triangular Treasure
const triangular = T = n=> n > 0 ? n + T(--n): 0;
const triangular = n=> (n = Math.max(n, 0)) * ++n / 2; //Replacing OR 0 with Math.max(n,0)
// 045: Sum of a sequence
const sequenceSum = SS = (begin, end, step) => begin < end ? begin + SS(begin+step, end, step):0;
// 046: Count the Digit
const nbDig = (n, d)=> [...Array(++n)].map((_,i)=> i*i).join``.match(new RegExp(d, 'g')).length;
// 047: No oddities here
const noOdds = values => values.filter(el => ~(el&1));
// m flag causes '^' and '$' match the begin/end of each line, not just string.
const noOdds = values => [...values].match(/[02468]+/gm).map(Number);
// 048: Factorial (Both options throw a new 'RangeError')
const factorial = n => n<0 || n>12? n() : n < 2? 1 : n*factorial(n - 1);
const factorial = F = n => n<0 || n>12? Array(-1) : n < 2? 1 : n*F(n - 1);
// 049: Count the divisors of a number
const getDivisorsCnt = n => {
for(var cont = 0, i = 0;; i < Math.sqrt(n); ++i)
if (!(n%i)) cont+=2;
if(!(n % Math.sqrt(n))) cont++;
return cont }
// An slow one but clever
const getDivisorsCnt = n => {
for (var total = 0, i = 1, length = Math.sqrt(n); i < length; total += (n%i++) ? 0 : 2);
return (n%length)? total: ++total}
function getDivisorsCnt(n,i,t){
for(t=1,i=n>>1;i;t+=n%i--==0);
return t}
// 050: Sum of all the multiples of 3 or 5
const findSum = FS = (n, res=0, i=0) => i <=n ? !(i%3) || !(i%5) ? FS(n, res+i, ++i) : FS(n, res, ++i) : res;
const findSum = n => [...Array(++n).keys()].reduce((acc, val)=> !(val%3 && val%5)? acc+val : acc, 0);
// Note: Math.trunc return the integer part of the Number
// 051: Maximum Length Difference
const mxdiflg = (a1,a2) => a1.length && a2.length ?
(x = a1.map(str=> str.length), y = a2.map(str=> str.length),
Math.max(Math.max(...x) - Math.min(...y), Math.max(...y) - Math.min(...x))) : -1;
const mxdiflg = (a1,a2, max=-1) =>{
for (let x of a1) for (let y of a2)
max = Math.max(max, Math.abs(x.length - y.length))
return max}
// 052: Deodorant Evaporator
with (Math) evaporator = (content,epd,threshold) => ceil( log(threshold/100) / log(1-epd/100));
// 053: Find the capitals
let capitals = word => [...word].reduce((acc, cur, i)=> {
return /[A-Z]/.test(cur) && acc.push(i), acc;}, []);
let capitals = word => word.match(/[A-Z]/g).map(el=>word.indexOf(el));
// 054: Anagram Detection
const isAnagram = (test, original) =>
[...test.toLowerCase()].sort().join`` == [...original.toLowerCase()].sort().join``;
// 055: Sort Numbers
const solution = nums => (nums||[]).sort((a,b) => a -b);
// 056: Sort the Gift Code
const sortGiftCode = code => [...code].sort().join``;
// 057: Sum of numbers from 0 to N
let SequenceSum = {
showSequence : count => count<0 ? count+'<0' :
[...Array(count+1).keys()].join('+') + (count ? ' = ' : '=') + count*(count+1)/2}
// 058: Maximum Multiple 11ms /
const maxMultiple = (divisor,bound) => bound - bound % divisor ;
// 059: Palindrome chain length
//speedwise: `${c}` > c+'', [...c] > split('') > split``
const palindromeChainLength = function(n) {
var x = parseInt(`${n}`.split``.reverse().join``);
if (n != x) return 1 + palindromeChainLength(n + x);
return 0;
}
// 060: Alternate capitalization 807ms / 1008ms / 246ms
function capitalize(s) {
let even = [...s].map((char, key) => key&1? char: char.toUpperCase()).join('');
let odd = [...s].map((char, key) => key%2? char.toUpperCase() : char).join('');
return [even, odd]
}
function capitalize(s) {
return [0,1].map(res => [...s].map((val, i) => i%2 === res ? val.toUpperCase() : val).join(''));
}
function capitalize(str) {
const STR = str.toUpperCase();
let even = '', odd = '';
for (var i = 0; i < STR.length; i++) {
if (i&1) {
even += str[i];
odd += STR[i];
} else {
even += STR[i];
odd += str[i];
}
}
return [even, odd]
}
// 061: Functional Addition 15ms/ 16ms
function add(n) {
return function (m) { return n + m }
}
var add = a=> n=> n + a;
// 062: Remove duplicate words 2860ms / 2139ms / 2055ms
const removeDuplicateWords = arr => [...new Set(arr.split(' '))].join(' ');
const removeDuplicateWords = arr => arr.split(' ').filter((val, key, arr) => arr.indexOf(val) == key).join(' ');
function removeDuplicateWords(str) {
let arr = str.split(' '), output = [];
for (let el of arr)
if (!output.includes(el))
output.push(el)
return output.join(' ');
}
// 063: Building Strings From a Hash 408ms / 255ms / 199ms
//speedwise: string '' > array []+join();
const solution = pairs=> Object.keys(pairs).map(key => `${key} = ${pairs[key]}`).join();
function solution(pairs) {
let array = [];
for (let p in pairs) {
array.push((`${p} = ${pairs[p]}`))
} return array.join();
}
function solution(pairs){
let response = '';
for (p in pairs) {
response += `${p} = ${pairs[p]},`;
} return response.slice(0, -1);
}
// 064: Find the middle element 466ms / 21ms
// speedwise: function name(args) > const name = function (args)
const gimme = arr=> arr.indexOf(arr.concat().sort((a,b)=> a-b)[1]);
function gimme(a) {
if ((a[0] < a[1] && a[0] > a[2]) ||
(a[0] > a[1] && a[0] < a[2])) return 0;
if((a[1] < a[0] && a[1] > a[2]) ||
(a[1] > a[0] && a[1] < a[2])) return 1;
if ((a[2] < a[0] && a[2] > a[1]) ||
(a[2] > a[0] && a[2] < a[1])) return 2;
}
// 065: Greet Me 213ms / 35ms
//speedwise: slice > substr
const greet = name => `Hello ${[...name][0].toUpperCase()+(name.toLowerCase().slice(1))}!`;
const greet = name => `Hello ${name.charAt(0).toUpperCase()+name.slice(1).toLowerCase()}!`;
// 066: Testing 1-2-3 134ms / 123ms
const number = arr => arr.map((el, key) => `${++key}: ${el}`);
const number = function(array){
if (array.length == 0) return [];
for (let i=0; i<array.length; ++i)
array[i] = (i+1) + ': ' + array[i];
return array;
}
// 067: Number Of Occurrences 64ms / 32ms
Array.prototype.numberOfOccurrences = function(n) {
return this.filter(el=> el == n).length;
}
Array.prototype.numberOfOccurrences = function (n) {
var occur = 0;
for (var i = 0; i < this.length; i++){
if (this[i] == n) occur++;
} return occur;
}
// 068: Predict your age!