Skip to content

Instantly share code, notes, and snippets.

@fbwright
fbwright / compiler.py
Created May 30, 2015 15:35
Recursive descent parser and interpreter/(Python|Forth) compiler
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function, division
import sys, re
if sys.version_info.major < 3:
input = raw_input
#I wonder... how difficult would be writing a parser that takes well-formed
#BNF in input and outputs a recursive descent parser based on that grammar?
#I'd have to recognize, name and put into an enum tokens ('...'), create a
#dictionary of identifiers (<...>), and each identifier will be assigned
@fbwright
fbwright / pcalc.nim
Created May 30, 2015 15:42
Calculator in Nimrod with RDP
#pCalc - Nimrod version7
import strutils, math, tables, logging
type
EProgramError = object of E_Base
EStackError = object of EProgramError
EStackUnderflowError = object of EStackError
ESyntaxError = object of EProgramError
ERuntimeError = object of EProgramError
EDivisionByZeroError = object of ERuntimeError
TSymbol = enum
@fbwright
fbwright / cod_fis.py
Last active August 29, 2015 14:22
Calcolo del codice fiscale
N,L="0123456789","ABCDEFGHIJKLMNOPQRSTUVWXYZ";A=N+L;D="! %')-/135! %')-/135\"$24+#&(,.0*6987";P=" !\"#$%&'() !\"#$%&'()*+,-./0123456789";p=lambda l,i:l.split("\t")[i].strip()
def G(s,f=0):
C,V="",""
for c in s.upper():
if c in "AEIOU":V+=c
else:C+=c
C=[C,C[0]+C[2:]][f and len(C)>3];return(C+V+"X"*3)[:3]
C=lambda n,c,s,g,m,a,C:(lambda c:c+L[sum(ord(([D,P][i%2])[A.index(x)])-32 for i,x in enumerate(c))%26])(G(c)+G(n,1)+str(a)[-2:]+"ABCDEHLMPRST"[m-1]+"%02d"%(g+40*s)+{p(l,1):p(l,0)for l in open("belfiore.txt").read().splitlines()}[C.upper()])
@fbwright
fbwright / backpack.py
Created June 23, 2015 11:55
[2015-06-17] Challenge #219 [Hard] The Cave of Prosperity
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#An attempt at finding a solution to the challenge #219 of /r/DailyProgrammer
#(http://www.reddit.com/r/dailyprogrammer/comments/3aewlg/)
#I am using a genetic algorithm to find an (usually approximate) solution.
from __future__ import print_function, division
import genetic_algorithm as ga
import sys, random, argparse
if sys.version_info.major < 3:
input = raw_input