Skip to content

Instantly share code, notes, and snippets.

@SeijiEmery
SeijiEmery / fizzbuzz.py
Last active July 28, 2018 19:07
FizzBuzz, in increasing levels of cleverness + obfuscation
def fizzbuzz (n):
fizz = n % 3 == 0
buzz = n % 5 == 0
if fizz and buzz:
return "FizzBuzz"
elif fizz:
return "Fizz"
elif buzz:
return "Buzz"
// utility/util/mem_bench.hpp
// Copyright (C) 2017 Seiji Emery
//
// Memory + time benchmarking code: this hijacks (overloads) global new / delete
// to trace memory allocations (very simple: # of allocations / frees + # bytes
// allocated / freed), and adds a global variable that displays this stuff
// from its dtor (guaranteed to be called after main() but before program exits).
//
// It also adds basic time profiling (global ctor / dtor) using std::chrono.
//
@SeijiEmery
SeijiEmery / window_management.cpp
Created April 2, 2018 08:08
Game engine window management snippet
//
// Property declaration macros: makes semantics clearer (and saves typing)
//
#define DECL_PROP_GET(name,type) type name () const
#define DECL_PROP_SET(name,type) This& name (type)
#define DECL_PROP_GET_VIRTUAL(name,type) virtual DECL_PROP_GET(name,type) = 0;
#define DECL_PROP_SET_VIRTUAL(name,type) virtual DECL_PROP_SET(name,type) = 0;
@SeijiEmery
SeijiEmery / IObject.cpp
Created March 9, 2018 23:20
c++ overengineering
class TypeInfo;
class InputRange;
class OutputRange;
class IORange;
class IObject {
public:
virtual ~IObject {}
virtual const TypeInfo& typeinfo () = 0;
@SeijiEmery
SeijiEmery / rubiks_cube.py
Last active May 3, 2018 02:43
rubiks cube
# Pseudo code. mix of python, lisp, and haskell syntax
# Wrote for fun after skimming https://en.wikipedia.org/wiki/Rubik%27s_Cube_group
# and http://www.math.harvard.edu/~jjchen/docs/Group%20Theory%20and%20the%20Rubik%27s%20Cube.pdf
# Solve a rubik's cube:
def RC-pre-solve (init-state, operations)
let solutions = {}, todo = [ (init-state, []) ]
# Algorithm is simple:
@SeijiEmery
SeijiEmery / linalg.py
Created November 3, 2017 23:52
Simple gauss-jordan matrix solver. Somewhat useful; might expand in the future or something.
def Matrix (T):
class M:
def __init__ (self, *args):
if (type(args[0]) == type(self)):
self.rows = [ [ x for x in row ] for row in args[0].rows ]
elif type(args[0] == str):
self.rows = [ map(T, row.strip().split()) for row in args[0].strip().split('\n') ]
else:
self.rows = [ map(T, row) for row in args[0] ]
@SeijiEmery
SeijiEmery / queue.cpp
Created October 28, 2017 20:54
Linked List Queue + Stack algorithms
#pragma once
template <typename T>
class Queue {
// Internal structure: singly linked list, with head + tail pointers
struct Node {
T value;
Node* next = nullptr;
@SeijiEmery
SeijiEmery / poisson.py
Last active October 18, 2017 21:41
Poisson distribution
# Author: Seiji Emery
# from https://en.wikipedia.org/wiki/Poisson_distribution
#
# Note: uses the obvious recurrence relation
# P(0) = exp(-lambda)
# P(k) = P(k - 1) * lambda / k, k > 0
# Which is equivalent to, but much clearer than using factorials imo.
from math import exp
//
// Memory + time benchmarking code: this hijacks (overloads) global new / delete
// to trace memory allocations (very simple: # of allocations / frees + # bytes
// allocated / freed), and adds a global variable that displays this stuff
// from its dtor (guaranteed to be called after main() but before program exits).
//
// It also adds basic time profiling (global ctor / dtor) using std::chrono.
//
// All of this can be achieved externally ofc using time + valgrind (*nix),
// and is perhaps preferable - but implementing these interally was an interesting
@SeijiEmery
SeijiEmery / load_obj.py
Created August 22, 2017 15:30
Obj-file loader (python)
#
# Scaffold, can be trivially extended to do useful things.
# Currently, just loads a file + prints # of tris, verts, normals, and uvs.
#
def load_obj (path):
class Parser:
def __init__ (self):
self.has_tris, self.has_verts, self.has_textures, self.has_normals = False, False, False, False
self.tris, self.verts, self.textures, self.normals = [], [], [], []