Skip to content

Instantly share code, notes, and snippets.

View omargfh's full-sized avatar

Omar Ibrahim omargfh

View GitHub Profile
@omargfh
omargfh / InfiniteScroll.ts
Last active May 3, 2024 18:54
InfiniteScroll
// 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";
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
@omargfh
omargfh / StarAllRepos.js
Last active January 15, 2023 03:08
Stars all repositories on a page where the "Star" button is visible with 500ms grace period
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;
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])}
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);
}
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
@omargfh
omargfh / topKFrequent.ts
Created November 21, 2022 03:00
Runtime 79 ms Beats 97.77%
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);
}
}
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];
}
@omargfh
omargfh / twoSum.ts
Last active November 21, 2022 02:12
Runtime 105 ms Beats 84.2%
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;
}
@omargfh
omargfh / validAnagram.ts
Last active November 21, 2022 01:23
LeetCode Runtime: 81ms (top 2.45%), Memory: 43.7 MB (top 3%)
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);