[Tutor] First real script

Carlos carloslara at web.de
Tue Nov 7 11:13:25 CET 2006


Hello to all,

Ok, after reading your comments I ended up with this:

#Easy_2DCA_01.py
#A very basic 2D script that lets you play with Wolfram's rule 30

A_List = [0]*10+[1]+[0]*10
A_Len = len(A_List)
B_List = []
print A_List
Iterations = 5

rule_30 = {
      (1, 1, 1) : 0,
      (1, 1, 0) : 0,
      (1, 0, 1) : 0,
      (1, 0, 0) : 1,
      (0, 1, 1) : 1,
      (0, 1, 0) : 1,
      (0, 0, 1) : 1,
      (0, 0, 0) : 0,
    }

for j in range (Iterations):
    for i in  range (A_Len):

        x = A_List[i-1]
        y = A_List[i]

        if i < A_Len-1:
            z = A_List[i+1]

        else:
            z = A_List[0]
       
        X = rule_30[x,y,z]

        B_List.append (X)
       
    print B_List
    A_List = B_List
    B_List = []

Not bad I think :P It is compact and readable, even by a dummy like me. 
I took the ideas that I completely understand, there are others that are 
slightly too much for me now, but they are noted too.

I want to thank all you guys for the comments. This is my first script, 
but I can see that this list a great resource for learning Python.

As you may have noticed the name of the script has changed, I will now 
try a 3D CA. I have been thinking that the best way to do this would be 
to use a matrix, like those found in SciPy. Do you think this is a good 
idea? I'm afraid that my reasoning is too linear, to add another 
dimension to my script I will add another dimension to my initial list.

Here is an example:

import scipy
a1 = scipy.zeros((4,5))
print a1
[[ 0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]]

Thats it for now.

Thanks again,
Carlos



More information about the Tutor mailing list