Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. # Brandon Cho
  2. # April 23rd 2017
  3. # hw3pr1.py
  4.  
  5. import time # provides time.sleep(0.5)
  6. from random import * # provides choice ( [0,1] ), etc.
  7. import sys # larger recursive stack
  8. sys.setrecursionlimit(100000) #100,000 deep
  9. from csplot import *
  10.  
  11. def runGenerations( L ):
  12. """ runGenerations keeps running evolve...
  13. """
  14. print(L) # display the list, L
  15. time.sleep(0.5) # pause a bit
  16. newL = evolve( L ) # evolve L into newL
  17. runGenerations( newL ) # recurse
  18.  
  19.  
  20. def evolve( L ):
  21. """ evolve takes in a list of integers, L,
  22. and returns a new list of integers
  23. considered to be the "next generation"
  24. """
  25. N = len(L) # N now holds the size of the list :
  26. return [ setNewElement( L, i ) for i in range(N) ]
  27.  
  28. def setNewElement( L, i, x=0 ):
  29. """ setNewElement returns the NEW list's ith element
  30. input L: any list of integers
  31. input i: the index of the new element to return
  32. input x: an exra, optional input for future use
  33. """
  34. return L[i] + 1
  35.  
  36.  
  37. # What's happening in the above example
  38. """
  39. runGenerations( [1,2,3] ) is being continuously ran by its recursion from 'runGenerations( newL ).
  40. Furthermore, the list all becomes one value higher because of setNewElement and how it is functioning with evolve.
  41. Evolve is taking the list registered in runGenerations and is returning the list through setNewElement
  42. causing the list to all increase by one and to be outputted as 'newL'.
  43. """
  44.  
  45. # Q0
  46.  
  47. def setNewElement( L, i, x=0 ):
  48. """ setNewElement returns the NEW list's ith element
  49. input L: any list of integers
  50. input i: th eindex of the new element to return
  51. input x: an extra, optional input for future use
  52. """
  53. return L[i]*2
  54.  
  55. # Q1
  56. def setNewElement( L, i, x=0 ):
  57. """ setNewElement returns the NEW list's ith element
  58. input L: any list of integers
  59. input i: th eindex of the new element to return
  60. input x: an extra, optional input for future use
  61. """
  62. return L[i]**3
  63.  
  64. # Q2
  65. def setNewElement( L, i, x=0 ):
  66. """ setNewElement returns the NEW list's ith element
  67. input L: any list of integers
  68. input i: the index of the new element to return
  69. input x: an extra, optional input for future use
  70. """
  71. return L[i-1]
  72.  
  73. # Q3
  74. def setNewElement( L, i, x=0 ):
  75. """ setNewElement returns the NEW list's ith element
  76. input L: any list of integers
  77. input i: the index of the new element to return
  78. input x: an extra, optional input for future use
  79. """
  80. if i == len(L)-1:
  81. return L[0]
  82.  
  83. else:
  84. return L[i+1]
  85.  
  86. # Q4
  87. def setNewElement( L, i, x=0 ):
  88. """ setNewElement returns the NEW list's ith element
  89. input L: any list of integers
  90. input i: the index of the new element to return
  91. input x: an extra, optional input for future use
  92. """
  93. return choice( [0,1] )
  94.  
  95.  
  96. # Detecting when we've reached our goal
  97. def allOnes(L):
  98. for x in L: # for every element in L is defined as x
  99. if x is not 1:
  100. return False
  101.  
  102. return True
  103.  
  104. # Improving runGenerations
  105.  
  106. this function below
  107.  
  108. def runGenerations( L ):
  109. """ runGenerations keeps running evolve...
  110. """
  111. count = 0
  112. print(L) # display the list, L
  113. time.sleep(0.5) # pause a bit
  114. count = count + 1
  115.  
  116. if allOnes(L) == True:
  117. return print(count) + exit()
  118.  
  119.  
  120. newL = evolve( L ) # evolve L into newL
  121. runGenerations( newL ) # recurse
  122. if allOnes(L) == False:
  123. return runGenerations(L)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement