Skip to content

Instantly share code, notes, and snippets.

View marsgpl's full-sized avatar
🔴
Recording

Iurii Belobeev marsgpl

🔴
Recording
View GitHub Profile
@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) {
@marsgpl
marsgpl / openUrl.dart
Last active May 11, 2020 13:42
Flutter open url
import 'package:url_launcher/url_launcher.dart';
Future<bool> openUrl(String url) async {
if (url == null || url.trim().length == 0) {
return false;
}
url = Uri.encodeFull(url.trim());
if (!url.contains('://')) {
const fetch = require("node-fetch")
const fetchAll = async function(urls, max=3) {
const results = Array(urls.length)
const workers = Array(max)
const index = {value:0}
for (let i=0; i<max; ++i) {
workers.push(fetchAll.worker(urls, results, index))
}
const rotateWords = function(arr) {
rotateWords.reverse(arr, 0, arr.length-1)
let firstSpaceIndex = arr.indexOf(" ")
let secondSpaceIndex = arr.indexOf(" ", firstSpaceIndex + 1)
rotateWords.reverse(arr, 0, firstSpaceIndex - 1)
rotateWords.reverse(arr, firstSpaceIndex + 1, secondSpaceIndex - 1)
rotateWords.reverse(arr, secondSpaceIndex + 1, arr.length - 1)
// ABAZDC, BACBAD => ABAD
// AGGTAB, GXTXAYB => GTAB
// aaaa, aa => aa
const getLongestSubseq = function(s1, s2) {
let longest = null
for ( let s1from=0; s1from<s1.length; ++s1from ) {
let current = []
let s1i = s1from, s2i = 0
const genBrackets = function(n) {
const result = []
if ( n > 0 ) {
genBrackets.add(n, 0, "", result)
}
return result.join(" ")
}
var moneyTypes = [5000, 1000, 500, 100, 50, 30]
var lims = {
5000: 4,
1000: 5,
500: 2,
100: 5,
50: 100,
30: 23
}