simplify

Annotated tests/shootout/chameneos.py

201:95ea3c071ed9
2007-02-22 paulb Added tentative ListComp, ListCompFor, ListCompIf and Sliceobj support. Changed pop-up element generation so that in cases where no information is available, the pop-up elements are not generated. Added a test of list comprehensions. Added a slice built-in type.
paulb@200 1
#-- The Computer Language Shootout
paulb@200 2
#-- http://shootout.alioth.debian.org/
paulb@200 3
#-- contributed by Tobias Polzin, translated from Mike Pall's Lua program
paulb@200 4
#-- modified by Josh Goldfoot to use ifs for the complement routine
paulb@200 5
paulb@200 6
import sys
paulb@200 7
paulb@200 8
N = int(sys.argv[1])
paulb@200 9
first = None
paulb@200 10
second = None
paulb@200 11
meetings = 0
paulb@200 12
paulb@200 13
RED, BLUE, YELLOW = xrange(1,4)
paulb@200 14
paulb@200 15
#-- Create a very social creature.
paulb@200 16
def creature(me):
paulb@200 17
    global N, first, second, meetings
paulb@200 18
    met = 0
paulb@200 19
    while 1:
paulb@200 20
        #-- Meet another creature.
paulb@200 21
paulb@200 22
        #-- Wait until meeting place clears.
paulb@200 23
        while second:
paulb@200 24
            yield None
paulb@200 25
paulb@200 26
        other = first
paulb@200 27
        if other:
paulb@200 28
            #-- Hey, I found a new friend!
paulb@200 29
            second = me
paulb@200 30
        else:
paulb@200 31
            # -- Sniff, nobody here (yet).
paulb@200 32
            if N <= 0:
paulb@200 33
                #-- Uh oh, the mall is closed.
paulb@200 34
                meetings += met
paulb@200 35
                yield None
paulb@200 36
paulb@200 37
                # The mall was closed, so everyone is faded.
paulb@200 38
                raise StopIteration
paulb@200 39
            N -= 1
paulb@200 40
            first = me
paulb@200 41
            while not second:
paulb@200 42
                yield None #-- Wait for another creature.
paulb@200 43
            other = second
paulb@200 44
paulb@200 45
            first = None
paulb@200 46
            second = None
paulb@200 47
            yield None
paulb@200 48
paulb@200 49
        # perform meeting
paulb@200 50
        met += 1
paulb@200 51
        if me != other:
paulb@200 52
            if me == BLUE:
paulb@200 53
                me = other == RED and YELLOW or RED
paulb@200 54
            elif me == RED:
paulb@200 55
                me = other == BLUE and YELLOW or BLUE
paulb@200 56
            elif me == YELLOW:
paulb@200 57
                me = other == BLUE and RED or BLUE
paulb@200 58
paulb@200 59
#-- Trivial round-robin scheduler.
paulb@200 60
def schedule(threads):
paulb@200 61
    global meetings
paulb@200 62
    try:
paulb@200 63
        while 1:
paulb@200 64
            for thread in threads:
paulb@200 65
                thread.next()
paulb@200 66
    except StopIteration:
paulb@200 67
        return meetings
paulb@200 68
paulb@200 69
def main():
paulb@200 70
    #-- A bunch of colorful creatures.
paulb@200 71
    threads = [
paulb@200 72
        creature(BLUE),
paulb@200 73
        creature(RED),
paulb@200 74
        creature(YELLOW),
paulb@200 75
        creature(BLUE) ]
paulb@200 76
paulb@200 77
    print schedule(threads)
paulb@200 78
paulb@200 79
main()