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
| /// <reference lib="esnext" /> | |
| interface ValidatorFieldKind<D> { | |
| validate(value: any): D | Error[] | |
| } | |
| type ValidatorFieldDef<R extends boolean, D> = { | |
| required: R, | |
| kind: ValidatorFieldKind<D> | |
| } |
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
| interface CodecFieldType<D> { | |
| encode(decoded: D): string | |
| decode(encoded: string): readonly [D, rest: string] | |
| } | |
| type CodecFieldDef<R extends true | false, D> = { | |
| required: R, | |
| type: CodecFieldType<D> | |
| } |
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 getIndentation(lines: string[] | TemplateStringsArray): string { | |
| let firstIndent: string | |
| if (lines[0] === '') { | |
| firstIndent = lines[1] | |
| } else { | |
| firstIndent = lines[0] | |
| } | |
| const matched = firstIndent.match(/^(\s+).*$/) |
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
| iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAYAAABccqhmAAAOuUlEQVR4AeycbXLjSA5EWXMxqU9m6WRtn4zbOduO8GpFCWB9F97E1NimQAD5QOUvDP/Z+QcCEAhL4J+NfyAAgbAEMICwo0c4BLYNA+ApgEBQApKNAYgCBwJBCWAAQQePbAiIAAYgChwIBCWAAQQdPLJjE/hWjwF8k+AnBAISwAACDh3JEPgmgAF8k+AnBAISwAACDh3JsQn8VI8B/KTB7xAIRgADCDZw5ELgJwEM4CcNfodAMAIYQLCBIzc2gUf12QaQUtpS4qT0msGvX78e2Tf9+/Pzs8qcaogQq5Re80yJz1NK2fizDSC7AxJAAALdCGAA3dBTGAL9CWAA/WdABxBoQuBZEQzgGRWuQSAIAQwgyKCRCYFnBDCAZ1S4BoEgBDCAIINGZmwCR+oxgCMyXIdAAAIYQIAhIxECRwSaGsD1et2ui50jsDnXtbVX+nx9fZlb8szInLRSoKfXWWIroXqatpkBCP7v37+31c5TqpkX7/f7pnXYkud2u5m7+vj4MM/JnLRS4GrPk/Tou1IS16tczQzgVRN8BgEI9CGAAfThTlUIDEEAAxhiDDQBgT4EMIA+3KkKgSYE3hXBAN4R4nMILEwAA1h4uEiDwDsCGMA7QnwOgYUJYAALDxdpsQlY1GMAFkrEQGBRAkMagLbWSm7BeXNpDXfReYeVpZl6n4OS8XqmR4Q/pAFob10D63VGHBQ95RPo9Typrp7pfAXlMwxpAOVlkhECsQhY1WIAVlLEQWBBAhjAgkNFEgSsBDAAKyniILAgAQxgwaEiKTYBj3oMwEOLWAgsRgADWGygyIGAhwAG4KFFLAQWI4ABLDZQ5MQm4FWPAXiJNYjXiyH3fd8sx9qOXjRpyacYvZQ0pbSl9P5Y6xM3JgEMYMy50BUEmhDAAJpgpggExiSAAYw5F7qCgJvAmRswgDPUuAcCixDAABYZJDIgcIYABnCGGvdAYBECGMAig0RGbAJn |
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 wx | |
| from wx.core import EVT_TIMER | |
| from Cocoa import NSApp, NSApplication | |
| # Adapted from https://discuss.wxpython.org/t/macos-window-opens-in-the-background-and-does-not-receive-focus/36763/2 | |
| class Frame(wx.Frame): | |
| def __init__(self, *args, **kw): | |
| super().__init__(*args, **kw) | |
| print(f"{self.IsFocusable()=}") |
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
| def unique_paths(m, n) | |
| grid = Array.new(m) | |
| m.times.with_index do |y| | |
| grid[y] = Array.new(n) | |
| grid[y][0] = 1 | |
| n.times.with_index do |x| | |
| from_left = if x - 1 >= 0 then grid[y][x - 1] else 0 end | |
| from_above = if y - 1 >= 0 then grid[y - 1][x] else 0 end |
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
| def rob(nums) | |
| rob_impl(nums, 0) | |
| end | |
| # https://nithinbekal.com/posts/ruby-memoization/ | |
| # https://stackoverflow.com/questions/35595047/how-to-generically-memoize-any-function-in-ruby | |
| # We could use a Class instance to hold the memory | |
| def memoize(method_name) | |
| memory = {} | |
| original = method(method_name) |
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
| # For some reason, only iterative DP passes Leetcode | |
| def coin_change(coins, amount) | |
| result = coin_change_iterative(coins, amount) | |
| if result.infinite? | |
| -1 | |
| else | |
| result | |
| end | |
| end |
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
| class NumMatrix | |
| # (0, 0) is the top left | |
| def initialize(matrix) | |
| @corner_areas = matrix | |
| @corner_areas.each.with_index do |row, y| | |
| row.each.with_index do |value, x| | |
| upper_sum = positive_dig(@corner_areas, y - 1, x) | |
| left_sum = positive_dig(@corner_areas, y, x - 1) | |
| @corner_areas[y][x] = value + upper_sum + left_sum - positive_dig(@corner_areas, y - 1, x - 1) |
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
| # Here is my solution, ignoring the sparse optimization, so we iterate over k. Originally, I wanted to only keep track | |
| # of the locations with fruit. | |
| def max_total_fruits(fruits, start_pos, k) | |
| steps = [] | |
| fruits.each do |pos, amount| | |
| steps[pos] = amount | |
| end | |
| right_sum = 0 |
NewerOlder