Skip to content

Instantly share code, notes, and snippets.

View DomNomNom's full-sized avatar

DomNomNom DomNomNom

View GitHub Profile
@DomNomNom
DomNomNom / SaneCode.cpp
Created August 28, 2014 02:58
Underhanded stuff in C++. No warnings from the compiler
// curtesy of https://github.com/DavidSaxon and http://www.parashift.com/c++-faq-lite/
#include <iostream>
// this function will be called by the base class
void baseFunction() {
std::cout << "Base Class!" << std::endl;
}
@DomNomNom
DomNomNom / rotationIdentity.cpp
Last active August 29, 2015 14:06
Rotation identity
// why does makeTransform1 give the same results as makeTransform2?
mat3 rotationMatrix(float angle, vec3 axis) {
... // returns a 3x3 rotation matrix for rotating about the given axis
}
mat3 makeTransform1(float alpha, float beta, float gamma) {
mat3 alphaTransform = rotationMatrix(alpha, vec3(0.0, 1.0, 0.0));
mat3 betaTransform = rotationMatrix(beta, vec3(1.0, 0.0, 0.0));
@DomNomNom
DomNomNom / periodicEvent.py
Created November 17, 2014 10:31
An example of having a function being called regularly while still processing user input
'''
An example of having a function being called regularly
while still processing user input
'''
import threading
# the regular event
timer = None
@DomNomNom
DomNomNom / MatrixSpline.py
Created December 5, 2014 04:41
Creating a spline with just a linear system
import numpy as np
import matplotlib.pyplot as plt
import scipy.sparse as sparse
import scipy.sparse.linalg
xs = [25.0, 50.0, 60.0]
ys = [ 1.0, -1.0, 0.3]
from bs4 import BeautifulSoup as bs
import urllib.request as request
import urllib.parse as parse
import pprint
# Selects sections of html that we are interested in
def prepare(html):
soup = bs(html, 'html.parser')
#!/usr/bin/python3 -i
# Hello there!
# The following line of code creates a mystery object which behaves in really weird ways.
# Have fun trying to figure it out.
# :Dom
#
# PS: This concept came from a Kiwi Pycon 2015 talk.
mystery = next(filter(lambda _:'.'in repr([_])and not(list!= type(_) or _),['%s'%{}]+__import__('gc').get_objects()))
@DomNomNom
DomNomNom / UnweightedDijkstras.py
Last active September 9, 2015 03:37
Searches a graph and returns the shortest (unweighted) path to the goal
# returns the shortest path (measured in the number of edges) from start-->goal.
# if no path exists None is returned
def unweightedDijkstra(graph, start, goal):
if goal is start: # special case
return (start,)
q = [(start,)] # q is a queue of paths (tuples of nodes)
visited = set()
while q:
@DomNomNom
DomNomNom / powerset.py
Last active April 5, 2016 21:48
Two ways of writing powerset which produce the same ordering. Have a go at proving it.
def powerset(list):
if not list:
yield set()
return
*rest, last = list
yield from powerset(rest)
for subset in powerset(rest):
yield subset | {last}
@DomNomNom
DomNomNom / circularAverage.py
Last active April 10, 2016 11:17
Programming puzzle: Average direction on a circle
def enforce0to360range(angle):
"""Converts the given angle (degrees) into the range [0 360).
>>> enforce0to360range(30)
30.0
>>> enforce0to360range(180)
180.0
>>> enforce0to360range(360)
0.0
>>> enforce0to360range(390.0)
@DomNomNom
DomNomNom / X.py
Last active April 28, 2016 01:37
"""
The main use case of this is to replace either
>>> [ x * (x + 1) for x in [1,2,3] ]
or
>>> map(lambda x: x * (x + 1), [1,2,3])
with
>>> map(X * (X + 1), [1,2,3]) #SWAG
You can naively think of it as 'lambda X:' without writing 'lambda X:'.