Skip to content

Instantly share code, notes, and snippets.

@globby
globby / OddEven.py
Created March 6, 2014 00:11
An implementation of the OddEven sorting algorithm
def oddeven(lst):
sorted_ = False
while not sorted_:
sorted_ = True
for i in range(1,len(lst)-1,2):
if a[i] > a[i+1]:
a[i], a[i+1] = a[i+1], a[i]
sorted_ = False
for i in range(0,len(lst)-1,2):
if a[i] > a[i+1]:
@globby
globby / Stoogesort.py
Created March 6, 2014 00:22
An implementation of the recursive Stoogesort algorithm
def stoogesort(lst):
def recurse(lst,i,l):
if lst[l] < lst[i]:
lst[i], lst[l] = lst[l], lst[i]
if (l - i + 1) >= 3:
t = (l - i + 1) / 3
recurse(lst, i, l-t)
recurse(lst, i+t, l)
recurse(lst, i, l-t)
recurse(lst,0,len(lst)-1)
@globby
globby / Swap.py
Created March 6, 2014 20:18
The in-place xor swap algorithm
def swap(lst,a,b):
lst[a] = lst[a] ^ lst[b]
lst[b] = lst[a] ^ lst[b]
lst[a] = lst[a] ^ lst[b]
@globby
globby / Infix.py
Created March 7, 2014 07:20
An infix evaluator implementing the shunting yard algorithm
import re
from operator import *
class Math(Exception):
pass
class InfixException(Math):
pass
re_float = re.compile(r'^(\d+\.\d*)|(\d*\.\d+)$')
re_int = re.compile(r'^\d+$')
class Infix:
def __init__(self):
@globby
globby / Clipboard.py
Created March 8, 2014 02:59
A simple clipboard modification class with ctypes
from ctypes import *
class Clipboard:
def __init__(self):
self.strcpy = cdll.msvcrt.strcpy
self.OpenClipboard = windll.user32.OpenClipboard
self.CloseClipboard = windll.user32.CloseClipboard
self.EmptyClipboard = windll.user32.EmptyClipboard
self.GetClipboardData = windll.user32.GetClipboardData
self.SetClipboardData = windll.user32.SetClipboardData
self.GlobalAlloc = windll.kernel32.GlobalAlloc
@globby
globby / Screencap.py
Created March 8, 2014 04:06
A simple(ish) screencap program with ctypes and PIL
from ctypes import *
from struct import calcsize, pack
from PIL import Image
CreateDC = windll.gdi32.CreateDCA
CreateCompatibleDC = windll.gdi32.CreateCompatibleDC
GetDeviceCaps = windll.gdi32.GetDeviceCaps
CreateCompatibleBitmap = windll.gdi32.CreateCompatibleBitmap
BitBlt = windll.gdi32.BitBlt
SelectObject = windll.gdi32.SelectObject
@globby
globby / goto.py
Last active August 29, 2015 14:06
A little goto implementation for python
import sys, re
globals_ = globals()
def setglobals(g):
global globals_
globals_ = g
def goto(l):
global globals_
with open(sys.argv[0], "rb") as f:
data = f.read()
data_ = data.split('\n')