Skip to content

Commit

Permalink
codewars
Browse files Browse the repository at this point in the history
  • Loading branch information
pvzprrrr committed Oct 1, 2024
1 parent e884b5a commit 0ad9115
Show file tree
Hide file tree
Showing 15 changed files with 137 additions and 140 deletions.
54 changes: 27 additions & 27 deletions codewars/Adding Big Numbers/BigNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@ function addBigNumber(a, b) {
const numLen = Math.max(a.length, b.length);
a = a.padStart(numLen, "0");
b = b.padStart(numLen, "0");

let res = [];

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

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

sum = sum.toString();

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

res.push(sum);
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 (remainder) {
res.push("1");
if (tailing) {
listy.push("1");
}

res.reverse();
res = res.join("");
return res;
}
listy.reverse();
listy = listy.join("");
return listy;
}
38 changes: 19 additions & 19 deletions codewars/Anagram difference/AnagramDiff.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
function anagramDifference(w1, w2) {
let freq1 = {};
let freq2 = {};
let res = 0;
for (let char of w1) {
freq1[char] = (freq1[char] || 0) + 1;
let rate1 = {};
let rate2 = {};
let listy = 0;
for (let sign of w1) {
rate1[sign] = (rate1[sign] || 0) + 1;
}
for (let char of w2) {
freq2[char] = (freq2[char] || 0) + 1;
for (let sign of w2) {
rate2[sign] = (rate2[sign] || 0) + 1;
}
for (let char in freq1) {
if (freq2[char]) {
res += Math.abs(freq1[char] - freq2[char]);
} else {
res += freq1[char];
}
for (let sign in rate1) {
if (rate2[sign]) {
listy += Math.abs(rate1[sign] - rate2[sign]);
} else {
listy += rate1[sign];
}
}
for (let char in freq2) {
if (!freq1[char]) {
res += freq2[char];
}
for (let sign in rate2) {
if (!rate1[sign]) {
listy += rate2[sign];
}
}
return res;
}
return listy;
}
9 changes: 4 additions & 5 deletions codewars/Array Deep Count/ArrayDeepCount.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
function deepCount(a) {
//...
if (a.length === 0) {
return 0;
}
let maxLength = 0;
let mxLen = 0;
for (let i = 0; i < a.length; i++) {
if (Array.isArray(a[i])) {
maxLength += deepCount(a[i]);
mxLen += deepCount(a[i]);
}
maxLength++;
mxLen++;
}
return maxLength;
return mxLen;
}
12 changes: 6 additions & 6 deletions codewars/Build Tower/BuildTower.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
function towerBuilder(nFloors) {
let stack = [];
let maxWidth = nFloors * 2 - 1;
let listy = [];
let mxWid = nFloors * 2 - 1;
for (let i = 0; i < nFloors; i++) {
let stars = i * 2 + 1;
let spaces = (maxWidth - stars) / 2;
let floor = " ".repeat(spaces) + "*".repeat(stars) + " ".repeat(spaces);
stack.push(floor);
let spaces = (mxWid - stars) / 2;
let level = " ".repeat(spaces) + "*".repeat(stars) + " ".repeat(spaces);
listy.push(level);
}
return stack;
return listy;
}
18 changes: 9 additions & 9 deletions codewars/Convert string to camel case/ConvertStringToCamel.js
Original file line number Diff line number Diff line change
@@ -1,11 +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;
}
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("");
}
}
return str.join("");
}
18 changes: 9 additions & 9 deletions codewars/Duplicate Encoder/Duplicate.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
function duplicateEncode(word) {
let map = new Map();
let scheme = new Map();
let arr = word.toLowerCase().split("");
for (let item of arr) {
if (map.has(item)) {
map.set(item, map.get(item) + 1);
if (scheme.has(item)) {
scheme.set(item, scheme.get(item) + 1);
} else {
map.set(item, 1);
scheme.set(item, 1);
}
}
let res = [];
let listy = [];
for (let i = 0; i < arr.length; i++) {
if (map.get(arr[i]) > 1) {
res.push(")");
if (scheme.get(arr[i]) > 1) {
listy.push(")");
} else {
res.push("(");
listy.push("(");
}
}
return res.join("");
return listy.join("");
}
10 changes: 5 additions & 5 deletions codewars/Find the missing letter/FindMissingLetter.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
function findMissingLetter(array) {
let caseWord = array[0] === array[0].toLowerCase();
let vocabulary = array[0] === array[0].toLowerCase();
array = array.join('').toLowerCase().split('');
let alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');
let startIndex = alphabet.indexOf(array[0]);
let alph = 'abcdefghijklmnopqrstuvwxyz'.split('');
let firsrIndex = alph.indexOf(array[0]);
for (let i = 0; i < array.length; i++) {
if (array[i] !== alphabet[startIndex + i]) {
return caseWord ? alphabet[startIndex + i] : alphabet[startIndex + i].toUpperCase();
if (array[i] !== alph[firsrIndex + i]) {
return vocabulary ? alph[firsrIndex + i] : alph[firsrIndex + i].toUpperCase();
}
}
return undefined;
Expand Down
8 changes: 4 additions & 4 deletions codewars/Flatten a Nested Map/NestedMap.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function flattenMap(map, parentKey = "") {
let flatMap = {};
let MapFlat = {};
for (let key in map) {
if (map.hasOwnProperty(key)) {
let newKey = parentKey ? `${parentKey}/${key}` : key;
Expand All @@ -8,11 +8,11 @@ function flattenMap(map, parentKey = "") {
map[key] !== null &&
!Array.isArray(map[key])
) {
Object.assign(flatMap, flattenMap(map[key], newKey));
Object.assign(MapFlat, flattenMap(map[key], newKey));
} else {
flatMap[newKey] = map[key];
MapFlat[newKey] = map[key];
}
}
}
return flatMap;
return MapFlat;
}
8 changes: 4 additions & 4 deletions codewars/Fun with tree - max sum/TreeMaxSum.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
function maxSum(root) {
const maxSumRootToLeaf = (node) => {
const FromBeginningToEnd = (node) => {
if (node === null) {
return 0;
}
if (node.left === null && node.right === null) {
return node.value;
}
const leftSum =
node.left !== null ? maxSumRootToLeaf(node.left) : -Infinity;
node.left !== null ? FromBeginningToEnd(node.left) : -Infinity;
const rightSum =
node.right !== null ? maxSumRootToLeaf(node.right) : -Infinity;
node.right !== null ? FromBeginningToEnd(node.right) : -Infinity;
return node.value + Math.max(leftSum, rightSum);
};
return maxSumRootToLeaf(root);
return FromBeginningToEnd(root);
}
13 changes: 6 additions & 7 deletions codewars/Merge two arrays/MergeTwoArrays.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
function mergeArrays(a, b) {
// your code here
let left = 0;
let right = 0;
let res = [];
let listy = [];
while (left < a.length && right < b.length) {
res.push(a[left]);
listy.push(a[left]);
left++;
res.push(b[right]);
listy.push(b[right]);
right++;
}
while (left < a.length) {
res.push(a[left]);
listy.push(a[left]);
left++;
}
while (right < b.length) {
res.push(b[right]);
listy.push(b[right]);
right++;
}
return res;
return listy;
}
14 changes: 7 additions & 7 deletions codewars/Moving Zeros To The End/ZeroToEnd.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
function moveZeros(arr) {
let counter = 0;
let newArr = [];
let count = 0;
let listy = [];
for (let item of arr) {
if (item === 0) {
counter += 1;
count += 1;
} else {
newArr.push(item);
listy.push(item);
}
}
for (let i = 0; i < counter; i++) {
newArr.push(0);
for (let i = 0; i < count; i++) {
listy.push(0);
}
return newArr;
return listy;
}
6 changes: 3 additions & 3 deletions codewars/Permutations/Permutations.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
function permutations(string) {
let res = [];
let listy = [];
let perm = (arr, currentArr) => {
if (arr.length === 0) {
res.push(currentArr.join(""));
listy.push(currentArr.join(""));
return;
}

Expand All @@ -13,5 +13,5 @@ function permutations(string) {
};

perm(string.split(""), []);
return [...new Set(res)];
return [...new Set(listy)];
}
9 changes: 4 additions & 5 deletions codewars/Simple Pig Latin/PigLatin.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
function pigIt(str) {
//Code here
str = str.split(" ");
let arr = [];
for (let i = 0; i < str.length; i++) {
let newStr = "";
let str1 = "";
if (str[i] === "!" || str[i] === "?") {
newStr = str[i].split("").slice(1).concat(str[i][0]);
str1 = str[i].split("").slice(1).concat(str[i][0]);
} else {
newStr = str[i].split("").slice(1).concat(str[i][0], "ay");
str1 = str[i].split("").slice(1).concat(str[i][0], "ay");
}
arr.push(newStr.join(""));
arr.push(str1.join(""));
}
return arr.join(" ");
}
Loading

0 comments on commit 0ad9115

Please sign in to comment.