Skip to content

codewars, math function, class + tests #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: Golosova_Anastasija_Sergeevna
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions codewars/Adding Big Numbers/add.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;
}
30 changes: 30 additions & 0 deletions codewars/Anagram difference/anagram.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function anagramDifference(first, second) {
if (first === "" && second === "") {
return 0;
} else if (first === "" || second === "") {
if (first === "") {
return second.length;
} else {
return first.length;
}
}
let сount = {};
if (first !== "" && second !== "") {
for (let letter of first) {
сount[letter] = (сount[letter] ?? 0) + 1;
}

for (let letter of second) {
if (сount[letter]) {
сount[letter] -= 1;
} else {
сount[letter] = -1;
}
}
}
let remove = 0;
for (let count of Object.values(сount)) {
remove += Math.abs(count);
}
return remove;
}
11 changes: 11 additions & 0 deletions codewars/Array Deep Count/deepcount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function deepCount(array) {
let sum = 0;

for (let i of array) {
sum += 1;
if (Array.isArray(i)) {
sum += deepCount(i);
}
}
return sum;
}
12 changes: 12 additions & 0 deletions codewars/Build Tower/buildtower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function towerBuilder(numOfFloors) {
const tower = [];
for (let floor = 0; floor < numOfFloors; floor++) {
const numOfStars = 2 * floor + 1;
const numOfSpaces = numOfFloors - floor - 1;
const stars = '*'.repeat(numOfStars);
const spaces = ' '.repeat(numOfSpaces);
const floorString = spaces + stars + spaces;
tower.push(floorString);
}
return tower;
}
15 changes: 15 additions & 0 deletions codewars/Convert string to camel case/camelcase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function toCamelCase(str) {
if (str.length === 0) {
return str;
}
const arr = str.split(/[-_]/);

const newArr = arr.map((word, index) => {
if (index === 0) {
return word[0] + word.substr(1).toLowerCase();
} else {
return word.substr(0, 1).toUpperCase() + word.substr(1).toLowerCase();
}
});
return newArr.join("");
}
13 changes: 13 additions & 0 deletions codewars/Duplicate Encoder/dublicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function duplicateEncode(word) {
word = word.toLowerCase();
const count = {};
for (const char of word) {
count[char] = (count[char] ?? 0) + 1;
}

let brackets = "";
for (const char of word) {
brackets += count[char] === 1 ? "(" : ")";
}
return brackets;
}
9 changes: 9 additions & 0 deletions codewars/Find the missing letter/missingletter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function findMissingLetter(array) {
for (let i = 0; i < array.length - 1; i++) {
const icode = array[i].charCodeAt(0);
const inextcode = array[i + 1].charCodeAt(0);
if (inextcode !== icode + 1) {
return String.fromCharCode(icode + 1);
}
}
}
24 changes: 24 additions & 0 deletions codewars/Flatten a Nested Map/map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function flattenMap(input) {
const result = {};

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

if (
value !== null &&
typeof value === "object" &&
!Array.isArray(value) &&
typeof value !== "function"
) {
flatten(value, newKey);
} else {
result[newKey] = value;
}
}
}

flatten(input, "");

return result;
}
1 change: 0 additions & 1 deletion codewars/Fun with tree - max sum/.gitkeep
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

18 changes: 18 additions & 0 deletions codewars/Fun with tree - max sum/tree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function maxSum(root) {
if (root === null) {
return 0;
}
function dfs(node) {
if (node === null) {
return Number.NEGATIVE_INFINITY;
}
if (node.left === null && node.right === null) {
return node.value;
}
const leftSum = dfs(node.left);
const rightSum = dfs(node.right);
return node.value + Math.max(leftSum, rightSum);
}

return dfs(root);
}
19 changes: 19 additions & 0 deletions codewars/Linked Lists - Sorted Insert/linkedlist.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/merge2arrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function mergeArrays(a, b) {
let result = [];
let i = 0;
let j = 0;

while (i < a.length || j < b.length) {
if (i < a.length) result.push(a[i++]);
if (j < b.length) result.push(b[j++]);
}

return result;
}
14 changes: 14 additions & 0 deletions codewars/Moving Zeros To The End/movingzeros.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function moveZeros(arr) {
const zeros = [];
const noZeros = [];

for (let i = 0; i < arr.length; i++) {
if (arr[i] === 0) {
zeros.push(arr[i]);
} else {
noZeros.push(arr[i]);
}
}
zeros.forEach(zero => noZeros.push(zero));
return noZeros;
}
18 changes: 18 additions & 0 deletions codewars/Permutations/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 shufflings = [];
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)) {
shufflings.push(char + permutation);
}
}

return [...new Set(shufflings)];
}
10 changes: 10 additions & 0 deletions codewars/Product of consecutive Fib numbers/productfib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function productFib(prod) {
let first = 1;
let second = 1;
while (first * second < prod) {
let x = second;
second += first;
first = x;
}
return [first, second, first * second === prod];
}
22 changes: 22 additions & 0 deletions codewars/Simple Pig Latin/piglatin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function pigIt(str) {
str = str.split(" ");
let ayLetters = "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, ayLetters);
let pigIt = "";
for (let char of word) {
pigIt += char;
}
result.push(pigIt);
}
let newStr = result.join(" ");
return newStr.trimEnd();
}
36 changes: 36 additions & 0 deletions codewars/Snail/snail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
snail = function (array) {
if (array.length === 0) {
return [];
}
let bottom = 0;
let top = array.length - 1;
let left = 0;
let right = array[0].length - 1;
let arranged = [];

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

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

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

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

return arranged;
};
14 changes: 14 additions & 0 deletions codewars/Sum of Digits - Digital Root/sumofdigits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function digitalRoot(n) {
let str = n.toString();
let sum = 0;

for (const char of str) {
let num = parseInt(char);
sum += num;
}
str = sum.toString();
if (str.length > 1) {
return digitalRoot(str);
}
return sum;
}
22 changes: 22 additions & 0 deletions codewars/Sum of Intervals/sumofintervals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function sumIntervals(intervals) {
intervals.sort((a, b) => a[0] - b[0]);

let sum = 0;
let End = Number;

for (const [start, finish] of intervals) {
if (start <= End && finish >= End) {
sum += finish - End;
if (End < finish) {
End = finish;
}
} else if (End > finish) {
continue;
} else {
sum += finish - start;
End = finish;
}
}

return sum;
}
11 changes: 11 additions & 0 deletions codewars/Sum of pairs/sumofpairs,js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function sumPairs(ints, s) {
let noticed = {};
for (let i = 0; i < ints.length; i++) {
let x = s - ints[i];
if (x in noticed) {
return [x, ints[i]];
}
noticed[ints[i]] = true;
}
return undefined;
}
Loading
Loading