Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Challenge

Name: Anonymous 2009-06-16 10:40

Write an encryption and decryption program which would
 Read file A and use its contents as bitstring which would
be input for this transformation(Its XOR btw), assuming the initial "previous bit" as 0:
If previous bit is equal to next bit write 0 into File B, else write 1 into File B.
The decryption program must take File B and produce File A.

Whoever does it in least characters wins.

Name: Anonymous 2009-06-16 12:13

Don't start all your homework threads with "Challenge".

Anyway, encoder:

import sys,array
p=0;f=array.array('B',sys.stdin.read())
for i in xrange(len(f)*8):
 if f[i/8]>>(i&7)&1==p:f[i/8]&=~(1<<(i&7))
 else:f[i/8]|=1<<(i&7);p=p+1&1
f.tofile(sys.stdout)


Decoder:

import sys,array
p=0;f=array.array('B',sys.stdin.read())
for i in xrange(len(f)*8):
 if f[i/8]>>(i&7)&1:p=~p
 if p:f[i/8]|=1<<(i&7)
 else:f[i/8]&=~(1<<(i&7))
f.tofile(sys.stdout)


Both are exactly 178 bytes, which makes me feel kind of good about it :). It's not a winning entry by any means, but I'd appreciate if some Guido's bitch could show me how to do this better (as in smaller), as I'm pretty new to this language.

xrange -> range doesn't count

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List