Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Brandon Cho
- # April 23rd 2017
- # hw3pr1.py
- import time # provides time.sleep(0.5)
- from random import * # provides choice ( [0,1] ), etc.
- import sys # larger recursive stack
- sys.setrecursionlimit(100000) #100,000 deep
- from csplot import *
- def runGenerations( L ):
- """ runGenerations keeps running evolve...
- """
- print(L) # display the list, L
- time.sleep(0.5) # pause a bit
- newL = evolve( L ) # evolve L into newL
- runGenerations( newL ) # recurse
- def evolve( L ):
- """ evolve takes in a list of integers, L,
- and returns a new list of integers
- considered to be the "next generation"
- """
- N = len(L) # N now holds the size of the list :
- return [ setNewElement( L, i ) for i in range(N) ]
- def setNewElement( L, i, x=0 ):
- """ setNewElement returns the NEW list's ith element
- input L: any list of integers
- input i: the index of the new element to return
- input x: an exra, optional input for future use
- """
- return L[i] + 1
- # What's happening in the above example
- """
- runGenerations( [1,2,3] ) is being continuously ran by its recursion from 'runGenerations( newL ).
- Furthermore, the list all becomes one value higher because of setNewElement and how it is functioning with evolve.
- Evolve is taking the list registered in runGenerations and is returning the list through setNewElement
- causing the list to all increase by one and to be outputted as 'newL'.
- """
- # Q0
- def setNewElement( L, i, x=0 ):
- """ setNewElement returns the NEW list's ith element
- input L: any list of integers
- input i: th eindex of the new element to return
- input x: an extra, optional input for future use
- """
- return L[i]*2
- # Q1
- def setNewElement( L, i, x=0 ):
- """ setNewElement returns the NEW list's ith element
- input L: any list of integers
- input i: th eindex of the new element to return
- input x: an extra, optional input for future use
- """
- return L[i]**3
- # Q2
- def setNewElement( L, i, x=0 ):
- """ setNewElement returns the NEW list's ith element
- input L: any list of integers
- input i: the index of the new element to return
- input x: an extra, optional input for future use
- """
- return L[i-1]
- # Q3
- def setNewElement( L, i, x=0 ):
- """ setNewElement returns the NEW list's ith element
- input L: any list of integers
- input i: the index of the new element to return
- input x: an extra, optional input for future use
- """
- if i == len(L)-1:
- return L[0]
- else:
- return L[i+1]
- # Q4
- def setNewElement( L, i, x=0 ):
- """ setNewElement returns the NEW list's ith element
- input L: any list of integers
- input i: the index of the new element to return
- input x: an extra, optional input for future use
- """
- return choice( [0,1] )
- # Detecting when we've reached our goal
- def allOnes(L):
- for x in L: # for every element in L is defined as x
- if x is not 1:
- return False
- return True
- # Improving runGenerations
- this function below
- def runGenerations( L ):
- """ runGenerations keeps running evolve...
- """
- count = 0
- print(L) # display the list, L
- time.sleep(0.5) # pause a bit
- count = count + 1
- if allOnes(L) == True:
- return print(count) + exit()
- newL = evolve( L ) # evolve L into newL
- runGenerations( newL ) # recurse
- if allOnes(L) == False:
- return runGenerations(L)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement