Skip to content

Instantly share code, notes, and snippets.

View almost's full-sized avatar

Thomas Parslow almost

View GitHub Profile
# Given a dictionary of words and scores and prefix return the top 10
# words in the dictionary that start with that prefix ordered by score
# (highest first)
# Builds a trie (a suffix tree) from the dictionary. The score
# ordering thing is my idea of how that could work, I haven't look at
# how others do it yet so it might be crazy :)
import random
# Another from Programming Pearls (again, my attempt before reading
# the rest of the chapter). Rotate a vector left in place by some
# amount without using extra memory proportional to the length of the
# input vector and while running in in O(n)
def rotate_left(vec, n):
i = 0
start = 0
letter = vec[i]
seen = 0
# Another from Programming Pearls (again, my attempt before reading
# the rest of the chapter). Given a dictionary and a word, find all
# anagrams of the word in the dictionary.
words = (x.strip().lower() for x in open("/usr/share/dict/words"))
lookup = {}
for word in words:
letters = "".join(sorted(list(word)))
lookup.setdefault(letters, []).append(word)
@almost
almost / word-parse.py
Last active November 22, 2015 23:07
Parse words from a string of smushed together words with a single regexp
# Solving the same problem as
# http://blogs.perl.org/users/ingy_dot_net/2015/11/perl-regular-expression-awesomeness.html
# but using a Trie instead of regexps, without backtracking and in
# Python
from itertools import groupby
phrase = "yougotmailmanners"
def makeTrie(words, prefix=""):
node = {}
<!DOCTYPE html>
<html lang="en">
<head>
<link
href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css"
rel="stylesheet">
</head>
<body>
<div class="container">
<div class="masthead">
{% if tweets %}
<table class="table">
<thead>
<tr>
<th>User</th>
<th>Tweet</th>
<th>When?</th>
<th>View on Twitter</th>
</tr>
</thead>
@almost
almost / concurrentSQSDelete.js
Last active December 22, 2015 11:40
Example of using through2-concurrent with sqs-readable-stream
var AWS = require('aws-sdk');
var SQSReadableStream = require('sqs-readable-stream');
var through2Concurrent = require('through2-concurrent');
var sqs = new AWS.SQS({
apiVersion: '2012-11-05',
region: 'us-east-1',
accessKeyId: 'YOUR AMAZON ACCESS KEY'
});
var sqsStream = new SQSReadableStream({
(defun tom-show-agenda (prefix)
(interactive "P")
(org-agenda-list)
(delete-other-windows)
(unless prefix
(calendar)
(other-window 1)
(split-window-vertically)
(other-window 1)
(find-daypage)))
@almost
almost / no-semis.js
Created November 25, 2015 13:46
Lack of semi-colons let line 2 and 3 combine. Runtime error :(
export function pagebounds2screen({x, y, width, height}) {
let ratio = window.devicePixelRatio
[x,y] = page2screen(x, y)
width = width * ratio
height = height * ratio
return {x, y, width, height}
}
@almost
almost / gist:8756247
Last active April 4, 2016 01:55
Regular Expression Puzzle Solver Output
$ ghc recross.hs && ./recross
[2 of 2] Compiling Main ( recross.hs, recross.o )
Linking recross ...
Iteration 0
. . . . . . .
. . . . . . . .
. . . . . . . . .
. . . . . . . . . .
. . . . . . . . . . .