Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/python
# First some classes to implement graph theory. These aren't supposed to know
# anything about Physics.
class Node(object):
"""Nodes are told about the edges that exist as they are created, so
they know about each other. This means that a set of connected Nodes can be
referred to by any one of them; none are special."""
@basak
basak / conway.py
Created September 16, 2012 09:48
Elegant and minimalist implementation of Conway's Game of Life
#!/usr/bin/python3
# Conway's Game of Life
# Based on Jack Diederich's implementation
# http://pyvideo.org/video/880/stop-writing-classes
from itertools import chain, product
neighbour_deltas = set(list(product([-1, 0, 1], repeat=2))) - set([(0, 0)])
#!/usr/bin/python3
STARTING_SCORE = ('0', '0')
# key: previous score; value: new score if player 0 scores a point from that
POINT_LOOKUP = {
('0', '0'): ('15', '0'),
('0', '15'): ('15', '15'),
('0', '30'): ('15', '30'),
('0', '40'): ('15', '40'),
@basak
basak / gist:5929977
Last active December 19, 2015 08:59
def rental_car_discount(days):
if days < 3:
return 0
elif 3 <= days < 7:
return 20
elif days >= 7:
return 50
def rental_car_cost(days):
return (days * 40) - rental_car_discount(days)
@basak
basak / gist:7391134
Last active December 27, 2015 21:19
#!/usr/bin/python3
from random import choice
from itertools import product
from time import sleep
class GameOfLife(object):
def __init__(self, height, width):
self.size = (height, width)
@basak
basak / slp_switch.s
Created October 14, 2014 12:03
gcc 4.9.1 output
.align 2
.thumb
.thumb_func
.type slp_switch, %function
slp_switch:
.LFB133:
.file 3 "platform/switch_arm32_gcc.h"
.loc 3 54 0
.cfi_startproc
@ args = 0, pretend = 0, frame = 16
@basak
basak / slp_switch.s
Created October 14, 2014 12:29
gcc 4.9.1 output with -fno-omit-frame-pointer
.align 2
.thumb
.thumb_func
.type slp_switch, %function
slp_switch:
.LFB133:
.file 3 "platform/switch_arm32_gcc.h"
.loc 3 57 0
.cfi_startproc
@ args = 0, pretend = 0, frame = 16
@basak
basak / slp_switch.s
Created October 14, 2014 16:57
gcc 4.9.1 output with -fno-omit-frame-pointer and return value from inline asm fix
.align 2
.thumb
.thumb_func
.type slp_switch, %function
slp_switch:
.LFB133:
.file 3 "platform/switch_arm32_gcc.h"
.loc 3 57 0
.cfi_startproc
@ args = 0, pretend = 0, frame = 16
@basak
basak / recurring_events.py
Last active December 30, 2015 01:05
asyncio event and worker pattern thoughts
import asyncio
class Worker:
'''A Worker has background asyncio tasks that are cancelled on __del__'''
def __init__(self, loop=None):
self._loop = loop if loop else asyncio.get_event_loop()
# This set serves to keep incomplete tasks alive as long as this class
# instance exists.
self._tasks = set()
@basak
basak / lxd-ssh.sh
Created October 11, 2017 02:57
lxd-ssh: wrapper for using ssh/scp/etc with lxd
#!/bin/sh
set -e
# lxd-ssh
# Wrapper for using ssh/scp/etc with lxd
# Author: Robie Basak <robie.basak@canonical.com>
# Last-Update: 2017-10-11
# Instructions:
#