Skip to content

Instantly share code, notes, and snippets.

grantslatton

Block or report user

Report or block grantslatton

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@grantslatton
grantslatton / curry.py
Last active Aug 29, 2015
A decorator that adds currying a la Haskell to Python. This maintains more functionality and easy of use than functool's "partial" function. This is just "because we can". Probably not a good idea to use this in production code.
View curry.py
from inspect import getargspec
def curry(f, n=None):
defaults = []
if n == None:
s = getargspec(f)
n = len(s.args)
if s.defaults:
n -= len(s.defaults)
defaults = zip(reversed(s.args),reversed(s.defaults))
def newfunc(*args, **kwargs):
@grantslatton
grantslatton / hngen.py
Last active Sep 28, 2019
A program that uses Markov chains to generate probabilistic Hacker News titles.
View hngen.py
import urllib2
import re
import sys
from collections import defaultdict
from random import random
"""
PLEASE DO NOT RUN THIS QUOTED CODE FOR THE SAKE OF daemonology's SERVER, IT IS
NOT MY SERVER AND I FEEL BAD FOR ABUSING IT. JUST GET THE RESULTS OF THE
CRAWL HERE: http://pastebin.com/raw.php?i=nqpsnTtW AND SAVE THEM TO "archive.txt"
@grantslatton
grantslatton / asofterworld.sh
Created Nov 1, 2013
A script that polls asofterworld, downloads a random comic, and sets your Ubuntu background to it. Run it as a crontab to get it automated every N time units.
View asofterworld.sh
#!/bin/bash
sessionfile=`find "${HOME}/.dbus/session-bus/" -type f`
export `grep "DBUS_SESSION_BUS_ADDRESS" "${sessionfile}" | sed '/^#/d'`
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if [ ! -d "$DIR/comics/" ]; then
mkdir "$DIR/comics/"
fi
archive="http://www.asofterworld.com/archive.php"
@grantslatton
grantslatton / fizzbuzz.c
Last active May 13, 2019
FizzBuzz solved using only bit twiddling. It essentially uses two deterministic finite automata for divisibility testing.
View fizzbuzz.c
#include <stdio.h>
int f0(unsigned int x) { return x? (x&(1<<31)? f1(x<<1) : f0(x<<1)) : 1; }
int f1(unsigned int x) { return x? (x&(1<<31)? f3(x<<1) : f2(x<<1)) : 0; }
int f2(unsigned int x) { return x? (x&(1<<31)? f0(x<<1) : f4(x<<1)) : 0; }
int f3(unsigned int x) { return x? (x&(1<<31)? f2(x<<1) : f1(x<<1)) : 0; }
int f4(unsigned int x) { return x? (x&(1<<31)? f4(x<<1) : f3(x<<1)) : 0; }
int t0(unsigned int x) { return x? (x&(1<<31)? t1(x<<1) : t0(x<<1)) : 1; }
int t1(unsigned int x) { return x? (x&(1<<31)? t0(x<<1) : t2(x<<1)) : 0; }
int t2(unsigned int x) { return x? (x&(1<<31)? t2(x<<1) : t1(x<<1)) : 0; }
@grantslatton
grantslatton / tictactoe.c
Created Oct 10, 2013
An explained version of the tic-tac-toe AI that plays perfectly. (http://grantslatton.com/posts/code/2013-10-09/obfuscated_tictactoe/)
View tictactoe.c
//Authored by Grant Slatton on 2013 October 9
//All code is released to the public domain under the terms of [http://unlicense.org/]
#include <stdio.h>
int decided_move; //variable the AI stores its thoughts in
int magic_square[]={8,1,6,3,5,7,4,9,2}; //flattened magic square 816
// 357
// 492
//
View whitespaced_tictactoe.c
//Authored by Grant Slatton on 2013 October 9
//All code is released to the public domain under the terms of [http://unlicense.org/]
#include <stdio.h>
int i,j,k,z,a[12]={0},d[]={8,1,6,3,5,7,4,9,2};
int *b=a+6;
int w(int *c){
for(i=0;i<c[5];i++)
View obfuscated_tictactoe.c
//Authored by Grant Slatton on 2013 October 9
//All code is released to the public domain under the terms of [http://unlicense.org/]
#include <stdio.h>
int i,j,k,z,a[12]={0},d[]={8,1,6,3,5,7,4,9,2};int *b=a+6;int w(int *c){for(i=0;i<c[5];i++)for(j=i+1;j<c[5];j++)for(k=j+1;k<c[5];k++)if((c[i]+c[j]+c[k]==0xf)*c[i]*c[j]*c[k])return 1;return 0;}int g(){return (a[5]+b[5]==9)+w(a)+w(b);}int m(int x,int *c){d[x]=0*(c[c[5]++]=d[x]);}int n(int x,int *c){c[c[5]]=0*(d[x]=c[--c[5]]);}int s(int x){if(g())return x*w(a)-x*w(b);int p,q,u=-9;for(q=0;q<9;q++)if(d[q]){m(q,a+3*(1-x));i=-s(-x);n(q,a+3*(1-x));if(i>u){u=i;p=q;}}z=p;return u;}int main(){while(!g()){scanf("%d%d",&i,&j);if(!d[i*3+j])continue;m(i*3+j,a);if(g())break;;s(-1);m(z,b);printf("%d %d\n",z/3,z%3);}}
@grantslatton
grantslatton / sudoku.py
Last active Dec 25, 2015
Gets input from user on sudoku board, then prints the solution (http://grantslatton.com/posts/code/2013-08-16/sudoku_solver/)
View sudoku.py
#Authored by Grant Slatton on 2013 August 16
#All code is released to the public domain under the terms of [http://unlicense.org/]
#This function checks to make sure the columns and rows
#Only have one of each digit
def check_lines(board):
for x in xrange(9):
current_row_set = set() #We check the rows and
current_col_set = set() #columns simultaneously
for y in xrange(9):
View infinite_precision.cpp
//Authored by Grant Slatton on 2013 August 20
//All code is released to the public domain under the terms of [http://unlicense.org/]
#include <list>
#include <iostream>
#include <string>
#include <stdint.h>
#include <sstream>
#include <cstdlib>
#include <cmath>
@grantslatton
grantslatton / lcs.py
Last active Sep 28, 2019
Longest Common String Subsequence implementation in Python (http://grantslatton.com/posts/blog/2013-08-25/LCS_FAQ/)
View lcs.py
#Authored by Grant Slatton on 2013 August 25
#All code is released to the public domain under the terms of [http://unlicense.org/]
import sys
def backtrack_LCS(C, str1, str2, i, j):
if not i*j:
return ""
elif str1[i-1] == str2[j-1]:
return backtrack_LCS(C, str1, str2, i-1, j-1) + str1[i-1]
else:
You can’t perform that action at this time.