Skip to content

Instantly share code, notes, and snippets.

@mpickering
Created September 4, 2012 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpickering/3627034 to your computer and use it in GitHub Desktop.
Save mpickering/3627034 to your computer and use it in GitHub Desktop.
fountain
#fountin
import random, math, itertools
def randomN(n):
while True:
yield random.randint(1,0.5*n*(n+1))
def stream(w):
rand = randomN(len(w))
while True:
yield (len(w) - math.ceil((-1+math.sqrt(1+(8*rand.next())))*0.5))+1
def update(e):
for d in e[1]:
if processed[d]:
e[0] ^= processed[d]
e[1].remove(d)
if len(e[1]) == 1:
processed[e[1][0]] = e[0]
hold.remove(e)
for d in hold:
if e[1][0] in d[1]:
update(d)
src = 'ashok thinks he is big man because he don't like the python'
ords = [ord(x) for x in src]
source = stream(src)
for x in range(1000):
hold = []
processed = [None] * len(src)
while not all(processed):
i+=1
selection = random.sample(range(len(src)),int(source.next()))
payload = [reduce(lambda x,y: x ^ y, [ords[i] for i in selection])] + selection
payload = [payload[0],payload[1:]]
hold.append(payload)
update(payload)
print [chr(x) for x in processed]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment