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 React, { useEffect, useRef, useState } from 'react'; | |
class VolumeGenerator { | |
public name: string; | |
private volume: number; | |
private delta: number; | |
constructor(name: string, initialVolume = 50) { | |
this.name = name; | |
this.volume = initialVolume; |
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 type { Plugin, UserConfig } from 'vite'; | |
type CrossOriginValue = 'anonymous' | 'use-credentials' | ''; | |
interface CrossOriginPluginOptions { | |
/** | |
* The value to set for the crossorigin attribute. | |
* @default 'use-credentials' | |
*/ | |
crossOriginValue?: CrossOriginValue; |
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 sleep(ms: number) { | |
return new Promise((resolve) => setTimeout(resolve, ms)); | |
} | |
class AsyncQueue<T> { | |
queuedItems: (T | Error)[]; | |
queuedProcessors: [(item: T) => void, (error: Error) => void][]; | |
constructor() { | |
// Note: The FIFO `shift` operations we do on these arrays are `O(n)`. |
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
from operator import eq, attrgetter as attr | |
from typing import TypeVar, Callable, Iterator, Optional | |
eq_or_none = lambda x, y: y is None or eq(x, y) | |
T = TypeVar('T') | |
def follow(start: T, next_fn: Callable[[T], T], stop: Callable[[T, T], bool] = eq_or_none) -> Iterator[Optional[T]]: | |
yield start |
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
type Slice = [lowIndex: number, highIndex: number]; | |
function getRandomIntegerInRange([low, high]: Slice): number { | |
return Math.floor(Math.random() * (high - low + 1)) + low; | |
} | |
function createPartitionForSlice([lowIndex, highIndex]: Slice) { | |
return function partition(pivotIndex: number, items: number[]): number { | |
// Partitioning a slice works like so: | |
// - We get the pivot value from the pivot index (in our case, a random index |
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
export class TrieNode { | |
public children: Record<string, TrieNode> = {}; | |
public isEndOfWord = false; | |
} | |
export class Trie { | |
private root: TrieNode; | |
constructor() { | |
this.root = new TrieNode(); |
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 Solution: | |
def search(self, nums: List[int], target: int) -> int: | |
# Special case for lists with only one or two elements. | |
if len(nums) <= 2: | |
try: | |
return nums.index(target) | |
except: | |
return -1 | |
# As the array has been sorted and then rotated, it's not actually |
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 Solution: | |
def findMin(self, nums: List[int]) -> int: | |
# Special case for lists with only two elements. | |
if len(nums) == 2: | |
return min(*nums) | |
# As the array has been sorted and then rotated, it's not actually correctly | |
# sorted and we can't use a binary search in the conventional way. However, | |
# because we're able to make definitive decisions on which side the minimum | |
# value will be found at on every iteration we're still able to use binary |
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
type Zipped<A extends readonly (readonly any[])[]> = { | |
[RowIndex in keyof A[0]]: RowIndex extends "length" ? number : { | |
[ArrayIndex in keyof A]: A[ArrayIndex] extends readonly any[] ? A[ArrayIndex][RowIndex] : never; | |
}; | |
}; | |
type NumericRange<TotalLength extends number, TempRange extends any[] = []> = | |
TempRange['length'] extends TotalLength ? TempRange : NumericRange<TotalLength, [...TempRange, TempRange['length']]>; | |
type TupleFromKeys<T, K extends readonly (keyof T)[]> = { |
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
type Constructor<T> = new (...args: any[]) => T; | |
type DefaultType<T> = T extends Constructor<infer U> ? U : T; | |
function defaultObject<V, K extends string | symbol = string | symbol>(Type: Constructor<V> | V): Record<K, DefaultType<V>> & Iterable<[K, DefaultType<V>]> { | |
const store = new Map<K, DefaultType<V>>(); | |
const iterable = { | |
*[Symbol.iterator] () { | |
for (let [key, value] of store) { | |
yield [key, value]; |
NewerOlder