UserPreferences

TedsCoalescenceCode


  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
import random

def make_list(num):     # Make list of num random integers betw 0 and 1
    x = []
    for i in range(num):
        x.append(random.randint(0,10))
    return x

def updownsame(num):    # Increase or decrease num by 1, or leave same
    if random.random() < 0.333333333:
        num += 1
    elif random.random() < 0.5:
        num -= 1
    if num > 10:        # Keep num in reasonable range
        num = 10
    if num < 0:
        num = 0
    return num

def delete_double(list, address):   # if list element is same as next, delete it
    if list[address] == list[address + 1]:
        del list[address]

# *********  MAIN PROGRAM STARTS HERE   *********

go_again = 1
while go_again:
    length = input ("Enter initial list length.  ")
    the_list = make_list(length)
    print "initial list:", the_list
    time = 0
    while len(the_list) > 1:        # each iteration of this while loop is a timestep
        for i in range(len(the_list)):    # up, down, same
            the_list[i] = updownsame(the_list[i])

        i = 0                             # get rid of doubles
        while i < (len(the_list) - 1):
            delete_double(the_list, i)
            i += 1
        print the_list

        time += 1

    print "Number of timesteps:", time
    go_again = (raw_input("Enter 'y' to go again. ") == 'y')

print
print "Good-bye."