Skip to content

Instantly share code, notes, and snippets.

View darius's full-sized avatar

Darius Bacon darius

View GitHub Profile
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<meta http-equiv="encoding" content="utf-8">
</head>
<canvas id="canvas" width="1490" height="512" style="border:1px dashed">
CA canvas
</canvas>
<script type="text/javascript">
var canvas = document.getElementById("canvas");

Some coding exercises, working up to matching regular expressions with Thompson's algorithm.

I'm assuming you know what a regular expression is and you're a fairly experienced programmer. I don't know if these exercises are either fun or effective -- please comment or fork here or email withal@gmail.com to help me improve them.

The problems

# My solution to a problem in https://github.com/darius/regexercise
def search(strings, chars):
"""Given a sequence of strings and an iterator of chars, return True
if any of the strings would be a substring of ''.join(chars); but
only consume chars up to the end of the match."""
if not all(strings):
return True
tails = []
for ch in chars:
words = map(str.rstrip, open('sowpods.txt').read().splitlines())
pals = {}
for word in words:
pals.setdefault(''.join(sorted(word)), []).append(word)
print(pals[max((word for word, clique in pals.iteritems() if 1 < len(clique)), key=len)])
#. ['CONSTRUCTIONISM', 'MISCONSTRUCTION']
// Not a serious or proper implementation of promises.
// I haven't even bothered to look at the JS promises spec, and I
// know it's missing error handling at the least.
// Seriously, don't use this: it's just the most straightforward
// code to run the benchmark below, and nothing else.
var agenda = [];
function eventLoop() {
var front = 0;
#!/usr/bin/env python
"""
Given Python2 source files, print out all the string literals that look
like they were meant to be raw strings, but are ordinary strings instead.
Exit status: 1 if any warnings, else 0.
"""
import token, tokenize
def main(argv):
# (Python 2)
# Developed from an example by Dave Long, but don't blame him!
from dis import cmp_op, dis, hascompare, hasjabs, hasjrel, opmap, HAVE_ARGUMENT
from itertools import chain
import types
class Clutch(object):
def __init__(self, *args, **kwargs):
for arg in args: setattr(self, arg.__name__, arg)
"""
no-vowel words: one beat
silent: -e -ed -es[er, usually]
one beat:
ae
ai
au
ea
http://wry.me/hacking/dequebot/demo.html
OK, not sure what to do next. Things I have in mind:
- Values on the left as a stack
- Primitives parameterized by those values
- bf<>
- +-*/~ (~ is reciprocal)
- = (how do we say 'less than'?)
- Digits push numbers, and we need ',' or ' ' to separate them
def main():
board = Board()
with Terminal() as ui:
keys = ui.gen_key_events()
while True:
ui.render(board.view())
if board.ended():
break
for k in keys:
if k == '^D':