diff --git a/codewars/Adding Big Numbers/Adding Big Numbers.js b/codewars/Adding Big Numbers/Adding Big Numbers.js new file mode 100644 index 0000000..ef82f87 --- /dev/null +++ b/codewars/Adding Big Numbers/Adding Big Numbers.js @@ -0,0 +1,18 @@ +function add(a, b) { + let carry = 0; + let result = ''; + const maxLength = Math.max(a.length, b.length); + a = a.padStart(maxLength, '0'); + b = b.padStart(maxLength, '0'); + for (let i = maxLength - 1; i >= 0; i--) { + const digit1 = parseInt(a[i]); + const digit2 = parseInt(b[i]); + const sum = digit1 + digit2 + carry; + carry = Math.floor(sum / 10); + result = (sum % 10) + result; + } + if (carry > 0) { + result = carry + result; + } + return result; +} \ No newline at end of file diff --git a/codewars/Anagram difference/Anagram difference.js b/codewars/Anagram difference/Anagram difference.js new file mode 100644 index 0000000..d52de60 --- /dev/null +++ b/codewars/Anagram difference/Anagram difference.js @@ -0,0 +1,25 @@ +function anagramDifference(w1,w2){ + const freq1 = {}; + const freq2 = {}; + w1 = w1.toLowerCase(); + w2 = w2.toLowerCase(); + for (const char of w1) { + freq1[char] = (freq1[char] || 0) + 1; + } + for (const char of w2) { + freq2[char] = (freq2[char] || 0) + 1; + } + let difference = 0; + for (const char in freq1) { + if (!freq2[char]) { + difference += freq1[char]; + } else { + difference += Math.abs(freq1[char] - freq2[char]); + delete freq2[char]; + } + } + for (const char in freq2) { + difference += freq2[char]; + } + return difference; +} \ No newline at end of file diff --git a/codewars/Array Deep Count/Array Deep Count.js b/codewars/Array Deep Count/Array Deep Count.js new file mode 100644 index 0000000..e63a625 --- /dev/null +++ b/codewars/Array Deep Count/Array Deep Count.js @@ -0,0 +1,17 @@ +function deepCount(a){ + let count = 0; + function countRecursively(subArr) { + for (let i = 0; i < subArr.length; i++) { + const element = subArr[i]; + + if (Array.isArray(element)) { + countRecursively(element); + count++; + } else { + count++; + } + } + } + countRecursively(a); + return count; +} \ No newline at end of file diff --git a/codewars/Build Tower/Build Tower.js b/codewars/Build Tower/Build Tower.js new file mode 100644 index 0000000..1850ab6 --- /dev/null +++ b/codewars/Build Tower/Build Tower.js @@ -0,0 +1,11 @@ +function towerBuilder(nFloors) { + const tower = []; + const maxWidth = 2 * nFloors - 1; + for (let i = 1; i <= nFloors; i++) { + const numBlocks = 2 * i - 1; + const numSpaces = (maxWidth - numBlocks) / 2; + const floorStr = ' '.repeat(numSpaces) + '*'.repeat(numBlocks) + ' '.repeat(numSpaces); + tower.push(floorStr); + } + return tower; +} \ No newline at end of file diff --git a/codewars/Convert string to camel case/Convert string to camel case.js b/codewars/Convert string to camel case/Convert string to camel case.js new file mode 100644 index 0000000..284a43d --- /dev/null +++ b/codewars/Convert string to camel case/Convert string to camel case.js @@ -0,0 +1,15 @@ +function toCamelCase(text) { + const words = text.split(/[-_]/); + if (words.length === 0) { + return ""; + } else if (words.length === 1) { + return words[0]; + } else { + const result = words[0] + words.slice(1).map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(''); + if (text.charAt(0).toUpperCase() === text.charAt(0)) { + return result; + } else { + return result.charAt(0).toLowerCase() + result.slice(1); + } + } +} \ No newline at end of file diff --git a/codewars/Duplicate Encoder/Duplicate Encoder.js b/codewars/Duplicate Encoder/Duplicate Encoder.js new file mode 100644 index 0000000..65a744e --- /dev/null +++ b/codewars/Duplicate Encoder/Duplicate Encoder.js @@ -0,0 +1,10 @@ +function duplicateEncode(word) { + const charCount = new Map(); + word.toLowerCase().split('').forEach(char => { + charCount.set(char, (charCount.get(char) || 0) + 1); + }); + const result = word.toLowerCase().split('').map(char => { + return charCount.get(char) === 1 ? '(' : ')'; + }).join(''); + return result; +} \ No newline at end of file diff --git a/codewars/Find the missing letter/Find the missing letter.js b/codewars/Find the missing letter/Find the missing letter.js new file mode 100644 index 0000000..274fcc1 --- /dev/null +++ b/codewars/Find the missing letter/Find the missing letter.js @@ -0,0 +1,8 @@ +function findMissingLetter(array) { + for (let i = 0; i < array.length - 1; i++) { + if (array[i].charCodeAt(0) + 1 !== array[i + 1].charCodeAt(0)) { + return String.fromCharCode(array[i].charCodeAt(0) + 1); + } + } + return ''; +} \ No newline at end of file diff --git a/codewars/Flatten a Nested Map/Flatten a Nested Map.js b/codewars/Flatten a Nested Map/Flatten a Nested Map.js new file mode 100644 index 0000000..88d298b --- /dev/null +++ b/codewars/Flatten a Nested Map/Flatten a Nested Map.js @@ -0,0 +1,17 @@ +function flattenMap(map) { + const result = {}; + function recurse(current, parentKey = '') { + for (let key in current) { + const newKey = parentKey ? `${parentKey}/${key}` : key; + if (current[key] === null || current[key] === undefined) { + result[newKey] = null; + } else if (typeof current[key] === 'object' && !Array.isArray(current[key])) { + recurse(current[key], newKey); + } else { + result[newKey] = current[key]; + } + } + } + recurse(map); + return result; +} \ No newline at end of file diff --git a/codewars/Fun with tree - max sum/Fun with trees.js b/codewars/Fun with tree - max sum/Fun with trees.js new file mode 100644 index 0000000..4f9d462 --- /dev/null +++ b/codewars/Fun with tree - max sum/Fun with trees.js @@ -0,0 +1,13 @@ +var TreeNode = function(value, left, right) { + this.value = value; + this.left = left || null; + this.right = right || null; +}; +function maxSum(root) { + if (root === null) { + return 0; + } + const leftSum = maxSum(root.left); + const rightSum = maxSum(root.right); + return Math.max(leftSum, rightSum) + root.value; +} \ No newline at end of file diff --git a/codewars/Linked Lists - Sorted Insert/Linked Lists - Sorted Insert.js b/codewars/Linked Lists - Sorted Insert/Linked Lists - Sorted Insert.js new file mode 100644 index 0000000..58dfa4e --- /dev/null +++ b/codewars/Linked Lists - Sorted Insert/Linked Lists - Sorted Insert.js @@ -0,0 +1,44 @@ +function Node(data) { + this.data = data; + this.next = null; + } + +function sortedInsert(head, data) { + const newNode = new Node(data); + if (head === null || data < head.data) { + newNode.next = head; + return newNode; + } + let current = head; + while (current.next !== null && data >= current.next.data) { + current = current.next; + } + newNode.next = current.next; + current.next = newNode; + return head; +} + +function buildLinkedList(arr) { + let head = null; + let tail = null; + for (const data of arr) { + const newNode = new Node(data); + if (head === null) { + head = newNode; + tail = newNode; + } else { + tail.next = newNode; + tail = newNode; + } + } + return head; +} +function printLinkedList(head) { + const result = []; + let current = head; + while (current !== null) { + result.push(current.data); + current = current.next; + } + return result.join(' -> '); +} \ No newline at end of file diff --git a/codewars/Merge two arrays/Merge two arrays.js b/codewars/Merge two arrays/Merge two arrays.js new file mode 100644 index 0000000..88a8569 --- /dev/null +++ b/codewars/Merge two arrays/Merge two arrays.js @@ -0,0 +1,9 @@ +function mergeArrays(a, b) { + const maxLength = Math.max(a.length, b.length); + let result = []; + for (let i = 0; i < maxLength; i++) { + result.push(a[i]); + result.push(b[i]); + } + return result.filter((value) => value !== undefined); +} \ No newline at end of file diff --git a/codewars/Moving Zeros To The End/Moving Zeros To The End.js b/codewars/Moving Zeros To The End/Moving Zeros To The End.js new file mode 100644 index 0000000..70ee91f --- /dev/null +++ b/codewars/Moving Zeros To The End/Moving Zeros To The End.js @@ -0,0 +1,12 @@ +function moveZeros(arr) { + let zeroCount = 0; + for (let i = 0; i < arr.length; i++) { + if (arr[i] !== 0) { + arr[zeroCount++] = arr[i]; + } + } + for (let i = zeroCount; i < arr.length; i++) { + arr[i] = 0; + } + return arr; +} \ No newline at end of file diff --git a/codewars/Permutations/So Many Permutations.js b/codewars/Permutations/So Many Permutations.js new file mode 100644 index 0000000..280210b --- /dev/null +++ b/codewars/Permutations/So Many Permutations.js @@ -0,0 +1,15 @@ +function permutations(string) { + if (string.length <= 1) { + return [string]; + } + const result = []; + for (let i = 0; i < string.length; i++) { + const char = string[i]; + const remainingChars = string.slice(0, i) + string.slice(i + 1); + const perms = permutations(remainingChars); + for (const perm of perms) { + result.push(char + perm); + } + } + return Array.from(new Set(result)); +} \ No newline at end of file diff --git a/codewars/Product of consecutive Fib numbers/Product of consecutive Fib numbers.js b/codewars/Product of consecutive Fib numbers/Product of consecutive Fib numbers.js new file mode 100644 index 0000000..ea94dbb --- /dev/null +++ b/codewars/Product of consecutive Fib numbers/Product of consecutive Fib numbers.js @@ -0,0 +1,12 @@ +function productFib(prod) { + let n = 0; + let fibN = 0; + let fibNPlus1 = 1; + while (fibN * fibNPlus1 < prod) { + const temp = fibN; + fibN = fibNPlus1; + fibNPlus1 = temp + fibNPlus1; + n++; + } + return [fibN, fibNPlus1, fibN * fibNPlus1 === prod]; +} \ No newline at end of file diff --git a/codewars/Simple Pig Latin/Simple Pig Latin.js b/codewars/Simple Pig Latin/Simple Pig Latin.js new file mode 100644 index 0000000..09e7e4a --- /dev/null +++ b/codewars/Simple Pig Latin/Simple Pig Latin.js @@ -0,0 +1,12 @@ +function pigIt(str) { + const words = str.split(' '); + function wordToPigLatin(word) { + if (/^[a-zA-Z]+$/.test(word)) { + return word.slice(1) + word[0] + 'ay'; + } else { + return word; + } + } + const pigLatinWords = words.map(wordToPigLatin); + return pigLatinWords.join(' '); +} \ No newline at end of file diff --git a/codewars/Snail/Snail.js b/codewars/Snail/Snail.js new file mode 100644 index 0000000..f649512 --- /dev/null +++ b/codewars/Snail/Snail.js @@ -0,0 +1,20 @@ +snail = function(array) { + const result = []; + while (array.length > 0) { + result.push(...array.shift()); + for (let i = 0; i < array.length; i++) { + result.push(array[i].pop()); + } + if (array.length > 0) { + result.push(...array.pop().reverse()); + } + const leftColumn = []; + for (let i = array.length - 1; i >= 0; i--) { + if (array[i].length > 0) { + leftColumn.push(array[i].shift()); + } + } + result.push(...leftColumn); + } + return result; +} \ No newline at end of file diff --git a/codewars/Sum of Digits - Digital Root/Sum of Digits.js b/codewars/Sum of Digits - Digital Root/Sum of Digits.js new file mode 100644 index 0000000..9ca5315 --- /dev/null +++ b/codewars/Sum of Digits - Digital Root/Sum of Digits.js @@ -0,0 +1,11 @@ +function digitalRoot(n) { + if (n < 10) { + return n; + } + let sum = 0; + while (n > 0) { + sum += n % 10; + n = Math.floor(n / 10); + } + return digitalRoot(sum); +} \ No newline at end of file diff --git a/codewars/Sum of Intervals/Sum of Intervals.js b/codewars/Sum of Intervals/Sum of Intervals.js new file mode 100644 index 0000000..82c3e66 --- /dev/null +++ b/codewars/Sum of Intervals/Sum of Intervals.js @@ -0,0 +1,19 @@ +function sumIntervals(intervals) { + intervals.sort((a, b) => a[0] - b[0]); + let mergedIntervals = [intervals[0]]; + for (let i = 1; i < intervals.length; i++) { + const currentInterval = intervals[i]; + const lastMergedInterval = mergedIntervals[mergedIntervals.length - 1]; + + if (currentInterval[0] <= lastMergedInterval[1]) { + lastMergedInterval[1] = Math.max(lastMergedInterval[1], currentInterval[1]); + } else { + mergedIntervals.push(currentInterval); + } + } + let sum = 0; + for (const interval of mergedIntervals) { + sum += interval[1] - interval[0]; + } + return sum; +} \ No newline at end of file diff --git a/codewars/Sum of pairs/Sum of Pairs.js b/codewars/Sum of pairs/Sum of Pairs.js new file mode 100644 index 0000000..0c252b7 --- /dev/null +++ b/codewars/Sum of pairs/Sum of Pairs.js @@ -0,0 +1,12 @@ +function sumPairs(ints, s) { + const seenValues = new Set(); + for (let i = 0; i < ints.length; i++) { + const currentNum = ints[i]; + const complement = s - currentNum; + if (seenValues.has(complement)) { + return [complement, currentNum]; + } + seenValues.add(currentNum); + } + return undefined; +} \ No newline at end of file diff --git a/codewars/Tic-Tac-Toe Checker/Tic-Tac-Toe Checker.js b/codewars/Tic-Tac-Toe Checker/Tic-Tac-Toe Checker.js new file mode 100644 index 0000000..bd3850d --- /dev/null +++ b/codewars/Tic-Tac-Toe Checker/Tic-Tac-Toe Checker.js @@ -0,0 +1,26 @@ +function isSolved(board) { + for (let player = 1; player <= 2; player++) { + for (let i = 0; i < 3; i++) { + if (board[i][0] === player && board[i][1] === player && board[i][2] === player) { + return player; + } + if (board[0][i] === player && board[1][i] === player && board[2][i] === player) { + return player; + } + } + if ( + (board[0][0] === player && board[1][1] === player && board[2][2] === player) || + (board[0][2] === player && board[1][1] === player && board[2][0] === player) + ) { + return player; + } + } + for (let i = 0; i < 3; i++) { + for (let j = 0; j < 3; j++) { + if (board[i][j] === 0) { + return -1; + } + } + } + return 0; +} \ 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 0000000..8961a80 --- /dev/null +++ b/codewars/Valid Parentheses/Valid Parentheses.js @@ -0,0 +1,13 @@ +function validParentheses(parens) { + const stack = []; + for (let char of parens) { + if (char === '(') { + stack.push(char); + } else if (char === ')') { + if (stack.length === 0 || stack.pop() !== '(') { + return false; + } + } + } + return stack.length === 0; +} \ No newline at end of file diff --git a/codewars/Where my anagrams at/Where my anagrams at.js b/codewars/Where my anagrams at/Where my anagrams at.js new file mode 100644 index 0000000..7a46a58 --- /dev/null +++ b/codewars/Where my anagrams at/Where my anagrams at.js @@ -0,0 +1,15 @@ +function areAnagrams(word1, word2) { + const sortedWord1 = word1.split('').sort().join(''); + const sortedWord2 = word2.split('').sort().join(''); + return sortedWord1 === sortedWord2; + } + function anagrams(word, words) { + const result = []; + for (let i = 0; i < words.length; i++) { + if (areAnagrams(word, words[i])) { + result.push(words[i]); + } + } + + return result; +} \ No newline at end of file