Skip to content

Instantly share code, notes, and snippets.

@pyrocat101
pyrocat101 / verbalArithmetic.pl
Created March 10, 2014 22:35
Generalized Verbal Arithmetic in Prolog.
pad(W1, W2, X, Y) :-
length(W1, L1),
length(W2, L2),
padZero(W1, W2, L1, L2, X, Y).
padZero(W1, W2, L, L, [0|W1], [0|W2]).
padZero(W1, W2, L1, L2, X, Y) :- L1 #> L2, pad(W1, [0|W2], X, Y).
padZero(W1, W2, L1, L2, X, Y) :- L1 #< L2, padZero(W2, W1, L2, L1, Y, X).
stripZero([], []).
@pyrocat101
pyrocat101 / buildwords.sh
Last active May 18, 2018 08:48
CS35L - Homework 2
# get content in <td>...</td>
sed -n 's/<td>\(.*\)<\/td>/\1/gip' | \
# remove empty lines
grep -v "^\s*$" | \
# remove odd lines
sed -n '1~2!p' | \
# remove <u> tags
sed 's/<u>\(.*\)<\/u>/\1/gi' | \
# convert apostrophe (` -> ')
sed "s/\`/'/g" | \
@pyrocat101
pyrocat101 / parse-regex.clj
Last active August 29, 2015 14:00
Regex Parser
(defn syntax-error []
(throw (ex-info "Syntax Error" {})))
(declare alt then star prime)
(defn expect [s c]
(if (not= (first s) c)
(syntax-error)
(rest s)))
@pyrocat101
pyrocat101 / regex-parser.py
Created April 23, 2014 07:30
Regex Parser (Python version)
from collections import deque
class SyntaxError(Exception):
pass
class RegexParser(object):
"""
Alt ::= Concat ('|' Alt)*
| Concat
Concat ::= Star Star*
Simplify[RSolve[{a[n] == (B + 1) a[n - 1] - a[n - 2], a[0] == 0, a[1] == B}, a[n], n]]
Block[{B = 2}, Simplify[Table[a[n] /. First[%], {n, 10}]]]
type maybe = True of string
| Maybe
| False
type regex = Alt of regex * regex * maybe array
| Then of regex * regex * maybe array
| Star of regex * maybe array
| Char of char
let make_memo () = Array.make 101 Maybe
@pyrocat101
pyrocat101 / poly
Created August 19, 2014 21:58
HITCON CTF2014 - polyglot (Python2/Python3/C/Ruby/Haskell polyglot to `cat flag`)
x = {-
#if 0
0 + """".to_i => 0}
print `cat flag`
__END__
""".find('x')}
import os
p = os.system;{
#endif
1};
import select
import socket
import pycares
class CAresError(socket.error):
def __init__(self, errno, strerror):
super(CAresError, self).__init__(errno, strerror)
@pyrocat101
pyrocat101 / hashtbl.py
Created October 8, 2014 00:37
Naïve implementation of chained hash table, just for proof of concept.
class Bucket(object):
def __init__(self):
self.next = None
class Entry(object):
def __init__(self, key, val):
self.key = key
self.val = val
self.next = None
@pyrocat101
pyrocat101 / evaluator.py
Created November 7, 2014 09:25
Expression evaluator
class Lexer(object):
def __init__(self, s):
self.s = s
self.idx = 0
self.token = None
def peek(self):
if self.idx >= len(self.s):
return None
else: