#!/usr/bin/env python #coding: utf-8 from Queue import Queue import random import sys import time def coroutine(func): def start(*args,**kwargs): cr = func(*args,**kwargs) cr.next() return cr return start @coroutine def individuo(id,infector=0): infected = 0 while 1: infector = (yield) if infector: if not infected: infectores[id] = infector #eventos.put((infector,id)) report.send((infector,id)) #print "%s: peguei de %s!"%(id, infector) else: break #~ while 1: for i in xrange(5): if susceptible.empty(): report.send((None,None)) break alvo = susceptible.get() try: alvo.send(id) except GeneratorExit: pass #~ time.sleep(.1) break @coroutine def rep(): while 1: iid,aid = (yield) if iid is None: break s = "%s|%s|A|"%(int(time.time()),iid) caminho = [str(aid),str(iid)] while 1: try: caminho += [str(infectores[iid])] iid = infectores[iid] except KeyError: break caminho.reverse() s += '/'.join(caminho) s +='/n' #print s sys.stdout.write(s) susceptible = Queue() #~ eventos = Queue() infectores = {} report = rep() pop = [individuo(i) for i in xrange(1,100)] random.shuffle(pop) [susceptible.put(i) for i in pop] seed = susceptible.get() seed.send(1) #report()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question