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 / recursive_rendering.jsx
Created July 10, 2020 15:21
recursive rendering tree in react
const Tree = ({ data }) => {
return data?.map((p: NumberingBranch) => {
return (
<div key={p.id} className="NumberingList__Parent">
<div>
<div className="NumberingList__ItemWrapper">
<span className="NumberingList__Title">{p.data.text}</span>
</div>
</div>
{p.children.length > 0 && <Tree data={p.children} />}
@leonidkuznetsov18
leonidkuznetsov18 / convert_to_tree_based_on_left_indent.js
Last active July 13, 2020 06:53
convert flatten array to tree structure based on leftIndent. From Word Pargraphs.
const makeTree = (paragraphs) => {
const findParentLevel = (levels, level) => {
if (levels[level]) {
return level;
}
return findParentLevel(levels, level - 1);
};
const tree = paragraphs.reduce(
(acc, p) => {
@leonidkuznetsov18
leonidkuznetsov18 / flettentArr_leftIndent.js
Created July 20, 2020 15:03
make falttenArray based on left indent
public makeFlatNumberingArray = (paragraphs) => {
const resultArr = [];
let levelData = null;
for (let i = 0; i < paragraphs.length; i++) {
const curr = paragraphs[i];
let level = 0;
let parentId = null;
let levelString = curr.data.isListItem ? curr.data.listItem.listString : hasNumbering(curr.data.text)[0];
for (let j = i - 1; j >= 0; j--) {
@leonidkuznetsov18
leonidkuznetsov18 / tree_from_flatten_arr.js
Last active August 26, 2020 11:51
make tree from flatten array
const createDataTree = dataset => {
let hashTable = Object.create(null)
dataset.forEach( aData => hashTable[aData.id] = { ...aData, childNodes : [] } )
let dataTree = []
dataset.forEach( aData => {
if( Number.isInteger(aData.parentId)) {
hashTable[aData.parentId].childNodes.push(hashTable[aData.id])
} else {
dataTree.push(hashTable[aData.id])
}
const input = [
{ type: "dir", name: "src", contents:
[
{ type: "dir", name: "doc", contents:
[
{ type: "file", name: "TOC.md" },
{ type: "file", name: "css.md" },
{ type: "file", name: "extend.md" },
{ type: "file", name: "faq.md" },
{ type: "file", name: "html.md" },
@leonidkuznetsov18
leonidkuznetsov18 / event_bus.ts
Created April 11, 2021 19:07
Event Bus Typescript
import { EventEmitter } from 'eventemitter3';
type ListenerFn = (...args: Array<any>) => void;
class EventBus {
public eventEmitter: EventEmitter;
/**
* Initiate the event emitter
*/
constructor() {
@leonidkuznetsov18
leonidkuznetsov18 / children_on_flight.ts
Last active May 2, 2021 21:28
Children on the Flight
// Task 1. Children on the Flight.
// We are building Web application for the airlines.
// There is a page where we show list of passengers
// for the given flights.
// There is an age restriction:
// All children younger than 5 years old should have
// at least one adult parent or guardian.
// Parent/guardian should be at least 18 years old.
// We want to highlight all children who are not
// allowed to be on the flight.
@leonidkuznetsov18
leonidkuznetsov18 / maximum_budget.ts
Last active April 28, 2021 16:04
Maximum Budget Problem
// Task 2. Maximum Budget Problem.
// We want to find out the most expensive trip combinations that can be purchased with a given budget. Given the price lists for two cities and a budget, find the total cost to buy them.
// Return maximum budget that can be spent, or -1 if it is not possible to buy both trips.
// Example 1
// Total trip budget = 10
// Paris trips = [3, 1]
// Barcelona trips = [5, 2, 8]
// Sample Output: 9
@leonidkuznetsov18
leonidkuznetsov18 / price_simple_parts.ts
Last active April 28, 2021 14:53
Price of simple parts
// Task 3. Price of simple parts
// We are selling computer hardware. We have products assembled from parts.
// For example PC is build from motherboard, cpu, hard drive, etc. Some parts are simple: they have no sub parts like CPU.
// When the part has subparts it is not simple. For example PC body has different subparts like covers, screws, etc.
// Every part has price.
// Task:
// For a given list of parts find total price of the all simple parts.
@leonidkuznetsov18
leonidkuznetsov18 / closure_push_arr.js
Created April 29, 2021 15:12
closure push to array
const gArray = () => {
var array = [];
const a = (value) => {
array.push(value);
return array;
}
return a;
};
const b = gArray();