[Tutor] First real script
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Mon Nov 6 17:20:42 CET 2006
On Mon, 6 Nov 2006, Carlos wrote:
> This is my first script, I mean the first that I'm trying to do on my
> own. Its a very simple Cellular Automata thing, the idea is that the
> initial list A_List is rewritten based on some rules, in this case
> Wolfram's rule 30. You can modify the list length and its initial state,
> you can also set the number of iterations.
Hi Carlos,
Cool. I did something like this a while ago too!
http://mail.python.org/pipermail/tutor/2002-September/017256.html
Looking at your code, the only suggestion I could make is to encode the
rules into some kind of lookup structure. That is, the block:
> if a == 1 and b == 1 and c == 1:
> X = 0
> elif a == 1 and b == 1 and c == 0:
> X = 0
> elif a == 1 and b == 0 and c == 1:
> X = 0
> elif a == 1 and b == 0 and c == 0:
> X = 1
> elif a == 0 and b == 1 and c == 1:
> X = 1
> elif a == 0 and b == 1 and c == 0:
> X = 1
> elif a == 0 and b == 0 and c == 1:
> X = 1
> elif a == 0 and b == 0 and c == 0:
> X = 0
could be tweaked to be more compact. Conceptually, it takes in three
values (X, Y, Z), and returns a result in X. If we look at it in a
different light, that's a key->value lookup, where the "key" is going to
be a 3-tuple.
So you can encode the guts of rule 30 with a dictionary:
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,
}
After we have this, the assignment to X looks much more simple:
X = rule_30[x, y, z]
Good luck!
More information about the Tutor
mailing list