Skip to content

codewars #77

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 1 commit into
base: Kuprijanov_Dmitrij_Vjacheslavovich
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
25 changes: 25 additions & 0 deletions codewars/Anagram difference/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function anagramDifference(w1, w2) {
const letterCounts1 = {};
const letterCounts2 = {};

for (const letter of w1) {
letterCounts1[letter] = (letterCounts1[letter] || 0) + 1;
}

for (const letter of w2) {
letterCounts2[letter] = (letterCounts2[letter] || 0) + 1;
}

let difference = 0;
for (const letter in letterCounts1) {
difference += Math.abs(letterCounts1[letter] - (letterCounts2[letter] || 0));
}

for (const letter in letterCounts2) {
if (!letterCounts1[letter]) {
difference += letterCounts2[letter];
}
}

return difference;
}
15 changes: 15 additions & 0 deletions codewars/Array Deep Count/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function deepCount(array) {
let count = 0;

function countElements(arr) {
count += arr.length;
for (const item of arr) {
if (Array.isArray(item)) {
countElements(item);
}
}
}

countElements(array);
return count;
}
9 changes: 9 additions & 0 deletions codewars/Build Tower/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function towerBuilder(nFloors) {
const tower = [];
for (let i = 1; i <= nFloors; i++) {
const spaces = " ".repeat(nFloors - i);
const stars = "*".repeat(i * 2 - 1);
tower.push(spaces + stars + spaces);
}
return tower;
}
11 changes: 11 additions & 0 deletions codewars/Convert string to camel case/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function toCamelCase(str) {
const parts = str.replace(/_/g, '-').split('-');
let result = [parts[0]];

for (let i = 1; i < parts.length; i++) {
result.push(parts[i][0].toUpperCase());
result.push(parts[i].slice(1));
}

return result.join('');
}
16 changes: 16 additions & 0 deletions codewars/Duplicate Encoder/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function duplicateEncode(word) {
const lowerWord = word.toLowerCase();
let result = '';

for (let i = 0; i < lowerWord.length; i++) {
const char = lowerWord[i];

if (lowerWord.split(char).length === 2) {
result += '(';
} else {
result += ')';
}
}

return result;
}
9 changes: 9 additions & 0 deletions codewars/Find the missing letter/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function findMissingLetter(array) {
const string = array.join('');

for (let i = 0; i < string.length; i++) {
if (string.charCodeAt(i + 1) - string.charCodeAt(i) !== 1) {
return String.fromCharCode(string.charCodeAt(i) + 1);
}
}
}
24 changes: 24 additions & 0 deletions codewars/Flatten a Nested Map/main.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;
}
14 changes: 14 additions & 0 deletions codewars/Fun with tree - max sum/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function maxSum(root) {
if (!root) return 0;

if (!root.left && !root.right) return root.value;

let leftMax = -Infinity;
let rightMax = -Infinity;

if (root.left) leftMax = maxSum(root.left);
if (root.right) rightMax = maxSum(root.right);

return root.value + Math.max(leftMax, rightMax);
}

25 changes: 25 additions & 0 deletions codewars/Linked Lists - Sorted Insert/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}

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

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

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

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

return head;
}
25 changes: 25 additions & 0 deletions codewars/Merge two arrays/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function mergeArrays(arr1, arr2) {
const result = [];
const maxLength = Math.max(arr1.length, arr2.length);


for (let i = 0; i < maxLength; i++) {

if (i < arr1.length) {
result.push(arr1[i]);
}


if (i < arr2.length) {
result.push(arr2[i]);
}
}

return result;
}


console.log(mergeArrays(['j', 'b', 'c', 'd', 'e'], [1, 2, 3, 4, 5]));


console.log(mergeArrays([1, 2, 3], ['a', 'b', 'c', 'd', 'e', 'f']));
13 changes: 13 additions & 0 deletions codewars/Moving Zeros To The End/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function moveZeros(arr) {
let nonZeroIndex = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] !== 0) {
arr[nonZeroIndex] = arr[i];
nonZeroIndex++;
}
}
for (let i = nonZeroIndex; i < arr.length; i++) {
arr[i] = 0;
}
return arr;
}
17 changes: 17 additions & 0 deletions codewars/Product of consecutive Fib numbers/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function productFib(prod) {
let f0 = 0;
let f1 = 1;
let found = false;

while (f0 * f1 <= prod) {
if (f0 * f1 === prod) {
found = true;
break;
}
let temp = f1;
f1 = f0 + f1;
f0 = temp;
}

return [f0, f1, found];
}
11 changes: 11 additions & 0 deletions codewars/Simple Pig Latin/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function pigIt(str){
text = str.split(" ");

for (var i = 0; i < text.length; i++) {
if (/[a-zA-Z]/.test(text[i])) {
text[i] = text[i].slice(1) + text[i][0] + 'ay';

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

let sum = 0;
for (let i = 0; i < str.length; i++) {
sum += parseInt(str[i], 10);
}

while (sum > 9) {
let temp = 0;
while (sum > 0) {
temp += sum % 10;
sum = Math.floor(sum / 10);
}
sum = temp;
}

return sum;
}
12 changes: 12 additions & 0 deletions codewars/Sum of pairs/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function sumPairs(ints, s) {
const seen = new Map();
for (let i = 0; i < ints.length; i++) {
const num = ints[i];
const complement = s - num;
if (seen.has(complement)) {
return [complement, num];
}
seen.set(num, i);
}
return undefined;
}
35 changes: 35 additions & 0 deletions codewars/Tic-Tac-Toe Checker/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
function isSolved(board) {
for (let i = 0; i < 3; i++) {
if (board[i][0] !== 0 && board[i][0] === board[i][1] && board[i][0] === board[i][2]) {
return board[i][0];
}
}

for (let i = 0; i < 3; i++) {
if (board[0][i] !== 0 && board[0][i] === board[1][i] && board[0][i] === board[2][i]) {
return board[0][i];
}
}

if (board[0][0] !== 0 && board[0][0] === board[1][1] && board[0][0] === board[2][2]) {
return board[0][0];
}
if (board[0][2] !== 0 && board[0][2] === board[1][1] && board[0][2] === board[2][0]) {
return board[0][2];
}

let draw = true;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (board[i][j] === 0) {
draw = false;
break;
}
}
}
if (draw) {
return 0;
}

return -1;
}
Loading