Skip to content

Commit 4dcecb9

Browse files
authored
Update elementary_cellular_automaton.py
1 parent 9da123f commit 4dcecb9

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

cellular_automata/elementary_cellular_automaton.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,22 @@
66
Each cell's next state depends on its current state and its two immediate neighbors.
77
88
Reference:
9-
https://en.wikipedia.org/wiki/Rule_30
9+
https://en.wikipedia.org/wiki/Rule_30
1010
"""
1111

12-
from typing import List
1312

14-
15-
def rule_30_step(current: List[int]) -> List[int]:
13+
def rule_30_step(current: list[int]) -> list[int]:
1614
"""
1715
Compute the next generation of a one-dimensional cellular automaton
1816
following Wolfram's Rule 30.
1917
2018
Each cell's next state is determined by its left, center, and right neighbors.
2119
2220
Args:
23-
current (List[int]): The current generation as a list of 0s (dead) and 1s (alive).
21+
current (list[int]): The current generation as a list of 0s (dead) and 1s (alive).
2422
2523
Returns:
26-
List[int]: The next generation as a list of 0s and 1s.
24+
list[int]: The next generation as a list of 0s and 1s.
2725
2826
Example:
2927
>>> rule_30_step([0, 0, 1, 0, 0])
@@ -38,22 +36,22 @@ def rule_30_step(current: List[int]) -> List[int]:
3836
# Combine neighbors into a 3-bit pattern
3937
pattern = (left << 2) | (center << 1) | right
4038

41-
# Rule 30 binary: 00011110 -> 30 in decimal
39+
# Rule 30 binary: 00011110 (bitwise representation of 30)
4240
next_gen.append((30 >> pattern) & 1)
4341

4442
return next_gen
4543

4644

47-
def generate_rule_30(size: int = 31, generations: int = 15) -> List[List[int]]:
45+
def generate_rule_30(size: int = 31, generations: int = 15) -> list[list[int]]:
4846
"""
4947
Generate multiple generations of Rule 30 automaton.
5048
5149
Args:
52-
size (int): Number of cells in one generation.
53-
generations (int): Number of generations to evolve.
50+
size (int): Number of cells in one generation. Default is 31.
51+
generations (int): Number of generations to evolve. Default is 15.
5452
5553
Returns:
56-
List[List[int]]: A list of generations (each a list of 0s and 1s).
54+
list[list[int]]: A list of generations (each a list of 0s and 1s).
5755
5856
Example:
5957
>>> len(generate_rule_30(15, 5))

0 commit comments

Comments
 (0)