-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
229 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,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; | ||
} |
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){ | ||
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; | ||
} |
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 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; | ||
} |
10 changes: 10 additions & 0 deletions
10
codewars/Convert string to camel case/convert_string_to_camel_case.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,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; | ||
} |
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 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
15
codewars/Find the missing letter/find_the_missing_letter.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,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; | ||
} |
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 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; | ||
} |
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 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
14
codewars/Moving Zeros To The End/moving_zeros_to_the_end.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,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 ; | ||
} |
10 changes: 10 additions & 0 deletions
10
codewars/Product of consecutive Fib numbers/product_of consecutive_fib_numbers.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,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]; | ||
} |
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 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(' '); | ||
} |
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 @@ | ||
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; | ||
} |
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 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); | ||
} | ||
} |
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 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 ; | ||
} |
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,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; | ||
} | ||
} |
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 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; | ||
} |
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,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; | ||
} |