This file contains hidden or 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 medianBySort(values: number[]) { | |
| values.sort((a, b) => a - b) | |
| const half = Math.floor(values.length / 2) | |
| if (values.length % 2) { | |
| return values[half] | |
| } | |
| return (values[half - 1] + values[half]) * 0.5 | |
| } | |
| function medianByK(values: number[], k: number): number { |
This file contains hidden or 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 getP(x: number): number { | |
| return Math.floor(Math.log2(x)) | |
| } | |
| export interface ResultPair<T> { | |
| value: T | |
| index: number | |
| } | |
| export function rmq<T>( |
This file contains hidden or 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 Vector = number[] | |
| function add(u: Vector, v: Vector): Vector { | |
| return u.map((x, i) => x + v[i]) | |
| } | |
| function sub(u: Vector, v: Vector): Vector { | |
| return u.map((x, i) => x - v[i]) | |
| } |
This file contains hidden or 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 CHARSET = '01' | |
| const UNRECOGNIZED = -1 | |
| // -1: unrecognized | |
| // other: next token position | |
| type NextIndex = number | typeof UNRECOGNIZED | |
| type Recognizer<T extends any[] = []> = (i: number, ...rest: T) => NextIndex | |
| function parse(s: string): boolean { | |
| const isRecognizedChar: Recognizer<[string]> = (i, allowedCharacters) => { |
This file contains hidden or 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
| /* | |
| * Please read the comment first to understand this creepy pasta. | |
| * Unfortunately, I don't have any ideas of implementing complete replica of function* (for return behavior) | |
| * | |
| * @param {function} f Schema of generator function scheme which receives any parameters and | |
| * - may return `{ value, nextF }` or `undefined` if there is nothing to yield. | |
| * - *value* is for `yield` and *nextF* for aftermath of `yield`. | |
| * - *nextF* has the same manner as *f*. | |
| * @return {function} Generator function which returns generator, which behaves like | |
| * - as if returned values of f were 'yielded'. |
This file contains hidden or 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 { DetailedHTMLProps, InputHTMLAttributes, MouseEvent, useRef } from 'react' | |
| interface Props extends DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement> { | |
| isApprovedInitial?: boolean | |
| onBeforeFileSelection(): Promise<boolean> | |
| } | |
| export default function InteractiveFileInput({ isApprovedInitial, onBeforeFileSelection, ...rest }: Props) { | |
| const dom = useRef<HTMLInputElement>(null) | |
| const isApproved = useRef<boolean>(isApprovedInitial ?? false) |
This file contains hidden or 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
| /** | |
| * Return the exp-th power of base in O(lg exp) time complexity. | |
| * @param {number} base - any real number | |
| * @param {number} exp - any non negative integer | |
| * @returns base ^ exp | |
| */ | |
| export function pow(base: number, exp: number): number { | |
| if (exp <= 0) return 1 | |
| if (exp === 1) return base |
This file contains hidden or 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 interface FinderOption { | |
| f: (x: number) => boolean | |
| p: number | |
| epsilon: number | |
| } | |
| // This is theoretically pure, but impractical since this may result stack overflow due to heavy recursion | |
| export function findDecisionBoundary(option: FinderOption, lower: number, upper: number, n: number = 0): { | |
| boundary?: number | |
| n: number |
This file contains hidden or 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 { useCallback, useRef } from 'react' | |
| type MouseEventHandler = (e: MouseEvent) => void | |
| export function useOutsideClickHandler<T extends HTMLElement>( | |
| callback: MouseEventHandler, | |
| ) { | |
| const userCallback = useRef<MouseEventHandler>(() => {}) | |
| userCallback.current = callback |
This file contains hidden or 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 type Relation = [string, string] | |
| function makeGraph(relations: Relation[]): Record<string, string[]> { | |
| const result: Record<string, string[]> = {} | |
| relations.forEach(([u, v]) => { | |
| (result[u] ??= []).push(v) | |
| result[v] ??= [] | |
| }) | |
| return result | |
| } |