Skip to content

Instantly share code, notes, and snippets.

View spirinvladimir's full-sized avatar
💭
💯

Spirin Vladimir spirinvladimir

💭
💯
View GitHub Profile
var m = [
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
'Sunday'
]
const N = [].concat(
function insert (node, x) {
if (x < node.value) {
if (node.left) {
if (x < node.left.value) {
insert(node.left, x)
} else {
node.left = {value: x, left: node.left}
}
} else {
node.left = {value: x}
@spirinvladimir
spirinvladimir / replace_without_case_sensitive.js
Created February 20, 2020 10:23
function should replace in string A to B without case sensitive between A and B
function replace_without_case_sensitive (where, from, to) {
return where.toLowerCase().split(from.toLowerCase()).reduce(
(acc, _) =>
[
acc[0] + _.length + from.length,
acc[1].concat(where.substring(acc[0], acc[0] + _.length))
],
[0, []]
)[1].join(to)
}
@spirinvladimir
spirinvladimir / 1.js
Last active November 6, 2019 09:26
Amazon - Online Assessment
function cellCompete(states, days) {
if (days === 0) return states
const a = new Array(states.length)
states.push(0)
states.unshift(0)
for (var i = 1; i < states.length - 1; i++)
a[i - 1] = states[i - 1] === states[i + 1]
? 0
: 1
return cellCompete(a, days - 1)
var deferred = require('deferred');
var assert = require('assert');
describe('deferred', function () {
it('map', function (done) {
var def1 = deferred();
var def2 = deferred();
setTimeout(function () {
def1.resolve(1);
@spirinvladimir
spirinvladimir / promise_redis.js
Created April 9, 2019 20:41
Node.js callback -> Promises , PS: many thanks Redis
new Promise((y, n) => redis((e, db) => e ? n(e) : y(db)))
@spirinvladimir
spirinvladimir / max_malloc.js
Created February 12, 2019 06:31
Max memory allocation at browser
var i = 0;
var bigest = (bytes) => {
console.log(i++, bytes);
try {
return new ArrayBuffer(bytes)
} catch (e) {
return bigest(bytes / 2)
}
}
@spirinvladimir
spirinvladimir / 100.js
Last active October 31, 2018 08:31
Solution for task "Sum 100" by https://www.diginex.com
/*Question 1: Sum to 100
Write a function that, given a string of digits, build an expression such that the digits will sum to 100 by inserting +, - anywhere between the digits.
For example, given the input 123456789, one such expression is 1 + 23 - 4 + 5 + 6 + 78 - 9 = 100
Your function should return all possible combinations.
*/
var results = {};
function calc(n, s, part) {
if (n === 0 && s === '' && part[0] !== '-') return results[part.substring(1)] = null
@spirinvladimir
spirinvladimir / index.js
Created October 3, 2018 10:24
Skiing in Singapore - a coding diversion by Redmart
var fs = require('fs');
var AVLTree = require('avl');
var tree = new AVLTree();
var map = fs.readFileSync('map.txt', 'utf-8').split('\n').map(l => l.split(' ').map(c => Number(c)));
var [I, J] = map.shift();
var result = {
path: 0,
drop: 0
};
@spirinvladimir
spirinvladimir / follow-the-white-rabbit.js
Last active September 17, 2018 05:32
Solution for "Follow The White Rabbit" by TrustPilot
var
fetch = require('node-fetch'),
md5 = require('md5'),
md5s = ["e4820b45d2277f3844eac66c903e84be", "23170acc097c24edb98fc5488ab033fe", "665e5bcb0c20062fe8abaaf4628bb154"],
match_md5 = frase => md5s.indexOf(md5(frase)) !== -1,
word2map = word => word.split('').reduce((acc, c) => {acc[c] = acc[c] || 0; acc[c]++; return acc}, {}),
match = rm => word => {
var char, i = 0, wm = {};
while (i < word.length) {
char = word[i];