-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from pvzprrrr/Naumova_Alena_Alekseevna
codewars
- Loading branch information
Showing
21 changed files
with
381 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,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; | ||
} |
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,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; | ||
} |
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 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; | ||
} |
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) { | ||
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
11
codewars/Convert string to camel case/ConvertStringToCamel.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,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(""); | ||
} |
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 @@ | ||
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(""); | ||
} |
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 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; | ||
} |
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 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; | ||
} |
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,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); | ||
} |
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,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; | ||
} |
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 @@ | ||
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; | ||
} |
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 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; | ||
} |
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 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)]; | ||
} |
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,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]; | ||
} | ||
} |
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,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(" "); | ||
} |
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,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; | ||
} |
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 digitalRoot(n) { | ||
if (n < 10) { | ||
return n; | ||
} | ||
return digitalRoot( | ||
n.toString().split("").reduce((summ, digit) => summ + +digit, 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,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; | ||
} |
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 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; | ||
} |
Oops, something went wrong.