Skip to content

Commit

Permalink
5 - 6 kyu solutions (2021.06.05.)
Browse files Browse the repository at this point in the history
  • Loading branch information
katalin-kovacs committed Jun 5, 2021
0 parents commit 2be6071
Show file tree
Hide file tree
Showing 33 changed files with 482 additions and 0 deletions.
14 changes: 14 additions & 0 deletions 5 - Directions Reduction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// https://www.codewars.com/kata/550f22f4d758534c1100025a

function dirReduc(arr){
for(let i = 0; i<arr.length; i++){
if((arr[i] === 'NORTH' && arr[i+1] === 'SOUTH') || (arr[i] === 'SOUTH' && arr[i+1] === 'NORTH')){
arr.splice(i, 2);
i=-1;
} else if((arr[i] === 'EAST' && arr[i+1] === 'WEST') || (arr[i] === 'WEST' && arr[i+1] === 'EAST')){
arr.splice(i, 2);
i=-1;
}
}
return arr;
}
17 changes: 17 additions & 0 deletions 5 - Formatting a number as price.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//https://www.codewars.com/kata/5318f00b31b30925fd0001f8

const numberToPrice = function(number) {
if(isNaN(number)|| number.length <=1){
return 'NaN';
} else{
let num = String(number).split('.');
let fraction ='00';
if(num.length===2){
fraction = num[1].slice(0,2);
if(fraction.length === 1){
fraction+='0';
}
}
return `${new Intl.NumberFormat('en-EN',{maximumFractionDigits:0}).format(num[0])}.${fraction}`;
}
}
5 changes: 5 additions & 0 deletions 5 - Regex Password Validation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// https://www.codewars.com/kata/52e1476c8147a7547a000811

function validate(password) {
return /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[A-Za-z\d]{6,}$/.test(password);
}
12 changes: 12 additions & 0 deletions 5 - Rot13.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// https://www.codewars.com/kata/530e15517bc88ac656000716

function rot13(message){
//a: 97 z:122
//return message.split('').map(c => 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('');
}
4 changes: 4 additions & 0 deletions 5 - Where my anagrams at.js
Original file line number Diff line number Diff line change
@@ -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(''));
}
5 changes: 5 additions & 0 deletions 6 - Sum of Digits - Digital Root.js
Original file line number Diff line number Diff line change
@@ -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);
}
12 changes: 12 additions & 0 deletions 6 - Turn String Input into Hash.js
Original file line number Diff line number Diff line change
@@ -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;
}
26 changes: 26 additions & 0 deletions 6 - Are they the same.js
Original file line number Diff line number Diff line change
@@ -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<array1Sorted.length; i++){
if(array1Sorted[i]*array1Sorted[i] === array2Sorted[i]){
result = true;
} else {
result = false;
break;
}
}
}
return result;
}
5 changes: 5 additions & 0 deletions 6 - Bit Counting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// https://www.codewars.com/kata/526571aae218b8ee490006f4

const countBits = function(n) {
return Number(n).toString(2).split('').reduce((x,y) => Number(x)+Number(y), 0);
};
9 changes: 9 additions & 0 deletions 6 - Count characters in your string.js
Original file line number Diff line number Diff line change
@@ -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;
}
20 changes: 20 additions & 0 deletions 6 - Counting Duplicates.js
Original file line number Diff line number Diff line change
@@ -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;
}
9 changes: 9 additions & 0 deletions 6 - Fibonacci, Tribonacci and friends.js
Original file line number Diff line number Diff line change
@@ -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;
}
10 changes: 10 additions & 0 deletions 6 - Find the odd int.js
Original file line number Diff line number Diff line change
@@ -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);
}
17 changes: 17 additions & 0 deletions 6 - Give me a Diamond.js
Original file line number Diff line number Diff line change
@@ -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';
}
37 changes: 37 additions & 0 deletions 6 - Help the bookseller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// https://www.codewars.com/kata/54dc6f5a224c26032800005c

function stockList(listOfArt, listOfCat){
let result = {};
for(let i = 0; i<listOfCat.length; i++){
result[listOfCat[i]] = 0;
}

if(listOfArt.length === 0 || listOfCat === 0){
return '';
} else {
let catalog = listOfArt.reduce(function(acc, curr, i){
i = curr.substring(0, curr.indexOf(' '));
curr = curr.substring(curr.indexOf(' ')+1);
acc[i] = curr;
return acc;
}, {});
console.log('Categories: ', listOfCat);

let catalogKeys = Object.keys(catalog).map(x => x[0]);
let catalogValues = Object.values(catalog);

for(let i = 0; i < listOfCat.length; i++){
for(let j = 0; j<catalogKeys.length; j++){
if(catalogKeys[j] === listOfCat[i]){
result[listOfCat[i]] = Number(result[listOfCat[i]]) + Number(catalogValues[j]);
}
}
}

let renderResult = res =>{
return Object.keys(res).map(k => `(${k} : ${res[k]})`).join(' - ');
}

return renderResult(result);
}
}
18 changes: 18 additions & 0 deletions 6 - IQ Test.js
Original file line number Diff line number Diff line change
@@ -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;
}
16 changes: 16 additions & 0 deletions 6 - If you can read this.js
Original file line number Diff line number Diff line change
@@ -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);
}
21 changes: 21 additions & 0 deletions 6 - Longest alphabetical substring.js
Original file line number Diff line number Diff line change
@@ -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;
}
11 changes: 11 additions & 0 deletions 6 - Mexican Wave.js
Original file line number Diff line number Diff line change
@@ -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;
}
12 changes: 12 additions & 0 deletions 6 - Moves in squared strings (II).js
Original file line number Diff line number Diff line change
@@ -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);
}
2 changes: 2 additions & 0 deletions 6 - One line task Square Every Digit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// https://www.codewars.com/kata/5acd142a2ec8c48521000104
sd=x=>+[...x+''].map(d=>d*d).join``
31 changes: 31 additions & 0 deletions 6 - Parse HTML-CSS Colors.js
Original file line number Diff line number Diff line change
@@ -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;
}
17 changes: 17 additions & 0 deletions 6 - Replace With Alphabet Position.js
Original file line number Diff line number Diff line change
@@ -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<text.length; i++){
let code = text.toLowerCase().charCodeAt(i);
if (code > 96 && code < 123){
result += (code-compensateAscii) + ' ';
}
}

return result.slice(0, result.length-1);
}
Loading

0 comments on commit 2be6071

Please sign in to comment.