Skip to content

Commit

Permalink
Merge pull request #29 from pvzprrrr/Naumova_Alena_Alekseevna
Browse files Browse the repository at this point in the history
codewars
  • Loading branch information
FeLL1kS authored Oct 10, 2024
2 parents 58b5956 + 0ad9115 commit fa600c6
Show file tree
Hide file tree
Showing 21 changed files with 381 additions and 0 deletions.
34 changes: 34 additions & 0 deletions codewars/Adding Big Numbers/BigNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function addBigNumber(a, b) {
const numLen = Math.max(a.length, b.length);
a = a.padStart(numLen, "0");
b = b.padStart(numLen, "0");

let listy = [];

let tailing = false;
for (let i = a.length - 1; i >= 0; i--) {
let sum = Number(a[i]) + Number(b[i]);

if (tailing) {
sum++;
tailing = false;
}

sum = sum.toString();

if (sum.length > 1) {
tailing = true;
sum = sum.slice(1);
}

listy.push(sum);
}

if (tailing) {
listy.push("1");
}

listy.reverse();
listy = listy.join("");
return listy;
}
24 changes: 24 additions & 0 deletions codewars/Anagram difference/AnagramDiff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function anagramDifference(w1, w2) {
let rate1 = {};
let rate2 = {};
let listy = 0;
for (let sign of w1) {
rate1[sign] = (rate1[sign] || 0) + 1;
}
for (let sign of w2) {
rate2[sign] = (rate2[sign] || 0) + 1;
}
for (let sign in rate1) {
if (rate2[sign]) {
listy += Math.abs(rate1[sign] - rate2[sign]);
} else {
listy += rate1[sign];
}
}
for (let sign in rate2) {
if (!rate1[sign]) {
listy += rate2[sign];
}
}
return listy;
}
13 changes: 13 additions & 0 deletions codewars/Array Deep Count/ArrayDeepCount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function deepCount(a) {
if (a.length === 0) {
return 0;
}
let mxLen = 0;
for (let i = 0; i < a.length; i++) {
if (Array.isArray(a[i])) {
mxLen += deepCount(a[i]);
}
mxLen++;
}
return mxLen;
}
11 changes: 11 additions & 0 deletions codewars/Build Tower/BuildTower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function towerBuilder(nFloors) {
let listy = [];
let mxWid = nFloors * 2 - 1;
for (let i = 0; i < nFloors; i++) {
let stars = i * 2 + 1;
let spaces = (mxWid - stars) / 2;
let level = " ".repeat(spaces) + "*".repeat(stars) + " ".repeat(spaces);
listy.push(level);
}
return listy;
}
11 changes: 11 additions & 0 deletions codewars/Convert string to camel case/ConvertStringToCamel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function toCamelCase(str) {
str = str.split("");
for (let i = 0; i < str.length; i++) {
if (str[i] === "-" || str[i] === "_") {
str.splice(i, 2, str[i + 1].toUpperCase());
} else {
continue;
}
}
return str.join("");
}
20 changes: 20 additions & 0 deletions codewars/Duplicate Encoder/Duplicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function duplicateEncode(word) {
let scheme = new Map();
let arr = word.toLowerCase().split("");
for (let item of arr) {
if (scheme.has(item)) {
scheme.set(item, scheme.get(item) + 1);
} else {
scheme.set(item, 1);
}
}
let listy = [];
for (let i = 0; i < arr.length; i++) {
if (scheme.get(arr[i]) > 1) {
listy.push(")");
} else {
listy.push("(");
}
}
return listy.join("");
}
12 changes: 12 additions & 0 deletions codewars/Find the missing letter/FindMissingLetter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function findMissingLetter(array) {
let vocabulary = array[0] === array[0].toLowerCase();
array = array.join('').toLowerCase().split('');
let alph = 'abcdefghijklmnopqrstuvwxyz'.split('');
let firsrIndex = alph.indexOf(array[0]);
for (let i = 0; i < array.length; i++) {
if (array[i] !== alph[firsrIndex + i]) {
return vocabulary ? alph[firsrIndex + i] : alph[firsrIndex + i].toUpperCase();
}
}
return undefined;
}
18 changes: 18 additions & 0 deletions codewars/Flatten a Nested Map/NestedMap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function flattenMap(map, parentKey = "") {
let MapFlat = {};
for (let key in map) {
if (map.hasOwnProperty(key)) {
let newKey = parentKey ? `${parentKey}/${key}` : key;
if (
typeof map[key] === "object" &&
map[key] !== null &&
!Array.isArray(map[key])
) {
Object.assign(MapFlat, flattenMap(map[key], newKey));
} else {
MapFlat[newKey] = map[key];
}
}
}
return MapFlat;
}
16 changes: 16 additions & 0 deletions codewars/Fun with tree - max sum/TreeMaxSum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function maxSum(root) {
const FromBeginningToEnd = (node) => {
if (node === null) {
return 0;
}
if (node.left === null && node.right === null) {
return node.value;
}
const leftSum =
node.left !== null ? FromBeginningToEnd(node.left) : -Infinity;
const rightSum =
node.right !== null ? FromBeginningToEnd(node.right) : -Infinity;
return node.value + Math.max(leftSum, rightSum);
};
return FromBeginningToEnd(root);
}
22 changes: 22 additions & 0 deletions codewars/Linked Lists - Sorted Insert/LinkedLists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function Node(data) {
this.data = data;
this.next = null;
}

function sortedInsert(head, data) {
const newNode = new Node(data);
if (!head) {
return newNode;
}
if (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;
}
20 changes: 20 additions & 0 deletions codewars/Merge two arrays/MergeTwoArrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function mergeArrays(a, b) {
let left = 0;
let right = 0;
let listy = [];
while (left < a.length && right < b.length) {
listy.push(a[left]);
left++;
listy.push(b[right]);
right++;
}
while (left < a.length) {
listy.push(a[left]);
left++;
}
while (right < b.length) {
listy.push(b[right]);
right++;
}
return listy;
}
15 changes: 15 additions & 0 deletions codewars/Moving Zeros To The End/ZeroToEnd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function moveZeros(arr) {
let count = 0;
let listy = [];
for (let item of arr) {
if (item === 0) {
count += 1;
} else {
listy.push(item);
}
}
for (let i = 0; i < count; i++) {
listy.push(0);
}
return listy;
}
17 changes: 17 additions & 0 deletions codewars/Permutations/Permutations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function permutations(string) {
let listy = [];
let perm = (arr, currentArr) => {
if (arr.length === 0) {
listy.push(currentArr.join(""));
return;
}

for (let i = 0; i < arr.length; i++) {
let nextArr = arr.slice(0, i).concat(arr.slice(i + 1));
perm(nextArr, currentArr.concat(arr[i]));
}
};

perm(string.split(""), []);
return [...new Set(listy)];
}
14 changes: 14 additions & 0 deletions codewars/Product of consecutive Fib numbers/FibNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function productFib(prod) {
let a = 0;
let b = 1;
while (a * b < prod) {
let next = a + b;
a = b;
b = next;
}
if (a * b === prod) {
return [a, b, true];
} else {
return [a, b, false];
}
}
14 changes: 14 additions & 0 deletions codewars/Simple Pig Latin/PigLatin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function pigIt(str) {
str = str.split(" ");
let arr = [];
for (let i = 0; i < str.length; i++) {
let str1 = "";
if (str[i] === "!" || str[i] === "?") {
str1 = str[i].split("").slice(1).concat(str[i][0]);
} else {
str1 = str[i].split("").slice(1).concat(str[i][0], "ay");
}
arr.push(str1.join(""));
}
return arr.join(" ");
}
31 changes: 31 additions & 0 deletions codewars/Snail/Snail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function snail(array) {
let listy = [];
let left = 0;
let right = array[0].length - 1;
let upper = 0;
let lower = array.length - 1;
while (left <= right && upper <= lower) {
for (let i = left; i <= right; i++) {
listy.push(array[upper][i]);
}
upper += 1;
for (let i = upper; i <= lower; i++) {
listy.push(array[i][right]);
}
right -= 1;
if (upper <= lower) {
for (let i = right; i >= left; i--) {
listy.push(array[lower][i]);
}
lower -= 1;
}
if (left <= right) {
for (let i = lower; i >= upper; i--) {
listy.push(array[i][left]);
}
left += 1;
}
}

return listy;
}
8 changes: 8 additions & 0 deletions codewars/Sum of Digits - Digital Root/SumOfDigits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function digitalRoot(n) {
if (n < 10) {
return n;
}
return digitalRoot(
n.toString().split("").reduce((summ, digit) => summ + +digit, 0)
);
}
18 changes: 18 additions & 0 deletions codewars/Sum of Intervals/SumOfIntervals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function sumIntervals(intervals) {
intervals.sort((a, b) => a[0] - b[0]);
let listy = [];
let actualInt = intervals[0];
for (let i = 1; i < intervals.length; i++) {
if (intervals[i][0] <= actualInt[1]) {
actualInt[1] = Math.max(actualInt[1], intervals[i][1]);
} else {
listy.push(actualInt);
actualInt = intervals[i];
}
}
listy.push(actualInt);
let lengthOfIntervals = listy
.map((item) => item[1] - item[0])
.reduce((summ, digits) => summ + digits, 0);
return lengthOfIntervals;
}
13 changes: 13 additions & 0 deletions codewars/Sum of pairs/SumOfPairs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function sumPairs(ints, s) {
let seen = new Set();

for (let i = 0; i < ints.length; i++) {
let current = ints[i];
let needed = s - current;
if (seen.has(needed)) {
return [ints[ints.indexOf(needed)], ints[i]];
}
seen.add(current);
}
return undefined;
}
Loading

0 comments on commit fa600c6

Please sign in to comment.