From f0fdd4b91cd4dded9e029a7a3a8b6a158d721a30 Mon Sep 17 00:00:00 2001 From: bnewbold Date: Thu, 22 Nov 2012 00:21:11 +0100 Subject: add Nov 22nd notes and scripts --- test_20121121/vonneumann.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100755 test_20121121/vonneumann.py (limited to 'test_20121121/vonneumann.py') diff --git a/test_20121121/vonneumann.py b/test_20121121/vonneumann.py new file mode 100755 index 0000000..3e71e9d --- /dev/null +++ b/test_20121121/vonneumann.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python + +import sys + +def pairs(fin): + for c in fin.read(): + bits = bin(ord(c))[2:].zfill(8) + while len(bits) >= 2: + yield bits[0:2] + bits = bits[2:] + +def main(fin, fout): + + bits = "" + + for p in pairs(fin): + if p == "11" or p == "00": + continue + bits += p[0] + if len(bits) == 8: + fout.write(chr(int(bits, 2))) + bits = "" + + +if __name__=="__main__": + if len(sys.argv) == 1: + fin = sys.stdin + fout = sys.stdout + elif len(sys.argv) == 3: + fin = open(sys.argv[1], 'r') + fout = open(sys.argv[2], 'w') + print "working..." + else: + print "ERROR: wrong number of arguments (in_file + out_file OR none)" + + main(fin, fout) + fin.close() + fout.close() -- cgit v1.2.3