Skip to content

Commit

Permalink
codewars2
Browse files Browse the repository at this point in the history
  • Loading branch information
AlyonaUi committed Oct 3, 2023
1 parent 01f54f6 commit b819e7a
Show file tree
Hide file tree
Showing 17 changed files with 229 additions and 0 deletions.
17 changes: 17 additions & 0 deletions codewars/Anagram difference/anagram_difference.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function anagramDifference(w1,w2){
let ar1 = w1.split('');
let ar2 = w2.split('');
let res = [];
if (ar1.length === 0) {
return ar2.length;
} else if (ar2.length === 0) {
return ar1.length;
}
for (let i = 0; i < ar1.length; i++) {
if (ar2.indexOf(ar1[i]) !== -1) {
res.push(ar2[ar2.indexOf(ar1[i])]);
ar2.splice(ar2.indexOf(ar1[i]), 1);
}
}
return (w1.length + w2.length) - res.length * 2;
}
13 changes: 13 additions & 0 deletions codewars/Array Deep Count/array_deep_count.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function deepCount(a){
let elem = a.length;
if (a.length !== 0) {
for (let i = 0; i < a.length; i++){
if (Array.isArray(a[i])) {
elem = elem + deepCount(a[i]);
}
}
} else {
return a.length;
}
return elem;
}
11 changes: 11 additions & 0 deletions codewars/Build Tower/build_tower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function towerBuilder(nFloors) {
let floors = [];
let result = [];

for (let i = nFloors; i > 0; i--) {
let spaces = i - 1;
let stars = 1 + 2 * (nFloors - i);
floors.push(' '.repeat(spaces) + '*'.repeat(stars) + ' '.repeat(spaces));
}
return floors;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function toCamelCase(str){
let res = Array.from(str);
for (let i = 0; i < res.length; i++) {
if (res[i] === '-' || res[i] === '_') {
res.splice(i, 2, res[i+1].toUpperCase());
}
}
let str1 = res.join('');
return str1;
}
12 changes: 12 additions & 0 deletions codewars/Duplicate Encoder/duplicate_encoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function duplicateEncode(word){
let res = '';
let str = word.toLowerCase();
for (let i = 0; i < str.length; i++) {
if (str.indexOf(str[i]) == str.lastIndexOf(str[i])) {
res += '(';
} else {
res += ')';
}
}
return res;
}
15 changes: 15 additions & 0 deletions codewars/Find the missing letter/find_the_missing_letter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function findMissingLetter(array)
{
let res = '';
let str = array.join('');
let lastWord = str.charCodeAt(0);
for (let i = 1; i < str.length; i++) {
if (str.charCodeAt(i) - lastWord === 1) {
lastWord = str.charCodeAt(i);
} else {
let num = lastWord + 1;
res = String.fromCharCode(num);
}
}
return res;
}
12 changes: 12 additions & 0 deletions codewars/Fun with tree - max sum/fun_with_trees.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function maxSum(root) {
if (root != null) {
var a = maxSum(root.left) + root.value;
var b = maxSum(root.right) + root.value;
if (b > a) {
return b;
} else {
return a;
}
}
return 0;
}
11 changes: 11 additions & 0 deletions codewars/Merge two arrays/merge_two_arrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function mergeArrays(a, b) {
const maxLength = Math.max(a.length, b.length);
let res = [];

for (let i = 0; i < maxLength; i++) {
res.push(a[i]);
res.push(b[i]);
}

return res.filter((value) => value !== undefined);
}
14 changes: 14 additions & 0 deletions codewars/Moving Zeros To The End/moving_zeros_to_the_end.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function moveZeros(arr) {
let arr0 = [];
let arr1 = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] === 0) {
arr0.push(0);
} else {
arr1.push(arr[i]);
}
}
let res = [...arr1, ...arr0];

return res ;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function productFib(prod){
let first = 0;
let last = 1;
while (first * last < prod) {
const rem = last;
last = first + last;
first = rem;
}
return [first, last, first * last === prod];
}
15 changes: 15 additions & 0 deletions codewars/Simple Pig Latin/simple_pig_latin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function pigIt(str){
let res = [];
let arr = str.split(' ');
for (let i = 0; i < arr.length; i++) {
if (arr[i] === "!" || arr[i] === "?") {
res.push(arr[i]);
} else {
let subStr1 = arr[i].slice(0, 1);
let subStr2 = arr[i].slice(1);
let vr = subStr2 + subStr1 + "ay";
res.push(vr);
}
}
return res.join(' ');
}
14 changes: 14 additions & 0 deletions codewars/Snail/snail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
snail = function(array) {
var result;
while (array.length) {
result = (result ? result.concat(array.shift()) : array.shift());
for (var i = 0; i < array.length; i++) {
result.push(array[i].pop());
}
result = result.concat((array.pop() || []).reverse());
for (var i = array.length - 1; i >= 0; i--) {
result.push(array[i].shift());
}
}
return result;
}
13 changes: 13 additions & 0 deletions codewars/Sum of Digits - Digital Root/sum_of_digits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function digitalRoot(n) {
let sum = 0;
let num = [];
num = Array.from(String(n), Number);
for (let i = 0; i < num.length; i++) {
sum += num[i];
}
if (String(sum).length === 1) {
return sum;
} else {
return digitalRoot(sum);
}
}
12 changes: 12 additions & 0 deletions codewars/Sum of pairs/sum_of_pairs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function sumPairs(ints, s) {
var seen = {};

for (let i = 0; i < ints.length; i++) {
let num = ints[i];
if (seen[s - num]) {
return [s - num, num];
}
seen[num] = true;
}
return undefined ;
}
29 changes: 29 additions & 0 deletions codewars/Tic-Tac-Toe Checker/tic-tac-toe_checker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function isSolved(board) {
for (let i = 0; i <= 2; i++) {
if (board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][0] != 0) {
return board[i][0];
}
}

let arr = board[0].concat(board[1], board[2]);

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

if (arr[0] == arr[4] && arr[0] == arr[8] && arr[0] != 0) {
return arr[0];
}

if (arr[2] == arr[4] && arr[2] == arr[6] && arr[2] != 0) {
return arr[2];
}

if (arr.includes(0)) {
return -1;
} else {
return 0;
}
}
16 changes: 16 additions & 0 deletions codewars/Valid Parentheses/valid_parentheses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function validParentheses(parens) {
if (parens[0] === ")" || parens.length % 2 !== 0) {
return false;
}
const leftSymbols = [];
for (let i = 0; i < parens.length; i++) {
if (parens[i] === '(') {
leftSymbols.push(parens[i]);
} else if (parens[i] === ')' && leftSymbols.length !== 0) {
leftSymbols.pop();
} else {
return false;
}
}
return leftSymbols.length === 0;
}
5 changes: 5 additions & 0 deletions codewars/Where my anagrams at/where_my_anagrams_at.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function anagrams(word, words) {
const sample = word.split('').sort().join('');
const result = words.filter((anag) => anag.length === sample.length && anag.split('').sort().join('') === sample);
return result;
}

0 comments on commit b819e7a

Please sign in to comment.