Skip to content

Instantly share code, notes, and snippets.

@autf
autf / 3triangles.js
Last active September 16, 2021 11:38
Exercise 6.1 < 6.3 Translation < edX NYUx DMEDX6063 Creative Coding
function setup() {
createCanvas(500, 500)
background('lightgray')
}
function draw() {
let side = 300
let third = side / 3
noStroke()
translate(100, 100)
require 'nokogiri'
require 'open-uri'
base = 'https://ncode.syosetu.com'
ncode = 'x0000xx'
seen = i = 777
# start from #(seen + 1) chapter
p out = `date -Im`.strip
`mkdir #{out}`
package main
import (
"io/fs"
"log"
"net/http"
"strings"
)
func isVideoFile(name string) bool {
@autf
autf / impl.kt
Last active November 1, 2021 06:24
cs61b sp21 lec14 Disjoint Sets
interface DisjointSets {
fun connect(p: Int, q: Int)
fun isConnected(p: Int, q: Int): Boolean
}
class DisjointSetsByArray(val elements: Int) : DisjointSets {
private val map = IntArray(elements) { -1 }
private fun top(id: Int): Int {
if (id !in map.indices) return -1
N = 1000
# s = N.times.each_with_object([1, 1]) { |_, a| a << 2*a[-2] + a[-1] }
# p [s, s.size]
# # https://www.crondose.com/2017/02/fibonacci-sequence-generator-ruby/
# s = N.times.reduce([1, 1]) { |a| a << 2*a[-2] + a[-1] }
# # https://docs.ruby-lang.org/ja/master/class/Enumerator.html#S_NEW
# enum = Enumerator.new { |y|
@autf
autf / class_as_decorator.py
Created February 14, 2022 07:14
__new__ quacks like __call__
class trace:
lv = 0
# https://docs.python.org/3/reference/datamodel.html#object.__new__
def __new__(cls, fn):
def wrapper(x):
indent = 4 * cls.lv * ' '
cls.lv += 1
print(indent + f'f({x})')
res = fn(x)
class T:
...
def _m(my): print('m', my)
T.m = _m
@classmethod
def _cm(cls): print('cm', cls)
T.cm = _cm
## ...OR
import dis
import rich # not in std
class Foo:
@staticmethod
def bar(co):
rich.inspect(co)
for c in co.co_consts:
if hasattr(c, 'co_code'):
Foo.bar(c)
import dis, json, sys, inspect, base64
bs = b'this is bytes'
plex = 1+2j
def code2dict(co):
d = {}
for k, v in inspect.getmembers(co):
# isbuildtin(x) -> Is x a built-in FUNCTION or METHOD?
if k.startswith('co_') and not inspect.isbuiltin(v):
import sys, itertools
for _, it in itertools.groupby(sorted(sys.stdlib_module_names), lambda s: s[0]):
for x in sorted(it): print(x)
print()