Skip to content

Commit

Permalink
CodeWars gotov
Browse files Browse the repository at this point in the history
  • Loading branch information
Sispeks228 committed Oct 2, 2023
1 parent c8965a3 commit 9746c4b
Show file tree
Hide file tree
Showing 22 changed files with 354 additions and 0 deletions.
18 changes: 18 additions & 0 deletions codewars/Adding Big Numbers/Adding Big Numbers.js
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;
}
25 changes: 25 additions & 0 deletions codewars/Anagram difference/Anagram difference.js
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;
}
17 changes: 17 additions & 0 deletions codewars/Array Deep Count/Array Deep Count.js
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;
}
11 changes: 11 additions & 0 deletions codewars/Build Tower/Build Tower.js
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;
}
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);
}
}
}
10 changes: 10 additions & 0 deletions codewars/Duplicate Encoder/Duplicate Encoder.js
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;
}
8 changes: 8 additions & 0 deletions codewars/Find the missing letter/Find the missing letter.js
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 '';
}
17 changes: 17 additions & 0 deletions codewars/Flatten a Nested Map/Flatten a Nested Map.js
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;
}
13 changes: 13 additions & 0 deletions codewars/Fun with tree - max sum/Fun with trees.js
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;
}
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(' -> ');
}
9 changes: 9 additions & 0 deletions codewars/Merge two arrays/Merge two arrays.js
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 codewars/Moving Zeros To The End/Moving Zeros To The End.js
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;
}
15 changes: 15 additions & 0 deletions codewars/Permutations/So Many Permutations.js
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));
}
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];
}
12 changes: 12 additions & 0 deletions codewars/Simple Pig Latin/Simple Pig Latin.js
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(' ');
}
20 changes: 20 additions & 0 deletions codewars/Snail/Snail.js
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;
}
11 changes: 11 additions & 0 deletions codewars/Sum of Digits - Digital Root/Sum of Digits.js
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);
}
19 changes: 19 additions & 0 deletions codewars/Sum of Intervals/Sum of Intervals.js
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;
}
12 changes: 12 additions & 0 deletions codewars/Sum of pairs/Sum of Pairs.js
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;
}
26 changes: 26 additions & 0 deletions codewars/Tic-Tac-Toe Checker/Tic-Tac-Toe Checker.js
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;
}
13 changes: 13 additions & 0 deletions codewars/Valid Parentheses/Valid Parentheses.js
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;
}
15 changes: 15 additions & 0 deletions codewars/Where my anagrams at/Where my anagrams at.js
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;
}

0 comments on commit 9746c4b

Please sign in to comment.