Skip to content
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

codewars #70

Open
wants to merge 1 commit into
base: Orlova_Ekaterina_Evgenevna
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
18 changes: 18 additions & 0 deletions codewars/Adding Big Numbers/addingBigNumbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function add(a, b) {
let result = '';
let carry = 0;
let i = a.length - 1;
let j = b.length - 1;

while (i >= 0 || j >= 0 || carry) {
let sum = carry;
if (i >= 0) sum += +a[i--];
if (j >= 0) sum += +b[j--];
result = (sum % 10) + result;
carry = Math.floor(sum / 10);
}

return result;
}

console.log(add("123","321"));
11 changes: 11 additions & 0 deletions codewars/Anagram difference/anagramDifference.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function anagramDifference(w1,w2){
let w3 = 0;
for(let i = 0; i < w1.length; i++) {
if(w2.includes(w1[i])){
w2 = w2.replace(w1[i], 1);
w3++;
}
}
return w1.length + w2.length - 2 * w3;
}
console.log(anagramDifference("codewars", "hackerrank"));
8 changes: 8 additions & 0 deletions codewars/Array Deep Count/arrayDeepCount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function deepCount(a){
let count = a.length;
for (let i = 0; i < a.length; i++) {
if (Array.isArray(a[i])) count += deepCount(a[i]);
}
return count
}
console.log(deepCount([1,2,3,4,[5]]));
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(nFloors) {
const pyramid = [];
for (let i = 1; i <= nFloors; i++) {
const spaces = ' '.repeat(nFloors - i);
const stars = '*'.repeat(2 * i - 1);
pyramid.push(spaces + stars + spaces);
}
return pyramid;
}
const numberOfFloors = 5;
const pyramidArray = towerBuilder(numberOfFloors);
console.log(pyramidArray.join('\n'));
13 changes: 13 additions & 0 deletions codewars/Convert string to camel case/camelCase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function toCamelCase1(str){
let result = "";
for (let i = 0; i < str.length; i++) {
if (str[i] === '_' || str[i] === '-') {
i++;
result += str[i].toUpperCase();
} else {
result += str[i];
}
}
return result;
}
console.log(toCamelCase1("the-stealth-warrior"));
23 changes: 23 additions & 0 deletions codewars/Duplicate Encoder/duplicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function duplicateEncode(word) {
const counting = {};
let stroke = "";

for (let i = 0; i < word.length; i++) {
const element = word[i].toLowerCase();
counting[element] = (counting[element] || 0) + 1;
}

for (let i = 0; i < word.length; i++) {
const char = word[i].toLowerCase();
if (counting[char] > 1) {
stroke += ")";
} else {
stroke += "(";
}
}
return stroke;
}
console.log(duplicateEncode("din"));
console.log(duplicateEncode("recede"));
console.log(duplicateEncode("Success"));
console.log(duplicateEncode("(( @" ));
12 changes: 12 additions & 0 deletions codewars/Find the missing letter/find_nissing_letter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function findMissingLetter(array)
{
let first = array[0].charCodeAt(0);
for (let i = 0; i < array.length; i++) {
if (array[i].charCodeAt(0) !== first + i) {
return String.fromCharCode(first + i);
}
}
return null
}
console.log(findMissingLetter(['a','b','c','d','f']));
console.log(findMissingLetter(['O','Q','R','S']));
33 changes: 33 additions & 0 deletions codewars/Flatten a Nested Map/flatten_nested_map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function flattenMap(map) {
const result = {};

function flatten(obj, parentKey = '') {
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
const newKey = parentKey ? `${parentKey}/${key}` : key;

if (typeof obj[key] === 'object' && obj[key] !== null && !Array.isArray(obj[key])) {
flatten(obj[key], newKey);
} else {
result[newKey] = obj[key];
}
}
}
}

flatten(map);
return result;
}

const input = {
'a': {
'b': {
'c': 12,
'd': 'Hello World'
},
'e': [1, 2, 3]
}
};

const output = flattenMap(input);
console.log(output);
35 changes: 35 additions & 0 deletions codewars/Fun with tree - max sum/tree_maxSum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class TreeNode {
constructor(value, left = null, right = null) {
this.value = value;
this.left = left;
this.right = right;
}
}

function maxSum(root) {
if (root === null) {
return 0;
}
if (root.left === null && root.right === null) {
return root.value;
}
let leftSum = maxSum(root.left);
let rightSum = maxSum(root.right);
let maxChildSum = Math.max(leftSum, rightSum);

return root.value + maxChildSum;

}

let root = new TreeNode(17,
new TreeNode(3,
new TreeNode(2)
),
new TreeNode(-10,
new TreeNode(16),
new TreeNode(1,
new TreeNode(13)
)
)
);
console.log(maxSum(root));
49 changes: 49 additions & 0 deletions codewars/Linked Lists - Sorted Insert/linked_list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}

function push(head, data) {
let newNode = new Node(data);
newNode.next = head;
return newNode;
}

function buildOneTwoThree() {
let head = null;
head = push(head, 3);
head = push(head, 2);
head = push(head, 1);
return head;
}

function sortedInsert(head, data) {
let newNode = new Node(data);

if (head === null || data < head.data) {
newNode.next = head;
return newNode;
}

let current = head;
while (current.next !== null && current.next.data < data) {
current = current.next;
}

newNode.next = current.next;
current.next = newNode;

return head;
}

let head = buildOneTwoThree();
head = sortedInsert(head, 4);

let current = head;
while (current !== null) {
process.stdout.write(current.data + " -> ");
current = current.next;
}
console.log("null");
17 changes: 17 additions & 0 deletions codewars/Merge two arrays/merge2arrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function mergeArrays(a, b) {
const result = [];
const maxLength = Math.max(a.length, b.length);
for (let i = 0; i < maxLength; i++) {
if (i < a.length) {
result.push(a[i]);
}
if (i < b.length) {
result.push(b[i]);
}
}
return result;
}
console.log(mergeArrays([1, 2, 3, 4, 5, 6, 7, 8], ['a', 'b', 'c', 'd', 'e']));
console.log(mergeArrays(['a', 'b', 'c', 'd', 'e'], [1, 2, 3, 4, 5]));
console.log(mergeArrays([2, 5, 8, 23, 67, 6], ['b', 'r', 'a', 'u', 'r', 's']));
console.log(mergeArrays(['b', 'r', 'a', 'u', 'r', 's', 'e', 'q', 'z'], [2, 5, 8, 23, 67, 6]));
14 changes: 14 additions & 0 deletions codewars/Moving Zeros To The End/movingZerosToTheEnd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function moveZerosToEnd(array) {
let nonZeroCount = 0;
for (let i = 0; i < array.length; i++) {
if (array[i] !== 0) {
array[nonZeroCount] = array[i];
nonZeroCount++;
}
}
for (let i = nonZeroCount; i < array.length; i++) {
array[i] = 0;
}
return array;
}
console.log(moveZerosToEnd([0,1,2,0,3,0,4]));
15 changes: 15 additions & 0 deletions codewars/Permutations/permutations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function permutations(string) {
if (string.length === 1) {
return [string];
}
let result = new Set();
for (let i = 0; i < string.length; i++) {
let remainingChars = string.slice(0, i) + string.slice(i + 1);
let permOfRemainingChars = permutations(remainingChars);
for (let permutation of permOfRemainingChars) {
result.add(string[i] + permutation);
}
}
return Array.from(result);
}
console.log(permutations('abc'));
15 changes: 15 additions & 0 deletions codewars/Product of consecutive Fib numbers/fibonacci.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function productFib(prod) {
let a = 0;
let b = 1;

while (a * b < prod) {
const next = a + b;
a = b;
b = next;
}

return [a, b, a * b === prod];
}

console.log(productFib(714));
console.log(productFib(800));
9 changes: 9 additions & 0 deletions codewars/Simple Pig Latin/simplePig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function pigIt(str) {
return str.split(' ').map(word => {
if (/^[a-zA-Z]+$/.test(word)) {
return word.slice(1) + word[0] + 'ay';
}
return word;
}).join(' ');
}
console.log(pigIt('Hello world !'));
29 changes: 29 additions & 0 deletions codewars/Snail/snail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
snail = function(array) {
let result = [];
while (array.length) {
result = result.concat(array.shift());

for (let i = 0; i < array.length; i++) {
if (array[i].length) {
result.push(array[i].pop());
}
}
if (array.length) {
result = result.concat(array.pop().reverse());
}

for (let i = array.length - 1; i >= 0; i--) {
if (array[i].length) {
result.push(array[i].shift());
}
}
}
return result;
}
let array = [
[1,2,3],
[8,9,4],
[7,6,5]
];

console.log(snail(array));
4 changes: 4 additions & 0 deletions codewars/Sum of Digits - Digital Root/sumOfDigits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function digitalRoot(n) {
return (n - 1) % 9 + 1;
}
console.log(digitalRoot(125))
23 changes: 23 additions & 0 deletions codewars/Sum of Intervals/sumOfIntervals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function sumIntervals(intervals) {
intervals.sort((a, b) => a[0] - b[0]);
let mergedIntervals = [];
let currentInterval = intervals[0];

for (let i = 1; i < intervals.length; i++) {
let nextInterval = intervals[i];
if (currentInterval[1] >= nextInterval[0]) {
currentInterval[1] = Math.max(currentInterval[1], nextInterval[1]);
} else {
mergedIntervals.push(currentInterval);
currentInterval = nextInterval;
}
}

mergedIntervals.push(currentInterval);
let totalLength = mergedIntervals.reduce((sum, interval) => sum + (interval[1] - interval[0]), 0);

return totalLength;
}

console.log(sumIntervals([[1, 4], [7, 10], [3, 5]]));
console.log(sumIntervals([[1, 5], [10, 20], [1, 6], [16, 19], [5, 11]]));
20 changes: 20 additions & 0 deletions codewars/Sum of pairs/sumOfPairs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function sumPairs(ints, s) {
const seen = new Set();

for (let i = 0; i < ints.length; i++) {
const current = ints[i];
const complement = s - current;

if (seen.has(complement)) {
return [complement, current];
}

seen.add(current);
}

return null;
}

console.log(sumPairs([4, 3, 2, 3, 4], 6));
console.log(sumPairs([0, 0, -2, 3], 2));
console.log(sumPairs([10, 5, 2, 3, 7, 5], 10));
Loading
Loading