simultaneous assignment

Serge Orlov Serge.Orlov at gmail.com
Tue May 2 16:37:35 EDT 2006


John Salerno wrote:
> bruno at modulix wrote:
>
> > Now if I may ask: what is your actual problem ?
>
> Ok, since you're so curious. :)
>
> Here's a scan of the page from the puzzle book:
> http://johnjsalerno.com/spies.png
>
> Basically I'm reading this book to give me little things to try out in
> Python. There's no guarantee that this puzzle is even conducive to (or
> worthy of) a programming solution.

So what you're trying to do is to run over all possible combinations?
Anyway you don't need to worry about identity, since boolean values are
immutable. In general when you see statement like

some_var = immutable value

you can be *sure* you're changing *only* some_var

Warning! Half-spoiler below :) Following is a function run_over_space
from my personal utils package for generating all combinations and an
example how it can be applied to your puzzle:

def decrement(point, space):
    """ Yield next point of iteration space """
    for coord in range(len(point)):
        if point[coord] > 0:
            point[coord] -= 1
            return
        else:
            point[coord] = space[coord]
            continue
    raise StopIteration

def run_over_space(space):
    """ Yield all points of iteration space.
    Space is a list of maximum values of each dimension"""
    point = space[:]
    while True:
        yield point
        decrement(point,space)

def describe_point(spy,w,x,y,z):
    if spy:
        print "Spy1 is right, ",
    else:
        print "Spy1 is wrong, ",
    print "w, x, y, z = ", w, x, y, z

for point in run_over_space([1,1,1,1,1]):
    describe_point(*point)




More information about the Python-list mailing list