Skip to content

Commit

Permalink
codewars_swag
Browse files Browse the repository at this point in the history
  • Loading branch information
m0onblvck authored and FeLL1kS committed Oct 17, 2024
1 parent 58b5956 commit 860ddcd
Show file tree
Hide file tree
Showing 18 changed files with 363 additions and 0 deletions.
14 changes: 14 additions & 0 deletions codewars/Adding Big Numbers/adding_Big_Numbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function add(a, b) {
let result = "";
let value = 0;
a = a.split("");
b = b.split("");

while (a.length || b.length || value) {
value += ~~a.pop() + ~~b.pop();
result = (value % 10) + result;
value = value > 9;
}

return result;
}
32 changes: 32 additions & 0 deletions codewars/Anagram difference/anagram_difference.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function anagramDifference(w1, w2) {
if (w1 === "" && w2 === "") {
return 0;
} else if (w1 === "" || w2 === "") {
if (w1 !== "") {
return w1.length;
} else {
return w2.length;
}
}
let letterCount = {};
if (w1 !== "" && w2 !== "") {
for (let letter of w1) {
letterCount[letter] = (letterCount[letter] || 0) + 1;
}

for (let letter of w2) {
if (letterCount[letter]) {
letterCount[letter]--;
} else {
letterCount[letter] = -1;
}
}
}
let lettersToRemove = 0;

for (let count of Object.values(letterCount)) {
lettersToRemove += Math.abs(count);
}

return lettersToRemove;
}
12 changes: 12 additions & 0 deletions codewars/Array Deep Count/array_Deep_Count.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function deepCount(a) {
let sumTotal = 0;

for (let element of a) {
sumTotal += 1;
if (Array.isArray(element)) {
sumTotal += deepCount(element);
}
}

return sumTotal;
}
10 changes: 10 additions & 0 deletions codewars/Build Tower/build_Tower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function towerBuilder(nFloors) {
const tower = [];

for (let i = 0; i < nFloors; i++) {
const stars = "*".repeat(2 * i + 1);
const spaces = " ".repeat(nFloors - i - 1);
tower.push(spaces + stars + spaces);
}
return tower;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function toCamelCase(str) {
if (str == "") {
return str;
}
const words = str.split(/[-_]/);

const upperCaseWords = words.map((word, index) => {
if (index === 0) {
return word.charAt(0) + word.slice(1).toLowerCase();
} else {
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
}
});

return upperCaseWords.join("");
}
16 changes: 16 additions & 0 deletions codewars/Duplicate Encoder/duplicate_Encoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function duplicateEncode(word) {
word = word.toLowerCase();

const charCount = {};

for (const char of word) {
charCount[char] = (charCount[char] || 0) + 1;
}

let result = "";
for (const char of word) {
result += charCount[char] === 1 ? "(" : ")";
}

return result;
}
10 changes: 10 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,10 @@
function findMissingLetter(array) {
for (let i = 0; i < array.length - 1; i++) {
const currChar = array[i].charCodeAt(0);
const nextChar = array[i + 1].charCodeAt(0);

if (nextChar !== currChar + 1) {
return String.fromCharCode(currChar + 1);
}
}
}
29 changes: 29 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,29 @@
function flattenMap(input) {
// Initialize the result object
const result = {};

// Recursive helper function
function flatten(current, prefix) {
for (const [key, value] of Object.entries(current)) {
const newKey = prefix ? `${prefix}/${key}` : key; // Create the new key with prefix

if (
value !== null &&
typeof value === "object" &&
!Array.isArray(value) &&
typeof value !== "function"
) {
// If the value is an object (but not null, array, or function), recurse
flatten(value, newKey);
} else {
// Otherwise, assign the value directly to the result
result[newKey] = value;
}
}
}

// Start the flattening process with the initial input
flatten(input, "");

return result;
}
19 changes: 19 additions & 0 deletions codewars/Linked Lists - Sorted Insert/sorted_Insert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function Node(data) {
this.data = data;
this.next = null;
}

function sortedInsert(head, data) {
const newNode = new Node(data);
if (!head || data < head.data) {
newNode.next = head;
return newNode;
}
let current = head;
while (current.next && current.next.data < data) {
current = current.next;
}
newNode.next = current.next;
current.next = newNode;
return head;
}
12 changes: 12 additions & 0 deletions codewars/Merge two arrays/merge_2_ arrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function mergeArrays(a, b) {
let resultLength = Math.max(a.length, b.length) * 2;
let merge = [a, b];
let result = [];

for (let i = 0; i < resultLength; i++) {
let value = merge[i % 2][Math.floor(i / 2)];
if (value != undefined) result.push(value);
}

return result;
}
18 changes: 18 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,18 @@
function moveZeros(arr) {
const zero = [];
const arr2 = [];

for (let i = 0; i < arr.length; i++) {
if (arr[i] === 0) {
zero.push(arr[i]);
} else {
arr2.push(arr[i]);
}
}

for (let j = 0; j < zero.length; j++) {
arr2.push(zero[j]);
}

return arr2;
}
18 changes: 18 additions & 0 deletions codewars/Permutations/so_Many_Permutations!.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function permutations(string) {
if (string.length <= 1) {
return [string];
}
let result = [];
for (let i = 0; i < string.length; i++) {
let char = string[i];
if (string.indexOf(char) != i) {
continue;
}
let remainingStr = string.slice(0, i) + string.slice(i + 1);
for (let permutation of permutations(remainingStr)) {
result.push(char + permutation);
}
}

return [...new Set(result)];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function productFib(prod) {
let first = 1;
let second = 1;
while (first * second < prod) {
second += first;
first = second - first;
}
return [first, second, first * second === prod];
}
29 changes: 29 additions & 0 deletions codewars/Simple Pig Latin/simple_Pig_Latin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function pigIt(str) {
str = str.split(" ");
let lastLetters = "ay";
let result = [];

for (let word of str) {
if (word === "!" || word === "?") {
result.push(word);
break;
}

word = word.split("");
let firstLetter = word.splice(0, 1)[0];
word.push(firstLetter, lastLetters);
let resultWord = "";

for (let char of word) {
resultWord += char;
}
result.push(resultWord);
}

let resStr = "";
result.forEach((str) => {
resStr += `${str} `;
});

return resStr.trimEnd();
}
37 changes: 37 additions & 0 deletions codewars/Snail/snail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
snail = function (array) {
if (array.length === 0) {
return [];
}

let result = [];
let top = 0;
let bottom = array.length - 1;
let left = 0;
let right = array[0].length - 1;

while (top <= bottom && left <= right) {
for (let i = left; i <= right; i++) {
result.push(array[top][i]);
}
top++;

for (let i = top; i <= bottom; i++) {
result.push(array[i][right]);
}
right--;

if (top <= bottom && left <= right) {
for (let i = right; i >= left; i--) {
result.push(array[bottom][i]);
}
bottom--;

for (let i = bottom; i >= top; i--) {
result.push(array[i][left]);
}
left++;
}
}

return result;
};
16 changes: 16 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,16 @@
function digitalRoot(n) {
let str = n.toString();
let result = 0;

for (const char of str) {
let num = parseInt(char);
result += num;
}

str = result.toString();
if (str.length >= "2") {
return digitalRoot(str);
}

return result;
}
20 changes: 20 additions & 0 deletions codewars/Sum of Intervals/sum_of_Intervals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function sumIntervals(intervals) {
intervals.sort((a, b) => a[0] - b[0]);

let sum = 0;
let currentEnd = -Infinity;

for (const [start, end] of intervals) {
if (start <= currentEnd && end >= currentEnd) {
sum += end - currentEnd;
currentEnd = Math.max(currentEnd, end);
} else if (end < currentEnd) {
continue;
} else {
sum += end - start;
currentEnd = end;
}
}

return sum;
}
46 changes: 46 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,46 @@
function isSolved(board) {
for (let i = 0; i < 3; i++) {
if (
board[i][0] !== 0 &&
board[i][0] === board[i][1] &&
board[i][0] === board[i][2]
) {
return board[i][0];
}
}

for (let j = 0; j < 3; j++) {
if (
board[0][j] !== 0 &&
board[0][j] === board[1][j] &&
board[0][j] === board[2][j]
) {
return board[0][j];
}
}

if (
board[0][0] !== 0 &&
board[0][0] === board[1][1] &&
board[0][0] === board[2][2]
) {
return board[0][0];
}
if (
board[0][2] !== 0 &&
board[0][2] === board[1][1] &&
board[0][2] === board[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;
}

0 comments on commit 860ddcd

Please sign in to comment.