Skip to content

Instantly share code, notes, and snippets.

View leonidkuznetsov18's full-sized avatar
:electron:
stay hungry, stay foolish

Leonid Kuznetsov leonidkuznetsov18

:electron:
stay hungry, stay foolish
View GitHub Profile
@leonidkuznetsov18
leonidkuznetsov18 / continuously_fetching.js
Created April 29, 2021 15:28
continuously fetching data
const getData = () =>
new Promise((resolve) =>
setTimeout(() => resolve(Math.random() * 5), Math.random() * 2000)
);
const fetchThrottledData = () => {
const arrOfData = [];
let startTime = new Date().getTime();
const res = () => {
@leonidkuznetsov18
leonidkuznetsov18 / firstUniqChar.js
Created April 30, 2021 10:29
First Unique Character in a String
const firstUniqChar = (s) => {
let res = new Map();
[...s].forEach((i) => {
res.has(i) ? res.set(i, res.get(i) + 1) : res.set(i, 1)
})
for (const [key, value] of res.entries()) {
if(value === 1) {
return s.indexOf(key)
@leonidkuznetsov18
leonidkuznetsov18 / smallest_substr.js
Last active May 9, 2022 21:56
smallest substring of N that contains all the characters in K
// We have an array of strings stored in strArr, which will contain only two strings,
// the first parameter being the string N and the second parameter being a string K of some characters.
// Your goal is to determine the smallest substring of N that contains all the characters in K.
// For example: if strArr is ["aabdccdbcacd", "aad"] then the smallest substring of N that contains
// all of the characters in K is "aabd" which is located at the beginning of the string.
// Both parameters will be strings ranging in length from 1 to 50 characters and
// all of K's characters will exist somewhere in the string N. Both strings will only
// contain lowercase alphabetic characters.
// Examples
@leonidkuznetsov18
leonidkuznetsov18 / minWindow.js
Created May 3, 2021 14:00
Minimum Window Substring
/**
* @param {string} s
* @param {string} t
* @return {string}
*/
var minWindow = function(s, t) {
let map = new Map();
let sLen = s.length;
let tLen = t.length;
@leonidkuznetsov18
leonidkuznetsov18 / max_depth.js
Created May 6, 2021 11:40
binary tree height
function TreeNode(val) {
this.val = val;
this.left = this.right = null;
}
/**
* @param {TreeNode} root
* @return {number}
*/
const maxDepth = root => {
@leonidkuznetsov18
leonidkuznetsov18 / reduce_ex.js
Created May 6, 2021 15:19
exercise with reduce
mocha.setup('bdd');
var expect = chai.expect;
const employees = [
{ name: "Barak Obama", role: "president", pay: 35000 },
{ name: "Noa Kirel", role: "artist", pay: 1000000 },
{ name: "Donald Trump", role: "president", pay: 40000 },
{ name: "Tova Cohen", role: "teacher", pay: 135210 },
{ name: "Ivri Lider", role: "artist", pay: 32000 },
@leonidkuznetsov18
leonidkuznetsov18 / promise.js
Created May 7, 2021 09:23
promise from scratch
class CustomPromise {
state = "PENDING"
value = undefined
thenCallbacks = []
errorCallbacks = []
constructor(action) {
action(this.resolver.bind(this), this.reject.bind(this))
}
@leonidkuznetsov18
leonidkuznetsov18 / dynamic_icon.js
Created December 16, 2021 14:51
react dynamic svg icon component
export const Icon: React.FC<IconProps> = ({
name,
...rest
}): JSX.Element | null => {
const ImportedIconRef = React.useRef<
React.FC<React.SVGProps<SVGSVGElement>>
>();
const [loading, setLoading] = React.useState(false);
React.useEffect((): void => {
@leonidkuznetsov18
leonidkuznetsov18 / string_to_tree.js
Last active May 19, 2022 11:36
string to tree js
const paths = ["path/to/file1.doc", "path/to/file2.doc", "foo/bar.doc"];
const stringToTree = (data) => {
return data.reduce((r, path) => {
path.split('/').reduce((o, name) => {
var temp = (o.children = o.children || []).find(q => q.name === name);
if (!temp) o.children.push(temp = { name });
return temp;
}, r);
const data = [
{
"name": "path",
"children": [
{
"name": "to",
"children": [
{
"name": "file1",
"value": true,