Skip to content

Instantly share code, notes, and snippets.

@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')
@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 / 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 / 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 / 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 / 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 / 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 / Gnomesort.py
Created March 6, 2014 00:04
An implementation of the Gnomesort algorithm
def gnomesort(lst):
pos = 1
while pos < len(lst):
if lst[pos] >= lst[pos-1]:
pos += 1
else:
lst[pos], lst[pos-1] = lst[pos-1], lst[pos]
if pos > 1:
pos -= 1
@globby
globby / Combsort.py
Created March 6, 2014 00:00
An implementation of the Combsort algorithm
def combsort(lst):
gap = len(lst)
shrink = 1.3
swapped = True
while not (gap == 1 and not swapped):
gap = int(gap/shrink)
if gap < 1:
gap = 1
i = 0
swapped = False
@globby
globby / Cocktail.py
Created March 5, 2014 23:51
An implementation of the Cocktail Sort algorithm
def Cocktail(lst):
swapped = True
while swapped:
swapped = False
for i in range(0,len(lst)-2):
if lst[i] > lst[i+1]:
lst[i], lst[i+1] = lst[i+1], lst[i]
swapped = True
if not swapped:
break