Skip to content

Instantly share code, notes, and snippets.

View kayac-chang's full-sized avatar
:octocat:
kirby

Kayac Hello kayac-chang

:octocat:
kirby
View GitHub Profile
// Write a function that takes two arrays as input, each array contains a list of A-Z;
// Your program should return True if the 2nd array is a subset of 1st array, or False if not.
// For example:
// isSubset([A,B,C,D,E], [A,E,D]) = true
// isSubset([A,B,C,D,E], [A,D,Z]) = false
// isSubset([A,D,E], [A,A,D,E]) = true
import { test, expect } from "vitest";
import { test, expect, describe } from "vitest";
function recur(n: number, cur?: number): number {
if (!cur) {
cur = 0;
}
if (n < 2) {
throw new Error("Invalid input");
}
if (n === 2) {
@kayac-chang
kayac-chang / cache.ts
Created July 5, 2023 12:38
cache with expired
/**
Design and implement a data structure for a cache,
which has the following functions:
- `get(key)`
Return the value associated with the specified key
if it exists in the cache, else return -1 .
- `put(key, value, weight)`
Associate with key in the cache,
such that might be later retrieved by `get(key)`.
@kayac-chang
kayac-chang / find-name-contain-most-words.ts
Created July 5, 2023 09:02
Find name contain most words
import { test, expect } from "vitest";
function isLowercase(char: string): boolean {
return char >= "a" && char <= "z";
}
function isUppercase(char: string): boolean {
return char >= "A" && char <= "Z";
}
@kayac-chang
kayac-chang / wrapForSuspense.ts
Created October 25, 2022 04:05
using suspense for data fetching or any promise
type PromiseState = "initial" | "pending" | "success" | "error";
export function wrapForSuspense<Task extends (...args: any[]) => Promise<any>>(
task: Task
) {
let status: PromiseState = "initial";
let result: Awaited<ReturnType<Task>>;
let suspend: Promise<void>;
return (...args: Parameters<Task>) => {
@kayac-chang
kayac-chang / check_credit_card.py
Created May 22, 2022 12:19
check_credit_card
def is_amax(card_number):
return re.search(r"(?=\b.{15}\b)^(34|37)", card_number)
def is_master_card(card_number):
return re.search(r"(?=\b.{16}\b)^5[1..5]", card_number)
def is_visa(card_number):
return re.search(r"(?=\b.{13,16}\b)^4", card_number)
@kayac-chang
kayac-chang / basic.ts
Created January 31, 2022 10:01
[learn FP with Kirby using `fp-ts`] Array
/**
* Array
* ===
* Basic array
*/
import { pipe } from "fp-ts/lib/function";
import * as Array from "fp-ts/Array";
const foo = [1, 2, 3, 4, 5];
@kayac-chang
kayac-chang / chain-or-else.ts
Created January 31, 2022 05:18
[learn FP with Kirby using `fp-ts`] TaskEither
/**
* chain and orElse
* ===
* sequential asynchronous processing can combined with chain and orElse
*/
import { pipe } from "fp-ts/lib/function";
import {
chain,
tryCatch,
@kayac-chang
kayac-chang / chain.ts
Created January 30, 2022 12:34
[learn FP with Kirby using `fp-ts`] Either
/**
* Chain with Either
* ===
* chain help solve nested either structure
*/
import { flow, pipe } from "fp-ts/lib/function";
import { Password, validate } from "./_prepare";
import { map, Either, chain, right } from "fp-ts/Either";
@kayac-chang
kayac-chang / task.ts
Created January 30, 2022 05:34
[learn FP with Kirby using `fp-ts`] Task
import { describe } from "../utils";
/**
* Task
* ===
* A task is a function that returns a promise which is expected to never be rejected.
*/
/**
* Why use Task?