Skip to content

Instantly share code, notes, and snippets.

View prendradjaja's full-sized avatar

Pandu Rendradjaja prendradjaja

View GitHub Profile
@prendradjaja
prendradjaja / readme.md
Last active September 5, 2024 00:38
type and interface in typescript

IMO the right question is "Why do both type and interface exist?"

You already know why interface exists -- so you can define an object type by naming its properties e.g.

interface Point {
    x: number;
    y: number;
}
@prendradjaja
prendradjaja / _readme.md
Last active August 8, 2024 03:18
Remove element in-place

https://leetcode.com/problems/remove-element/description/

This was interesting: I made this problem a lot harder by trying to move the "to remove" elements to the end instead of moving the "to keep" elements to the beginning.

I found my idea hard to implement iteratively, and I had to switch to recursion to get it working. After that, I was able to convert my solution to iterative -- but it would've been pretty hard to implement this approach iteratively from the start!

Guards

main = do
  print $ mySignum (-2)
  print $ mySignum 0
  print $ mySignum 2

mySignum n
  | n < 0 = -1
 | n &gt; 0 = 1
@prendradjaja
prendradjaja / tables.txt
Created March 31, 2023 17:00
How precise is the Fibonacci distances trick?
km | mi (est.) | mi (act.) | error
-----------------------------------
1 | 1 | 0.62 | 60.9%
2 | 1 | 1.24 | -19.5%
3 | 2 | 1.86 | 7.3%
5 | 3 | 3.11 | -3.4%
8 | 5 | 4.97 | 0.6%
13 | 8 | 8.08 | -1.0%
21 | 13 | 13.05 | -0.4%
34 | 21 | 21.13 | -0.6%
import re
def sscanf(s, fmt):
'''
Parses the string against the given template, returning the values of the
slots. If no match, return None.
The string must be a full match (like re.fullmatch()).
class Draggable {
private element: HTMLElement;
private currentDrag: undefined | {
initialMouseX: number,
initialMouseY: number,
initialElementX: number,
initialElementY: number,
removeListeners: () => void,
};
@prendradjaja
prendradjaja / line.py
Created March 22, 2022 20:44
Linear equation in Python
import math
class Line:
def __init__(self, m, b):
self.m = m
self.b = b
def __call__(self, x):
return self.m * x + self.b
@prendradjaja
prendradjaja / aoc-2021-20.md
Last active December 28, 2021 19:21
'use block' examples