-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c8965a3
commit 9746c4b
Showing
22 changed files
with
354 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
15 changes: 15 additions & 0 deletions
15
codewars/Convert string to camel case/Convert string to camel case.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ''; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
44 changes: 44 additions & 0 deletions
44
codewars/Linked Lists - Sorted Insert/Linked Lists - Sorted Insert.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(' -> '); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
12 changes: 12 additions & 0 deletions
12
codewars/Moving Zeros To The End/Moving Zeros To The End.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} |
12 changes: 12 additions & 0 deletions
12
codewars/Product of consecutive Fib numbers/Product of consecutive Fib numbers.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(' '); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |