diff --git a/codewars/Adding Big Numbers/addingBigNumbers.js b/codewars/Adding Big Numbers/addingBigNumbers.js new file mode 100644 index 000000000..5f0b7229b --- /dev/null +++ b/codewars/Adding Big Numbers/addingBigNumbers.js @@ -0,0 +1,18 @@ +function add(a, b) { + let result = ''; + let carry = 0; + let i = a.length - 1; + let j = b.length - 1; + + while (i >= 0 || j >= 0 || carry) { + let sum = carry; + if (i >= 0) sum += +a[i--]; + if (j >= 0) sum += +b[j--]; + result = (sum % 10) + result; + carry = Math.floor(sum / 10); + } + + return result; +} + +console.log(add("123","321")); \ No newline at end of file diff --git a/codewars/Anagram difference/anagramDifference.js b/codewars/Anagram difference/anagramDifference.js new file mode 100644 index 000000000..798b518a7 --- /dev/null +++ b/codewars/Anagram difference/anagramDifference.js @@ -0,0 +1,11 @@ +function anagramDifference(w1,w2){ + let w3 = 0; + for(let i = 0; i < w1.length; i++) { + if(w2.includes(w1[i])){ + w2 = w2.replace(w1[i], 1); + w3++; + } + } +return w1.length + w2.length - 2 * w3; +} +console.log(anagramDifference("codewars", "hackerrank")); \ No newline at end of file diff --git a/codewars/Array Deep Count/arrayDeepCount.js b/codewars/Array Deep Count/arrayDeepCount.js new file mode 100644 index 000000000..4bee4bcbd --- /dev/null +++ b/codewars/Array Deep Count/arrayDeepCount.js @@ -0,0 +1,8 @@ +function deepCount(a){ + let count = a.length; + for (let i = 0; i < a.length; i++) { + if (Array.isArray(a[i])) count += deepCount(a[i]); + } + return count +} +console.log(deepCount([1,2,3,4,[5]])); \ No newline at end of file diff --git a/codewars/Build Tower/buildTower.js b/codewars/Build Tower/buildTower.js new file mode 100644 index 000000000..90804f718 --- /dev/null +++ b/codewars/Build Tower/buildTower.js @@ -0,0 +1,12 @@ +function towerBuilder(nFloors) { + const pyramid = []; + for (let i = 1; i <= nFloors; i++) { + const spaces = ' '.repeat(nFloors - i); + const stars = '*'.repeat(2 * i - 1); + pyramid.push(spaces + stars + spaces); + } + return pyramid; +} +const numberOfFloors = 5; +const pyramidArray = towerBuilder(numberOfFloors); +console.log(pyramidArray.join('\n')); \ No newline at end of file diff --git a/codewars/Convert string to camel case/camelCase.js b/codewars/Convert string to camel case/camelCase.js new file mode 100644 index 000000000..fe658fdf9 --- /dev/null +++ b/codewars/Convert string to camel case/camelCase.js @@ -0,0 +1,13 @@ +function toCamelCase1(str){ + let result = ""; + for (let i = 0; i < str.length; i++) { + if (str[i] === '_' || str[i] === '-') { + i++; + result += str[i].toUpperCase(); + } else { + result += str[i]; + } + } + return result; + } + console.log(toCamelCase1("the-stealth-warrior")); \ No newline at end of file diff --git a/codewars/Duplicate Encoder/duplicate.js b/codewars/Duplicate Encoder/duplicate.js new file mode 100644 index 000000000..a508a4365 --- /dev/null +++ b/codewars/Duplicate Encoder/duplicate.js @@ -0,0 +1,23 @@ +function duplicateEncode(word) { + const counting = {}; + let stroke = ""; + + for (let i = 0; i < word.length; i++) { + const element = word[i].toLowerCase(); + counting[element] = (counting[element] || 0) + 1; + } + + for (let i = 0; i < word.length; i++) { + const char = word[i].toLowerCase(); + if (counting[char] > 1) { + stroke += ")"; + } else { + stroke += "("; + } + } + return stroke; + } +console.log(duplicateEncode("din")); +console.log(duplicateEncode("recede")); +console.log(duplicateEncode("Success")); +console.log(duplicateEncode("(( @" )); diff --git a/codewars/Find the missing letter/find_nissing_letter.js b/codewars/Find the missing letter/find_nissing_letter.js new file mode 100644 index 000000000..1816c7e69 --- /dev/null +++ b/codewars/Find the missing letter/find_nissing_letter.js @@ -0,0 +1,12 @@ +function findMissingLetter(array) +{ + let first = array[0].charCodeAt(0); + for (let i = 0; i < array.length; i++) { + if (array[i].charCodeAt(0) !== first + i) { + return String.fromCharCode(first + i); + } + } + return null +} +console.log(findMissingLetter(['a','b','c','d','f'])); +console.log(findMissingLetter(['O','Q','R','S'])); \ No newline at end of file diff --git a/codewars/Flatten a Nested Map/flatten_nested_map.js b/codewars/Flatten a Nested Map/flatten_nested_map.js new file mode 100644 index 000000000..fdba83155 --- /dev/null +++ b/codewars/Flatten a Nested Map/flatten_nested_map.js @@ -0,0 +1,33 @@ +function flattenMap(map) { + const result = {}; + + function flatten(obj, parentKey = '') { + for (let key in obj) { + if (obj.hasOwnProperty(key)) { + const newKey = parentKey ? `${parentKey}/${key}` : key; + + if (typeof obj[key] === 'object' && obj[key] !== null && !Array.isArray(obj[key])) { + flatten(obj[key], newKey); + } else { + result[newKey] = obj[key]; + } + } + } + } + + flatten(map); + return result; + } + + const input = { + 'a': { + 'b': { + 'c': 12, + 'd': 'Hello World' + }, + 'e': [1, 2, 3] + } + }; + + const output = flattenMap(input); + console.log(output); \ No newline at end of file diff --git a/codewars/Fun with tree - max sum/tree_maxSum.js b/codewars/Fun with tree - max sum/tree_maxSum.js new file mode 100644 index 000000000..c62695b55 --- /dev/null +++ b/codewars/Fun with tree - max sum/tree_maxSum.js @@ -0,0 +1,35 @@ +class TreeNode { + constructor(value, left = null, right = null) { + this.value = value; + this.left = left; + this.right = right; + } +} + +function maxSum(root) { + if (root === null) { + return 0; + } + if (root.left === null && root.right === null) { + return root.value; +} + let leftSum = maxSum(root.left); + let rightSum = maxSum(root.right); + let maxChildSum = Math.max(leftSum, rightSum); + + return root.value + maxChildSum; + +} + +let root = new TreeNode(17, + new TreeNode(3, + new TreeNode(2) + ), + new TreeNode(-10, + new TreeNode(16), + new TreeNode(1, + new TreeNode(13) + ) + ) +); +console.log(maxSum(root)); \ No newline at end of file diff --git a/codewars/Linked Lists - Sorted Insert/linked_list.js b/codewars/Linked Lists - Sorted Insert/linked_list.js new file mode 100644 index 000000000..d2982f50e --- /dev/null +++ b/codewars/Linked Lists - Sorted Insert/linked_list.js @@ -0,0 +1,49 @@ +class Node { + constructor(data) { + this.data = data; + this.next = null; + } +} + +function push(head, data) { + let newNode = new Node(data); + newNode.next = head; + return newNode; +} + +function buildOneTwoThree() { + let head = null; + head = push(head, 3); + head = push(head, 2); + head = push(head, 1); + return head; +} + +function sortedInsert(head, data) { + let newNode = new Node(data); + + if (head === null || data < head.data) { + newNode.next = head; + return newNode; + } + + let current = head; + while (current.next !== null && current.next.data < data) { + current = current.next; + } + + newNode.next = current.next; + current.next = newNode; + + return head; +} + +let head = buildOneTwoThree(); +head = sortedInsert(head, 4); + +let current = head; +while (current !== null) { + process.stdout.write(current.data + " -> "); + current = current.next; +} +console.log("null"); \ No newline at end of file diff --git a/codewars/Merge two arrays/merge2arrays.js b/codewars/Merge two arrays/merge2arrays.js new file mode 100644 index 000000000..bcb0b64f3 --- /dev/null +++ b/codewars/Merge two arrays/merge2arrays.js @@ -0,0 +1,17 @@ +function mergeArrays(a, b) { + const result = []; + const maxLength = Math.max(a.length, b.length); + for (let i = 0; i < maxLength; i++) { + if (i < a.length) { + result.push(a[i]); + } + if (i < b.length) { + result.push(b[i]); + } + } + return result; + } + console.log(mergeArrays([1, 2, 3, 4, 5, 6, 7, 8], ['a', 'b', 'c', 'd', 'e'])); + console.log(mergeArrays(['a', 'b', 'c', 'd', 'e'], [1, 2, 3, 4, 5])); + console.log(mergeArrays([2, 5, 8, 23, 67, 6], ['b', 'r', 'a', 'u', 'r', 's'])); + console.log(mergeArrays(['b', 'r', 'a', 'u', 'r', 's', 'e', 'q', 'z'], [2, 5, 8, 23, 67, 6])); \ No newline at end of file diff --git a/codewars/Moving Zeros To The End/movingZerosToTheEnd.js b/codewars/Moving Zeros To The End/movingZerosToTheEnd.js new file mode 100644 index 000000000..330185f91 --- /dev/null +++ b/codewars/Moving Zeros To The End/movingZerosToTheEnd.js @@ -0,0 +1,14 @@ +function moveZerosToEnd(array) { + let nonZeroCount = 0; + for (let i = 0; i < array.length; i++) { + if (array[i] !== 0) { + array[nonZeroCount] = array[i]; + nonZeroCount++; + } + } + for (let i = nonZeroCount; i < array.length; i++) { + array[i] = 0; + } + return array; + } + console.log(moveZerosToEnd([0,1,2,0,3,0,4])); \ No newline at end of file diff --git a/codewars/Permutations/permutations.js b/codewars/Permutations/permutations.js new file mode 100644 index 000000000..3f1fcbe19 --- /dev/null +++ b/codewars/Permutations/permutations.js @@ -0,0 +1,15 @@ +function permutations(string) { + if (string.length === 1) { + return [string]; + } + let result = new Set(); + for (let i = 0; i < string.length; i++) { + let remainingChars = string.slice(0, i) + string.slice(i + 1); + let permOfRemainingChars = permutations(remainingChars); + for (let permutation of permOfRemainingChars) { + result.add(string[i] + permutation); + } + } + return Array.from(result); +} +console.log(permutations('abc')); \ No newline at end of file diff --git a/codewars/Product of consecutive Fib numbers/fibonacci.js b/codewars/Product of consecutive Fib numbers/fibonacci.js new file mode 100644 index 000000000..3c9125010 --- /dev/null +++ b/codewars/Product of consecutive Fib numbers/fibonacci.js @@ -0,0 +1,15 @@ +function productFib(prod) { + let a = 0; + let b = 1; + + while (a * b < prod) { + const next = a + b; + a = b; + b = next; + } + + return [a, b, a * b === prod]; + } + + console.log(productFib(714)); + console.log(productFib(800)); \ No newline at end of file diff --git a/codewars/Simple Pig Latin/simplePig.js b/codewars/Simple Pig Latin/simplePig.js new file mode 100644 index 000000000..69253ea62 --- /dev/null +++ b/codewars/Simple Pig Latin/simplePig.js @@ -0,0 +1,9 @@ +function pigIt(str) { + return str.split(' ').map(word => { + if (/^[a-zA-Z]+$/.test(word)) { + return word.slice(1) + word[0] + 'ay'; + } + return word; + }).join(' '); +} +console.log(pigIt('Hello world !')); \ No newline at end of file diff --git a/codewars/Snail/snail.js b/codewars/Snail/snail.js new file mode 100644 index 000000000..5978bb278 --- /dev/null +++ b/codewars/Snail/snail.js @@ -0,0 +1,29 @@ +snail = function(array) { + let result = []; + while (array.length) { + result = result.concat(array.shift()); + + for (let i = 0; i < array.length; i++) { + if (array[i].length) { + result.push(array[i].pop()); + } + } + if (array.length) { + result = result.concat(array.pop().reverse()); + } + + for (let i = array.length - 1; i >= 0; i--) { + if (array[i].length) { + result.push(array[i].shift()); + } + } +} + return result; +} +let array = [ + [1,2,3], + [8,9,4], + [7,6,5] +]; + +console.log(snail(array)); \ No newline at end of file diff --git a/codewars/Sum of Digits - Digital Root/sumOfDigits.js b/codewars/Sum of Digits - Digital Root/sumOfDigits.js new file mode 100644 index 000000000..ec136e848 --- /dev/null +++ b/codewars/Sum of Digits - Digital Root/sumOfDigits.js @@ -0,0 +1,4 @@ +function digitalRoot(n) { + return (n - 1) % 9 + 1; + } + console.log(digitalRoot(125)) \ No newline at end of file diff --git a/codewars/Sum of Intervals/sumOfIntervals.js b/codewars/Sum of Intervals/sumOfIntervals.js new file mode 100644 index 000000000..92eddfb28 --- /dev/null +++ b/codewars/Sum of Intervals/sumOfIntervals.js @@ -0,0 +1,23 @@ +function sumIntervals(intervals) { + intervals.sort((a, b) => a[0] - b[0]); + let mergedIntervals = []; + let currentInterval = intervals[0]; + + for (let i = 1; i < intervals.length; i++) { + let nextInterval = intervals[i]; + if (currentInterval[1] >= nextInterval[0]) { + currentInterval[1] = Math.max(currentInterval[1], nextInterval[1]); + } else { + mergedIntervals.push(currentInterval); + currentInterval = nextInterval; + } + } + + mergedIntervals.push(currentInterval); + let totalLength = mergedIntervals.reduce((sum, interval) => sum + (interval[1] - interval[0]), 0); + + return totalLength; +} + +console.log(sumIntervals([[1, 4], [7, 10], [3, 5]])); +console.log(sumIntervals([[1, 5], [10, 20], [1, 6], [16, 19], [5, 11]])); \ No newline at end of file diff --git a/codewars/Sum of pairs/sumOfPairs.js b/codewars/Sum of pairs/sumOfPairs.js new file mode 100644 index 000000000..2dbffbde8 --- /dev/null +++ b/codewars/Sum of pairs/sumOfPairs.js @@ -0,0 +1,20 @@ +function sumPairs(ints, s) { + const seen = new Set(); + + for (let i = 0; i < ints.length; i++) { + const current = ints[i]; + const complement = s - current; + + if (seen.has(complement)) { + return [complement, current]; + } + + seen.add(current); + } + + return null; + } + +console.log(sumPairs([4, 3, 2, 3, 4], 6)); +console.log(sumPairs([0, 0, -2, 3], 2)); +console.log(sumPairs([10, 5, 2, 3, 7, 5], 10)); \ No newline at end of file diff --git a/codewars/Tic-Tac-Toe Checker/tic_tac_toe.js b/codewars/Tic-Tac-Toe Checker/tic_tac_toe.js new file mode 100644 index 000000000..3bb1e4477 --- /dev/null +++ b/codewars/Tic-Tac-Toe Checker/tic_tac_toe.js @@ -0,0 +1,45 @@ +function isSolved(board) { + for (let i = 0; i < 3; i++) { + if (board[i][0] === board[i][1] && board[i][1] === board[i][2] && board[i][0] !== 0) { + return board[i][0]; + } + } + + for (let i = 0; i < 3; i++) { + if (board[0][i] === board[1][i] && board[1][i] === board[2][i] && board[0][i] !== 0) { + return board[0][i]; + } + } + + if (board[0][0] === board[1][1] && board[1][1] === board[2][2] && board[0][0] !== 0) { + return board[0][0]; + } + if (board[0][2] === board[1][1] && board[1][1] === board[2][0] && board[0][2] !== 0) { + return board[0][2]; + } + + for (let i = 0; i < 3; i++) { + for (let j = 0; j < 3; j++) { + if (board[i][j] === 0) { + return -1; + } + } + } + + return 0; +} + + +let board1 = [ + [0, 0, 1], + [0, 1, 2], + [2, 1, 0] +]; +console.log(isSolved(board1)); + +let board2 = [ + [1, 1, 1], + [0, 2, 2], + [0, 0, 0] +]; +console.log(isSolved(board2)); \ No newline at end of file diff --git a/codewars/Valid Parentheses/valid_parentheses.js b/codewars/Valid Parentheses/valid_parentheses.js new file mode 100644 index 000000000..b5883d663 --- /dev/null +++ b/codewars/Valid Parentheses/valid_parentheses.js @@ -0,0 +1,19 @@ +function validParentheses(input) { + let stack = []; + for (let char of input) { + if (char === '(') { + stack.push(char); + } else if (char === ')') { + if (stack.length === 0) { + return false; + } + stack.pop(); + } + } + return stack.length === 0; +} + +console.log(validParentheses("()")); +console.log(validParentheses(")(()))")); +console.log(validParentheses("(")); +console.log(validParentheses("(())((()())())")); \ No newline at end of file diff --git a/codewars/Where my anagrams at/anagrams.js b/codewars/Where my anagrams at/anagrams.js new file mode 100644 index 000000000..8cbd4c2b8 --- /dev/null +++ b/codewars/Where my anagrams at/anagrams.js @@ -0,0 +1,11 @@ +function anagrams(word, words) { + function sortLetters(str) { + return str.split('').sort().join(''); + } + const sortedWord = sortLetters(word); + return words.filter(w => sortLetters(w) === sortedWord); +} + +console.log(anagrams('abba', ['aabb', 'abcd', 'bbaa', 'dada'])); +console.log(anagrams('racer', ['crazer', 'carer', 'racar', 'caers', 'racer'])); +console.log(anagrams('laser', ['lazing', 'lazy', 'lacer'])); \ No newline at end of file