Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View marsgpl's full-sized avatar
🧬
AI

Iurii Belobeev marsgpl

🧬
AI
View GitHub Profile
const FACTS = [
'm = 3.28 ft',
'ft = 12 in',
'hr = 60 min',
'min = 60 sec',
]
const QUERY = '2 m = ? in'
const CORRECT_ANSWER = 78.72
@marsgpl
marsgpl / asyncMapper.ts
Last active December 7, 2023 00:43
asyncMapper.ts
// write async generator to return N random strings per call
// add parameter to limit total amount of strings returned by generator
// write async mapper that accepts async generator as argument and
// a callback that is applied for every generator result
// mapper should return all generator results as array
async function getLinesTotal() {
return 8
@marsgpl
marsgpl / findLargestArea.ts
Created December 5, 2023 23:25
findLargestArea.ts
const grid = [
[3, 1, 1, 1],
[2, 1, 0, 1],
[1, 1, 0, 2],
[2, 0, 1, 0],
]
interface Area {
value: number
points: Set<number>
@marsgpl
marsgpl / graph.c
Last active December 5, 2023 06:07
graph.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct graph_node {
char *name;
struct graph_node *parent;
struct graph_node **children;
size_t children_size;
size_t children_n;
@marsgpl
marsgpl / floodFill.ts
Last active December 5, 2023 02:33
floodFill.ts
function floodFill(
image: number[][],
x: number,
y: number,
newColor: number,
): void {
const minY = 0
const maxY = image.length - 1
if (maxY < 0) { return }
@marsgpl
marsgpl / promiseAll.ts
Created December 4, 2023 17:15
promiseAll.ts
async function promiseAll<T = unknown>(promises: Promise<T>[]): Promise<T[]> {
const results: T[] = []
for (let i = 0; i < promises.length; ++i) {
results.push(await promises[i])
}
return results
}
@marsgpl
marsgpl / deepEquals.ts
Last active December 6, 2023 17:28
deepEquals.ts
function deepEquals(v1: unknown, v2: unknown): boolean {
if (v1 === v2) { return true } // equals
const t1 = typeof v1
const t2 = typeof v2
if (t1 !== t2) { return false } // diff types
if (t1 !== 'object') { return false }
// objects or arrays
@marsgpl
marsgpl / findLongestCommonString.ts
Created June 9, 2023 09:27
Find Longest Common String
const S = "sustainable";
const K = "disabling";
interface Tree {
[char: string]: Tree;
}
function buildTree(str: string): Tree {
const tree: Tree = {};
const prevs = [tree];
<root>
root
<a>
a
<aa>aa</aa>
<bb>bb</bb>
</a>
<b>b
<ba>ba</ba>
<bb>bb</bb>
interface PromiseAllContext<T> {
results: T[]
jobs: (() => Promise<T>)[]
launched: number
}
async function worker<T>(context: PromiseAllContext<T>) {
const { jobs, results } = context
while (context.launched < jobs.length) {