Pastebin

Paste #1228: neighbour

< previous paste - next paste>

Pasted by tdn

Download View as text

def neighbour(s):
    """Find a neighbour state"""
    (h, reservations) = s
    # Pick a random reservation
    idx = int(rand() * len(reservations))
    r = reservations[idx]
    rsize = len(r.seats)
    #print """We randomly selected reservation %s, with %s seats""" % (r.id, rsize)
    #print r.pp()

    # Pick random row with enough seats
    row = None
    while not row:
        crow = int(rand() * len(h.rows))
        if len(h.rows[crow].seats) >= rsize:
            row = h.rows[crow]
    #print """We will now put this reservation in row %s with size %s""" % (crow, len(h.rows[crow].seats))
    # Pick a random start seat in this row
    new_idx = int(rand() * (len(h.rows[crow].seats)-rsize))
    # Delete the old reservation
    for ts in r.seats:
        ts.sold -= 1
    r.seats = []
    for x in range(0, rsize):
        r.seats.append(row.seats[x+new_idx])
        row.seats[x+new_idx].sold += 1
    # Place the new reservation
    return (h, reservations)

New Paste


Do not write anything in this field if you're a human.

Go to most recent paste.