This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// InfiniteScroll.ts | |
// Infinite scroll component that fetches data from the server as the | |
// user scrolls. | |
// | |
// Author: Omar Ibrahim | |
// Date: 2024-03-20 | |
// Documented: www.omar-ibrahim.com/blog/articles/5 | |
"use client"; | |
import { LoaderUnit } from "@/components/layout/Loading"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
class ConversationBuilder(): | |
def __init__(self): | |
self.signal_keys = dict() # key -> signal | |
self.signal_responses = dict() # signal -> list of responses | |
self.initial_response = None | |
self.termination_signal = None | |
self.parser = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fn = (i, total) => { | |
string = ""; | |
approx = Math.floor(i / total * 20) | |
for (let n = 0; n < 20; n++) { | |
if (n <= approx) | |
string += "█" | |
else | |
string += "–" | |
} | |
return string; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
d = dict() | |
runningTotal = {a:b for (a, b) in zip(d.keys(), [0] + reduce(lambda x, y: x+[x[-1]+y], d.values(), [0])[1:-1])} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MinStack { | |
#stack = []; | |
#history = [Number.POSITIVE_INFINITY]; | |
constructor() {} | |
push(val: number): void { | |
this.#stack.push(val); | |
if (val <= this.#history[this.#history.length - 1]) { | |
this.#history.push(val); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, useRef, useState } from "react"; | |
type CanvasProps = { | |
settings: Record<string, any>; | |
}; | |
const Canvas = ({ settings }: CanvasProps) => { | |
const canvas = useRef<HTMLCanvasElement>(null); | |
let ctx: CanvasRenderingContext2D | null = null; // This will be initialized when DOM loads |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function topKFrequent(nums: number[], k: number): number[] { | |
let map = new Map<number, number>(); | |
for (const num of nums) { | |
if (map.has(num)) { | |
map.set(num, map.get(num) + 1); | |
} | |
else { | |
map.set(num, 1); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function groupAnagrams(strs: string[]): string[][] { | |
let objectResults: Record<string, string[]> = {}; | |
for (const e of strs) { | |
const hashKey: string = e.split('').sort().join(''); | |
if (objectResults.hasOwnProperty(hashKey)) { | |
objectResults[hashKey] = [...objectResults[hashKey], e]; | |
} | |
else { | |
objectResults[hashKey] = [e]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function hash_core(num: number, bins: number): number { | |
let message: string[] = num.toString().split(''); | |
let hash: number = 5381; | |
message.forEach((e: string) => { | |
hash = (hash << 5) + hash + e.charCodeAt(0); | |
}) | |
return Math.abs(hash) % bins; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const LETTERS: number = 26; | |
const ASCII_LOWER: number = 'a'.charCodeAt(0); | |
function isAnagram(s: string, t: string): boolean { | |
if (s.length !== t.length) { | |
return false; | |
} | |
let s_a: number[] = []; | |
let t_a: number[] = []; | |
for (let i = 0; i < LETTERS; i++) { | |
s_a.push(0); |
NewerOlder