Created
April 10, 2018 13:51
-
-
Save niradler/c667371372fb4360cf21341b85b0723c to your computer and use it in GitHub Desktop.
wix recruitment day
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
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
function solution(A) { | |
// write your code in JavaScript (Node.js 8.9.4) | |
const merge_sort = (A) => { | |
//https://gist.github.com/paullewis/1982121 | |
function sort(array) { | |
var length = array.length, | |
mid = Math.floor(length * 0.5), | |
left = array.slice(0, mid), | |
right = array.slice(mid, length); | |
if (length === 1) { | |
return array; | |
} | |
return merge(sort(left), sort(right)); | |
} | |
function merge(left, right) { | |
var result = []; | |
while (left.length || right.length) { | |
if (left.length && right.length) { | |
if (left[0] < right[0]) { | |
result.push(left.shift()); | |
} else { | |
result.push(right.shift()); | |
} | |
} else if (left.length) { | |
result.push(left.shift()); | |
} else { | |
result.push(right.shift()); | |
} | |
} | |
return result; | |
} | |
return sort(A) | |
} | |
const find_a = (b, c, sortedA) => { | |
for (let i = sortedA.length - 1; i > -1; i--) { | |
if (sortedA[i] + b > c) return sortedA[i]; | |
} | |
return -1; | |
} | |
const cut_arr = (arr, index) => { | |
const temp_arr = []; | |
for (let i in arr) { | |
if (i < index) temp_arr.push(arr[i]) | |
} | |
return temp_arr; | |
} | |
const find_triplet = (sortedA) => { | |
for (let i = sortedA.length - 1; i > 0; i--) { | |
const c = sortedA[i] | |
const b = sortedA[i - 1] | |
const tempArr = cut_arr(sortedA, i - 1); | |
const a = find_a(b, c, tempArr) | |
if (a != -1) | |
return a + b + c; | |
} | |
return -1 | |
} | |
const sortedA = merge_sort(A) | |
const max_tri = find_triplet(sortedA); | |
if (max_tri) return max_tri; | |
return -1; | |
} | |
// you can write to stdout for debugging purposes, e.g. console.log('this is a | |
// debug message'); | |
function solution(S, T) { | |
// write your code in JavaScript (Node.js 8.9.4) | |
const str_to_arr = (str) => { | |
const temp_str = []; | |
let index = 0; | |
for (let i of str) { | |
index++; | |
if (isNaN(i)) { | |
temp_str.push(i) | |
} else { | |
let number_of_err = i, | |
new_str = str.substring(index, str.length); | |
console.log('number_of_err', i, 'substring', new_str) | |
for (let j of new_str) { | |
if (isNaN(j)) { | |
console.log('t_number_of_err', number_of_err, j) | |
number_of_err = number_of_err + j; | |
} else { | |
break; | |
} | |
} | |
console.log('number_of_err', number_of_err) | |
for (let j = 0; j < Number(number_of_err); j++) { | |
temp_str.push('?') | |
} | |
} | |
} | |
return temp_str; | |
} | |
const s_arr = str_to_arr(S); | |
const t_arr = str_to_arr(T); | |
console.log(s_arr, t_arr) | |
if (s_arr.length != t_arr.length) | |
return false; | |
for (let i = 0; i < s_arr.length; i++) { | |
if (s_arr[i] == t_arr[i] || (s_arr[i] == '?' || t_arr[i] == '?')) | |
continue; | |
else { | |
return false; | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment