Skip to content

Instantly share code, notes, and snippets.

View gvx's full-sized avatar

Jasmijn Wellner gvx

  • None
  • The Netherlands
View GitHub Profile
@gvx
gvx / mastermind.py
Created May 16, 2011 20:20
An implementation of Knuth's five-guess algorithm to solve a mastermind code [CC0]
from itertools import product
def score(self, other):
first = len([speg for speg, opeg in zip(self, other) if speg == opeg])
return first, sum([min(self.count(j), other.count(j)) for j in 'ABCDEF']) - first
possible = [''.join(p) for p in product('ABCDEF', repeat=4)]
results = [(right, wrong) for right in range(5) for wrong in range(5 - right) if not (right == 3 and wrong == 1)]
def solve(scorefun):
@gvx
gvx / heuristic-fluff.py
Created March 26, 2011 11:30
Tic Tac Toe minimax implementation. Import-friendly and easy to extend. heuristic-fluff.py is a more elaborate heuristic. See my blog for why it isn't the default.
from tictactoe import *
class FluffHeuristic(Computer):
a = [0, 2, 8, 6]
b = [1, 5, 7, 3]
def play(self, board):
if not self.other.name:
for i in range(9):
x = board.get(i)
if x and x != self:
@gvx
gvx / test.dj
Created March 6, 2011 22:37
DejaVu test for parsing tree
func some function:
a test
catch handler:
some mistake
if this is wrong:
tell me why
elseif this should not fail:
hello
if hi:
bye
@gvx
gvx / doc.py
Created March 5, 2011 23:03
A simple test of annotations, generating extended docstrings
def doc(f):
newdoc = f.__doc__ and [f.__doc__, ''] or []
c = f.__code__
ann = f.__annotations__
max_i = c.co_argcount + c.co_kwonlyargcount
for arg_index in range(max_i):
name = c.co_varnames[arg_index]
if name in ann:
newdoc.append(name + ': ' + str(ann[name]))
else:
#By Robin Wellner (gvx)
#I hereby waive copyright and related or neighboring rights to this work
#See the Creative Commons Zero Waiver at <http://creativecommons.org/publicdomain/zero/1.0/>
#a simple way to create one object without having to maintain it's class
#only works in Python 3.x
@apply
class oneObject:
x = 6
@gvx
gvx / shebang.py
Created June 28, 2009 13:04
A Python script to enable #! for Windows
#By Robin Wellner (gvx)
#I hereby waive copyright and related or neighboring rights to this work
#See the Creative Commons Zero Waiver at <http://creativecommons.org/publicdomain/zero/1.0/>
import sys
import subprocess
f = open (sys.argv[1], "r")
r = f.readline()[:-1]
f.close()
@gvx
gvx / patch.py
Created May 3, 2009 21:53
A simple patch script in Python
#By Robin Wellner (gvx)
#I hereby waive copyright and related or neighboring rights to this work
#See the Creative Commons Zero Waiver at <http://creativecommons.org/publicdomain/zero/1.0/>
import sys
diff_file = sys.stdin.read().split('\n')
if len(sys.argv) > 1:
filename = sys.argv[1]
else:
@gvx
gvx / defdef.lua
Created February 16, 2009 13:59
default definitions for Lua
-- defdef.lua
-- default definitions for lua
-- By Robin Wellner (gvx)
-- I hereby waive copyright and related or neighboring rights to this work
-- See the Creative Commons Zero Waiver at <http://creativecommons.org/publicdomain/zero/1.0/>
-- Evaluate truth like in Python
function is_true (arg)
if arg == '' or arg == 0 then
@gvx
gvx / listtools.py
Created January 30, 2009 16:44
Functions for lists
#Listtools 0.1
#By Robin Wellner (gvx)
#Functions for lists
#I hereby waive copyright and related or neighboring rights to this work
#See the Creative Commons Zero Waiver at <http://creativecommons.org/publicdomain/zero/1.0/>
def append(lst, object):
"listtools.append(lst, object) -- append object to end"
lst.append(object)
@gvx
gvx / namespace.py
Created January 21, 2009 17:52
Simple module simulation
#namespace
#By Robin Wellner (gvx)
#I hereby waive copyright and related or neighboring rights to this work
#See the Creative Commons Zero Waiver at <http://creativecommons.org/publicdomain/zero/1.0/>
#Simulation of modules containing one or more function/class/object.
#Warning: modules are named differently than what you would expect.
def namespace(name):
class namespace: