From 2be607141e5f448de66fe9693570082fe95b55e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Katalin=20Kov=C3=A1cs?= Date: Sat, 5 Jun 2021 13:42:04 +0200 Subject: [PATCH] 5 - 6 kyu solutions (2021.06.05.) --- 5 - Directions Reduction.js | 14 +++++++ 5 - Formatting a number as price.js | 17 +++++++++ 5 - Regex Password Validation.js | 5 +++ 5 - Rot13.js | 12 ++++++ 5 - Where my anagrams at.js | 4 ++ 6 - Sum of Digits - Digital Root.js | 5 +++ 6 - Turn String Input into Hash.js | 12 ++++++ 6 - Are they the same.js | 26 +++++++++++++ 6 - Bit Counting.js | 5 +++ 6 - Count characters in your string.js | 9 +++++ 6 - Counting Duplicates.js | 20 ++++++++++ 6 - Fibonacci, Tribonacci and friends.js | 9 +++++ 6 - Find the odd int.js | 10 +++++ 6 - Give me a Diamond.js | 17 +++++++++ 6 - Help the bookseller.js | 37 +++++++++++++++++++ 6 - IQ Test.js | 18 +++++++++ 6 - If you can read this.js | 16 ++++++++ 6 - Longest alphabetical substring.js | 21 +++++++++++ 6 - Mexican Wave.js | 11 ++++++ 6 - Moves in squared strings (II).js | 12 ++++++ 6 - One line task Square Every Digit.js | 2 + 6 - Parse HTML-CSS Colors.js | 31 ++++++++++++++++ 6 - Replace With Alphabet Position.js | 17 +++++++++ 6 - Salesman's Travel.js | 27 ++++++++++++++ 6 - Sort the odd.js | 18 +++++++++ 6 - Stop gninnipS My sdroW!.js | 5 +++ 6 - String Evaluation.js | 22 +++++++++++ ...cutive Powers And ....\302\241Eureka!!.js" | 13 +++++++ 6 - The Supermarket Queue.js | 19 ++++++++++ 6 - Tribonacci Sequence.js | 18 +++++++++ 6 - Unary function chainer.js | 11 ++++++ 6 - Which are in.js | 14 +++++++ 6 - Your order, please.js | 5 +++ 33 files changed, 482 insertions(+) create mode 100644 5 - Directions Reduction.js create mode 100644 5 - Formatting a number as price.js create mode 100644 5 - Regex Password Validation.js create mode 100644 5 - Rot13.js create mode 100644 5 - Where my anagrams at.js create mode 100644 6 - Sum of Digits - Digital Root.js create mode 100644 6 - Turn String Input into Hash.js create mode 100644 6 - Are they the same.js create mode 100644 6 - Bit Counting.js create mode 100644 6 - Count characters in your string.js create mode 100644 6 - Counting Duplicates.js create mode 100644 6 - Fibonacci, Tribonacci and friends.js create mode 100644 6 - Find the odd int.js create mode 100644 6 - Give me a Diamond.js create mode 100644 6 - Help the bookseller.js create mode 100644 6 - IQ Test.js create mode 100644 6 - If you can read this.js create mode 100644 6 - Longest alphabetical substring.js create mode 100644 6 - Mexican Wave.js create mode 100644 6 - Moves in squared strings (II).js create mode 100644 6 - One line task Square Every Digit.js create mode 100644 6 - Parse HTML-CSS Colors.js create mode 100644 6 - Replace With Alphabet Position.js create mode 100644 6 - Salesman's Travel.js create mode 100644 6 - Sort the odd.js create mode 100644 6 - Stop gninnipS My sdroW!.js create mode 100644 6 - String Evaluation.js create mode 100644 "6 - Take a Number And Sum Its Digits Raised To The Consecutive Powers And ....\302\241Eureka!!.js" create mode 100644 6 - The Supermarket Queue.js create mode 100644 6 - Tribonacci Sequence.js create mode 100644 6 - Unary function chainer.js create mode 100644 6 - Which are in.js create mode 100644 6 - Your order, please.js diff --git a/5 - Directions Reduction.js b/5 - Directions Reduction.js new file mode 100644 index 0000000..6957b17 --- /dev/null +++ b/5 - Directions Reduction.js @@ -0,0 +1,14 @@ +// https://www.codewars.com/kata/550f22f4d758534c1100025a + +function dirReduc(arr){ + for(let i = 0; i String.fromCharCode(c.charCodeAt(0)+13)).join(''); + + const inputTable = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; + const outputTable = 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm'; + let index = x => inputTable.indexOf(x); + let shift = x => index(x)> -1 ? outputTable[index(x)] : x; + return message.split('').map(shift).join(''); + } \ No newline at end of file diff --git a/5 - Where my anagrams at.js b/5 - Where my anagrams at.js new file mode 100644 index 0000000..f6618d8 --- /dev/null +++ b/5 - Where my anagrams at.js @@ -0,0 +1,4 @@ +// https://www.codewars.com/kata/523a86aa4230ebb5420001e1 +function anagrams(word, words) { + return words.filter(element => element.split('').sort().join('') === word.split('').sort().join('')); + } \ No newline at end of file diff --git a/6 - Sum of Digits - Digital Root.js b/6 - Sum of Digits - Digital Root.js new file mode 100644 index 0000000..8e198e0 --- /dev/null +++ b/6 - Sum of Digits - Digital Root.js @@ -0,0 +1,5 @@ +//https://www.codewars.com/kata/541c8630095125aba6000c00 +function digital_root(n) { + let result = n.toString().split('').map(x => Number(x)).reduce((a,b)=>a+b); + return result<=9 ? result : digital_root(result); + } \ No newline at end of file diff --git a/6 - Turn String Input into Hash.js b/6 - Turn String Input into Hash.js new file mode 100644 index 0000000..09cbfa6 --- /dev/null +++ b/6 - Turn String Input into Hash.js @@ -0,0 +1,12 @@ +//https://www.codewars.com/kata/52180ce6f626d55cf8000071 + +function strToHash(str){ + let result = {}; + if(str.length !==0){ + let codes = str.split(', '); + codes.forEach( + element => result[element.substring(0, element.indexOf('='))] = Number(element.substring(element.indexOf('=')+1, element.length)) + ); + } + return result; + } \ No newline at end of file diff --git a/6 - Are they the same.js b/6 - Are they the same.js new file mode 100644 index 0000000..001c2b6 --- /dev/null +++ b/6 - Are they the same.js @@ -0,0 +1,26 @@ +// https://www.codewars.com/kata/550498447451fbbd7600041c + +function comp(array1, array2){ + let result = false; + + if(array1 === null || array2 === null){ + return result; + } else if(array1.length !== array2.length){ + return result; + } else if(array1.length === 0 && array2.length === 0){ + result = true; + return result; + } else if (Array.isArray(array1) && Array.isArray(array2)){ + let array1Sorted = array1.map(x => Math.abs(x)).sort((a,b) => a-b); + let array2Sorted = array2.sort((a,b) => a-b); + for (let i =0; i Number(x)+Number(y), 0); + }; \ No newline at end of file diff --git a/6 - Count characters in your string.js b/6 - Count characters in your string.js new file mode 100644 index 0000000..490f191 --- /dev/null +++ b/6 - Count characters in your string.js @@ -0,0 +1,9 @@ +//https://www.codewars.com/kata/52efefcbcdf57161d4000091 + +function count (string) { + let result = {}; + string.split('').forEach(function(c){ + result[c] ? result[c]++ : result[c] = 1; + }); + return result; + } \ No newline at end of file diff --git a/6 - Counting Duplicates.js b/6 - Counting Duplicates.js new file mode 100644 index 0000000..952ceff --- /dev/null +++ b/6 - Counting Duplicates.js @@ -0,0 +1,20 @@ +// https://www.codewars.com/kata/54bf1c2cd5b56cc47f0007a1 + +function duplicateCount(text){ + let counter = {}; + text = text.toLowerCase(); + + for(let i = 0; i< text.length; i++){ + counter[text[i]] ? counter[text[i]] += counter[text[i]] : counter[text[i]] = 1; + } + + let result = 0; + counter = Object.values(counter); + for(let i = 0; i < counter.length; i++){ + if (counter[i] > 1) { + result++; + } + } + + return result; + } \ No newline at end of file diff --git a/6 - Fibonacci, Tribonacci and friends.js b/6 - Fibonacci, Tribonacci and friends.js new file mode 100644 index 0000000..96ea7fc --- /dev/null +++ b/6 - Fibonacci, Tribonacci and friends.js @@ -0,0 +1,9 @@ +// https://www.codewars.com/kata/556e0fccc392c527f20000c5 + +function Xbonacci(signature,n){ + const result = signature.slice(0,n); + while(result.length < n){ + result[result.length] = result.slice(-signature.length).reduce((p,c)=>p+c,0); + } + return result; + } \ No newline at end of file diff --git a/6 - Find the odd int.js b/6 - Find the odd int.js new file mode 100644 index 0000000..21e8286 --- /dev/null +++ b/6 - Find the odd int.js @@ -0,0 +1,10 @@ +//https://www.codewars.com/kata/54da5a58ea159efa38000836 + +function findOdd(A) { + let obj = {}; + let o = 0; + A.map(x => x in obj ? obj[x]+=1 : obj[x] = 1); + //console.log(obj); + Object.keys(obj).map(x => obj[x]%2!==0 ? o=x : 0) + return Number(o); + } \ No newline at end of file diff --git a/6 - Give me a Diamond.js b/6 - Give me a Diamond.js new file mode 100644 index 0000000..00a973c --- /dev/null +++ b/6 - Give me a Diamond.js @@ -0,0 +1,17 @@ +//https://www.codewars.com/kata/5503013e34137eeeaa001648 + +function diamond(n){ + if(n < 0 || !(n%2)){ + return null; + } + + const middleIdx = Math.floor(n/2); + //Array.apply: The apply() method calls a function with a given this value, + //and arguments provided as an array (or an array-like object). + return Array.apply(null, {length: n}) + .map((element, index) => { + const indent = Math.abs(index-middleIdx); + const numOfStars = n - indent*2; + return Array(indent + 1).join(' ') + Array(numOfStars + 1).join('*'); + }).join('\n')+'\n'; + } \ No newline at end of file diff --git a/6 - Help the bookseller.js b/6 - Help the bookseller.js new file mode 100644 index 0000000..b99e57d --- /dev/null +++ b/6 - Help the bookseller.js @@ -0,0 +1,37 @@ +// https://www.codewars.com/kata/54dc6f5a224c26032800005c + +function stockList(listOfArt, listOfCat){ + let result = {}; + for(let i = 0; i x[0]); + let catalogValues = Object.values(catalog); + + for(let i = 0; i < listOfCat.length; i++){ + for(let j = 0; j{ + return Object.keys(res).map(k => `(${k} : ${res[k]})`).join(' - '); + } + + return renderResult(result); + } + } \ No newline at end of file diff --git a/6 - IQ Test.js b/6 - IQ Test.js new file mode 100644 index 0000000..01b6775 --- /dev/null +++ b/6 - IQ Test.js @@ -0,0 +1,18 @@ +//https://www.codewars.com/kata/552c028c030765286c00007d + +// Bob is preparing to pass IQ test. The most frequent task in this test is to find out which one of the given numbers differs from the others. Bob observed that one number usually differs from the others in evenness. Help Bob — to check his answers, he needs a program that among the given numbers finds one that is different in evenness, and return a position of this number. + +// ! Keep in mind that your task is to help Bob solve a real IQ test, which means indexes of the elements start from 1 (not 0) + +// Examples: +// iqTest("2 4 7 8 10") => 3 // Third number is odd, while the rest of the numbers are even + +// iqTest("1 2 1 1") => 2 // Second number is even, while the rest of the numbers are odd + + +function iqTest(numbers){ + const numberArr = numbers.split(' ').map(num => Number(num)); + const even = numberArr.filter(num => num%2 === 0); + const odd = numberArr.filter(num => num%2 !==0); + return odd.length < even.length ? numberArr.indexOf(odd[0])+1 : numberArr.indexOf(even[0])+1; + } \ No newline at end of file diff --git a/6 - If you can read this.js b/6 - If you can read this.js new file mode 100644 index 0000000..d021b2c --- /dev/null +++ b/6 - If you can read this.js @@ -0,0 +1,16 @@ +// https://www.codewars.com/kata/586538146b56991861000293 + +function to_nato(words) { + let result = ''; + + for(const char of words){ + let c = char.toLowerCase() + if (Object.keys(NATO).includes(c)){ + result += NATO[c] + ' '; + } else if(c === ',' || c === '.' || c === '!' || c === '?'){ + result += c + ' '; + } + } + + return result.slice(0, result.length-1); + } \ No newline at end of file diff --git a/6 - Longest alphabetical substring.js b/6 - Longest alphabetical substring.js new file mode 100644 index 0000000..a01e56f --- /dev/null +++ b/6 - Longest alphabetical substring.js @@ -0,0 +1,21 @@ +//https://www.codewars.com/kata/5a7f58c00025e917f30000f1 + +function longest(str) { + let longest = '', + length = 0, + start = 0, + prev = str[0]; + + for(let i = 1; i <= str.length; ++i){ + if(i === str.length || str[i] < prev){ + if(length < i-start){ + longest = str.substring(start, i); + length = i-start; + } + start = i; + } + prev = str[i]; + }; + + return longest; + } \ No newline at end of file diff --git a/6 - Mexican Wave.js b/6 - Mexican Wave.js new file mode 100644 index 0000000..ff61afd --- /dev/null +++ b/6 - Mexican Wave.js @@ -0,0 +1,11 @@ +// https://www.codewars.com/kata/58f5c63f1e26ecda7e000029 + +function wave(str){ + const result = []; + for (let i = 0; i < str.length; i++){ + if(str[i] !== ' ') { + result.push(Array.from(str, (c, j) => i === j ? c.toUpperCase() : c).join('')); + } + } + return result; + } \ No newline at end of file diff --git a/6 - Moves in squared strings (II).js b/6 - Moves in squared strings (II).js new file mode 100644 index 0000000..7465109 --- /dev/null +++ b/6 - Moves in squared strings (II).js @@ -0,0 +1,12 @@ +//https://www.codewars.com/kata/56dbe7f113c2f63570000b86 + +function rot(strng) { + return strng.split('\n').map(x => x.split('').reverse().join('')).reverse().join('\n'); +} +function selfieAndRot(strng) { + return strng.split('\n').map(x=> x + '.'.repeat(x.length)).join('\n')+'\n'+ + strng.split('\n').map(x=> x + '.'.repeat(x.length)).reverse().map(x=>x.split('').reverse().join('')).join('\n'); +} +function oper(fct, s) { + return fct(s); +} \ No newline at end of file diff --git a/6 - One line task Square Every Digit.js b/6 - One line task Square Every Digit.js new file mode 100644 index 0000000..9d104a8 --- /dev/null +++ b/6 - One line task Square Every Digit.js @@ -0,0 +1,2 @@ +// https://www.codewars.com/kata/5acd142a2ec8c48521000104 +sd=x=>+[...x+''].map(d=>d*d).join`` \ No newline at end of file diff --git a/6 - Parse HTML-CSS Colors.js b/6 - Parse HTML-CSS Colors.js new file mode 100644 index 0000000..722d604 --- /dev/null +++ b/6 - Parse HTML-CSS Colors.js @@ -0,0 +1,31 @@ +// https://www.codewars.com/kata/58b57ae2724e3c63df000006 + +function parseHTMLColor(color) { + //transform color to lowercase + let tempColor = color.toLowerCase(); + + //named color to 6digit hex + if(Object.keys(PRESET_COLORS).includes(tempColor)){ + tempColor = PRESET_COLORS[tempColor]; + } + + //remove # + tempColor = tempColor.substring(1).split(''); + + //3digit hex to 5digit hex + if(tempColor.length === 3){ + tempColor = tempColor.reduce((c,i)=>c.concat(i,i),[]); + } + + //from array format to string + tempColor = tempColor.join(''); + + //calculate rgb + let result = { + r: parseInt(tempColor.slice(0,2),16), + g: parseInt(tempColor.slice(2,4),16), + b: parseInt(tempColor.slice(4,6),16) + } + + return result; + } \ No newline at end of file diff --git a/6 - Replace With Alphabet Position.js b/6 - Replace With Alphabet Position.js new file mode 100644 index 0000000..b13519d --- /dev/null +++ b/6 - Replace With Alphabet Position.js @@ -0,0 +1,17 @@ +// https://www.codewars.com/kata/546f922b54af40e1e90001da + +function alphabetPosition(text) { + //ASCII a --> 97 + //ASCII z --> 122 + const compensateAscii = 96; + let result = ''; + + for (let i = 0; i 96 && code < 123){ + result += (code-compensateAscii) + ' '; + } + } + + return result.slice(0, result.length-1); + } \ No newline at end of file diff --git a/6 - Salesman's Travel.js b/6 - Salesman's Travel.js new file mode 100644 index 0000000..aab5cb6 --- /dev/null +++ b/6 - Salesman's Travel.js @@ -0,0 +1,27 @@ +// https://www.codewars.com/kata/56af1a20509ce5b9b000001e + +function travel(r, zipcode) { + //console.log('r: ', r); + //console.log('Zip: ', zipcode); + + let addresses = r.split(','); + let addsByZip = []; + + for (const adds of addresses){ + let zip = adds.substring(adds.length-8, adds.length); + if(zip === zipcode){ + addsByZip.push(adds.substring(0, adds.length-9)); + } + } + //console.log('Adds: ', addsByZip); + + let result =zipcode+':'; + let streetTown = []; + let houseNum = []; + for(const adds of addsByZip){ + streetTown.push(adds.substring(adds.indexOf(' ')+1, adds.length)); + houseNum.push(adds.substring(0, adds.indexOf(' '))); + } + result = result+streetTown.join(',')+'/'+ houseNum.join(','); + return result; + } \ No newline at end of file diff --git a/6 - Sort the odd.js b/6 - Sort the odd.js new file mode 100644 index 0000000..bbc4ea7 --- /dev/null +++ b/6 - Sort the odd.js @@ -0,0 +1,18 @@ +//https://www.codewars.com/kata/578aa45ee9fd15ff4600090d + +function sortArray(array) { + let oddArr = array.filter(e => e % 2 !== 0).sort((a,b) => a-b); + + let result = []; + let oddIndex = 0; + for(let i = 0; i < array.length; i++){ + if (array[i]% 2 === 0){ + result.push(array[i]); + } else { + result.push(oddArr[oddIndex]); + oddIndex++; + } + } + + return result; + } \ No newline at end of file diff --git a/6 - Stop gninnipS My sdroW!.js b/6 - Stop gninnipS My sdroW!.js new file mode 100644 index 0000000..e78a094 --- /dev/null +++ b/6 - Stop gninnipS My sdroW!.js @@ -0,0 +1,5 @@ +// https://www.codewars.com/kata/5264d2b162488dc400000001 + +function spinWords(string){ + return string.split(' ').map(x => x.length >= 5 ? x.split('').reverse().join('') : x).join(' '); + } \ No newline at end of file diff --git a/6 - String Evaluation.js b/6 - String Evaluation.js new file mode 100644 index 0000000..6ce1fa6 --- /dev/null +++ b/6 - String Evaluation.js @@ -0,0 +1,22 @@ +//https://www.codewars.com/kata/57f548337763f20e02000114 + +function string_evaluation(string,condition){ + const math = { + '<': function(x,y) {return x < y}, + '>': function(x,y) {return x > y}, + '==': function(x,y) {return x == y}, + '!=': function(x,y) {return x != y}, + '<=': function(x,y) {return x <= y}, + '>=': function(x,y) {return x >= y} + } + + return condition.map(currCond => { + const length = currCond.length; + const value1 = currCond.charAt(0); + const value2 = currCond.charAt(length-1) + const operator = length ===3 ? currCond.charAt(1):currCond.substring(1,3); + const c1 = Number(value1) == value1 ? Number(value1) : string.split('').filter(char=>char ==value1).length; + const c2 = Number(value2) == value2 ? Number(value2) : string.split('').filter(char=>char ==value2).length; + return math[operator](c1,c2); + }); + }; \ No newline at end of file diff --git "a/6 - Take a Number And Sum Its Digits Raised To The Consecutive Powers And ....\302\241Eureka!!.js" "b/6 - Take a Number And Sum Its Digits Raised To The Consecutive Powers And ....\302\241Eureka!!.js" new file mode 100644 index 0000000..c7eee7a --- /dev/null +++ "b/6 - Take a Number And Sum Its Digits Raised To The Consecutive Powers And ....\302\241Eureka!!.js" @@ -0,0 +1,13 @@ +// https://www.codewars.com/kata/5626b561280a42ecc50000d1 + +function sumDigPow(a, b) { + let eureka = []; + for(let i = a; i <= b; i++) + { + let digits = String(i).split(''); + if(i === digits.reduce((acc, currV, currI) => acc + currV ** (currI + 1), 0)) { + eureka.push(i); + } + } + return eureka; + } \ No newline at end of file diff --git a/6 - The Supermarket Queue.js b/6 - The Supermarket Queue.js new file mode 100644 index 0000000..66b9d31 --- /dev/null +++ b/6 - The Supermarket Queue.js @@ -0,0 +1,19 @@ +// https://www.codewars.com/kata/57b06f90e298a7b53d000a86 + +function queueTime(customers, n) { + let maxTime = Math.max(...customers); + if(customers.length === 0){ + return 0; + } else if(n>customers.length){ + return maxTime; + } else if(n===1){ + return customers.reduce((a,b) => a+b); + } else{ + let tills = new Array(n).fill(0); + for(let chekoutTime of customers){ + let nextTill = tills.indexOf(Math.min(...tills)); + tills[nextTill] += chekoutTime; + } + return Math.max(...tills); + } + } \ No newline at end of file diff --git a/6 - Tribonacci Sequence.js b/6 - Tribonacci Sequence.js new file mode 100644 index 0000000..e2977f7 --- /dev/null +++ b/6 - Tribonacci Sequence.js @@ -0,0 +1,18 @@ +// https://www.codewars.com/kata/556deca17c58da83c00002db + +function tribonacci(signature,n){ + if(n===0){ + return []; + } else if(n functions.reduce((res, fnc)=>fnc(res), input); +} \ No newline at end of file diff --git a/6 - Which are in.js b/6 - Which are in.js new file mode 100644 index 0000000..51959b2 --- /dev/null +++ b/6 - Which are in.js @@ -0,0 +1,14 @@ +//https://www.codewars.com/kata/550554fd08b86f84fe000a58 + +function inArray(array1,array2){ + let result = []; + for (const element of array2){ + for(let i = 0; i word.charAt(word.search(/[1-9]/))+word).sort().map(word => word.substring(1)).join(' '); + } \ No newline at end of file