Skip to content

Instantly share code, notes, and snippets.

@dataclass
class Curried:
f: Callable
def __call__(self, *args, **kwargs):
f = partial(self.f, *args, **kwargs)
try:
signature(f).bind()
except TypeError:
return type(self)(f)
else:
@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):
import threading
from time import perf_counter as time
import random
from statistics import median, stdev
results = {'thread': [], 'call': []}
end = 0
def inside():
global end
@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 / py2md.py
Last active February 1, 2017 20:12 — forked from AtHeartEngineer/py2md.py
#!/usr/bin/env python
# coding=utf-8
# <a class="btn btn-default pull-right" href="https://gist.github.com/TylerShaw/48ead56c19ce905ac513"><i class="fa fa-git"></i> Download the gist here!</a>
# Py2Md started as a little project to do the magical "self documenting code". After thinking about it, I realized self documenting code is great, but it's really not the point.
# Commenting code properly, if only better, is the point.
# This script evolved from me wanting to code better. I often look at other peoples code, or even old code I've written, and it takes me a few minutes to even figure out what each section is doing.
# This will hopefully solve that, not only by forcing me to comment code better, but to publish my code with decent comments.
# This script reads in a python file (either itself, or anything it's
# imported into) and converts the python file into a markdown file. It
@gvx
gvx / gist:5696885
Created June 3, 2013 08:35
Extract page numbers that haven't been included in index.txt yet
last = None
with open('index.txt') as f:
for line in f:
new = int(line.decode('utf-8').split('\t', 1)[0])
if last is not None:
if last - new > 1:
if last - new == 2:
print new + 1
else:
@gvx
gvx / testveri.py
Last active December 11, 2015 17:49
An experiment with metaclasses, with dynamic type verification
from veri import Verified
class Something(metaclass=Verified):
# a single type
some_int = int
some_str = str
# a 'struct' using tuples
some_tuple = (int, int, str)
# a list of integers
@gvx
gvx / aliasdict.py
Created June 14, 2011 17:48
Path to Philosophy code
class Alias(object):
def __init__(self, initial):
self._set = {initial}
self.initial = initial
def add(self, alias):
self._set.add(alias)
def merge(self, other):
self._set.update(other._set)
def __iter__(self):
return iter(self._set)
@gvx
gvx / gist:3954076
Created October 25, 2012 17:08
Daily Programmer #107 difficult
import sys
vowels = set('aeiouy')
consonants = set('bcdfghjklmnpqrstvwxz')
impossible_digraphs = ['aa', 'bg', 'bh', 'bk', 'bq', 'bw', 'bx', 'bz', 'cb', 'cf', 'cg', 'cj', 'cm', 'cp', 'cv', 'cw', 'cx', 'cz', 'dx', 'dz', 'fc', 'fd', 'fg', 'fj', 'fk', 'fn', 'fp', 'fq', 'fv', 'fw', 'fx', 'fz', 'gc', 'gj', 'gk', 'gq', 'gv', 'gx', 'gz', 'hj', 'hk', 'hq', 'hv', 'hx', 'hz', 'ij', 'iy', 'jb', 'jc', 'jd', 'jf', 'jg', 'jh', 'jj', 'jk', 'jl', 'jm', 'jn', 'jp', 'jq', 'jr', 'js', 'jt', 'jv', 'jw', 'jx', 'jy', 'jz', 'kq', 'kv', 'kx', 'kz', 'lj', 'lx', 'md', 'mj', 'mk', 'mq', 'mx', 'mz', 'pg', 'pj', 'pq', 'pv', 'px', 'pz', 'qa', 'qb', 'qc', 'qd', 'qe', 'qf', 'qg', 'qh', 'qi', 'qj', 'qk', 'ql', 'qm', 'qn', 'qo', 'qp', 'qq', 'qr', 'qs', 'qt', 'qv', 'qw', 'qx', 'qy', 'qz', 'rx', 'sx', 'sz', 'tq', 'tx', 'uj', 'uq', 'uw', 'uy', 'vb', 'vc', 'vd', 'vf', 'vg', 'vh', 'vj', 'vk', 'vl', 'vm', 'vn', 'vp', 'vq', 'vr', 'vs', 'vt', 'vv', 'vw', 'vx', 'vz', 'wj', 'wk', 'wp', 'wq', 'wv', 'ww', 'wx', 'wz', 'xb', 'xf', 'xg', 'xj', 'xk', 'xm',
@gvx
gvx / units.l
Created October 15, 2012 17:56
Thingy to learn how to use Flex and Bison
%{
#include "units.tab.c"
%}
%option noyywrap
int -?[0-9]+
%%
[ \t] /*empty*/