A Slice of Life 02-201
More on Slices
Last Time: append() and copy() Operations
s := make([]int, 10)!s = append(s, 5)!
0! 0! 0! 0! 0! 0! 0! 0! 0! 0! 5!0! 1! 2! 3! 4! 5! 6! 7! 8! 9! 10!
c := make([]int, 11)!copy(c, s)!
0! 0! 0! 0! 0! 0! 0! 0! 0! 0! 5!0! 1! 2! 3! 4! 5! 6! 7! 8! 9! 10!
s!
c!
Return to Prime Numbers
func ListPrimes(n int) []int {!!primeBool := PrimeSieve(n)!!primes := make([]int, 0)!!for i:=range primeBool {!! !if !primeBool[i] && i>=2 {!! ! !primes = append(primes, i)!! !}!!}!!return primes!
}!
This stores the primes, which we can print separately.
Return to Prime Numbers
func PrintList(list []int) {!!for _, val := range list {!! !fmt.Println(val)!!}!
}!
This function now works for any slice of ints.
Brainteasers
Exercise: Given a slice a, how could you delete the element at index i?
Brainteasers
Exercise: Given a slice a, how could you delete the element at index i?
a = append(a[:i], a[i+1:])!
Brainteasers
Exercise: Given a slice a, how could you delete the element at index i?
a = append(a[:i], a[i+1:])!
Exercise: Given a slice a, how would you replace the i-th element with the j-th element (thus reducing the length of the slice by 1)?
Brainteasers
Exercise: Given a slice a, how could you delete the element at index i?
a = append(a[:i], a[i+1:])!
Exercise: Given a slice a, how would you replace the i-th element with the j-th element (thus reducing the length of the slice by 1)?
a[i] = a[j]!a = append(a[:j], a[j+1:])!
The Game of Life
Cellular Automata in HW3
The rule
Produces the pattern on the right above, something even more complex.
The Autolab Assignment
Input. You will write a program that takes in a CA rule and produces a given number of iterationsof the CA. Your command should be able to be run using the following command line:
go run ca.go RULE WIDTH STEPS
Where STEPS is the number of steps to run the CA. Since we can’t work with an infinitely longsequence of cells, we will only model WIDTH cells. Finally, RULE is a sequence of 8 ’0’s and ’1’sthat give the outcome of each subrule in the order used in the examples above. For example, therule:
would be given as “11111010” and the rule
2
Rule
Cellular Automata in HW3
The rule
Produces the pattern on the right above, something even more complex.
The Autolab Assignment
Input. You will write a program that takes in a CA rule and produces a given number of iterationsof the CA. Your command should be able to be run using the following command line:
go run ca.go RULE WIDTH STEPS
Where STEPS is the number of steps to run the CA. Since we can’t work with an infinitely longsequence of cells, we will only model WIDTH cells. Finally, RULE is a sequence of 8 ’0’s and ’1’sthat give the outcome of each subrule in the order used in the examples above. For example, therule:
would be given as “11111010” and the rule
2
Rule
Cellular Automata in HW3
The rule
Produces the pattern on the right above, something even more complex.
The Autolab Assignment
Input. You will write a program that takes in a CA rule and produces a given number of iterationsof the CA. Your command should be able to be run using the following command line:
go run ca.go RULE WIDTH STEPS
Where STEPS is the number of steps to run the CA. Since we can’t work with an infinitely longsequence of cells, we will only model WIDTH cells. Finally, RULE is a sequence of 8 ’0’s and ’1’sthat give the outcome of each subrule in the order used in the examples above. For example, therule:
would be given as “11111010” and the rule
2
Rule
Cellular Automata in HW3
The rule
Produces the pattern on the right above, something even more complex.
The Autolab Assignment
Input. You will write a program that takes in a CA rule and produces a given number of iterationsof the CA. Your command should be able to be run using the following command line:
go run ca.go RULE WIDTH STEPS
Where STEPS is the number of steps to run the CA. Since we can’t work with an infinitely longsequence of cells, we will only model WIDTH cells. Finally, RULE is a sequence of 8 ’0’s and ’1’sthat give the outcome of each subrule in the order used in the examples above. For example, therule:
would be given as “11111010” and the rule
2
Rule
Cellular Automata in HW3
The rule
Produces the pattern on the right above, something even more complex.
The Autolab Assignment
Input. You will write a program that takes in a CA rule and produces a given number of iterationsof the CA. Your command should be able to be run using the following command line:
go run ca.go RULE WIDTH STEPS
Where STEPS is the number of steps to run the CA. Since we can’t work with an infinitely longsequence of cells, we will only model WIDTH cells. Finally, RULE is a sequence of 8 ’0’s and ’1’sthat give the outcome of each subrule in the order used in the examples above. For example, therule:
would be given as “11111010” and the rule
2
Rule
Cellular Automata in HW3
The rule
Produces the pattern on the right above, something even more complex.
The Autolab Assignment
Input. You will write a program that takes in a CA rule and produces a given number of iterationsof the CA. Your command should be able to be run using the following command line:
go run ca.go RULE WIDTH STEPS
Where STEPS is the number of steps to run the CA. Since we can’t work with an infinitely longsequence of cells, we will only model WIDTH cells. Finally, RULE is a sequence of 8 ’0’s and ’1’sthat give the outcome of each subrule in the order used in the examples above. For example, therule:
would be given as “11111010” and the rule
2
Rule
2-D Cellular Automaton: The Game of Life
1. A live cell (dark) with < 2 live neighbors dies. 2. A live cell with 2-3 live neighbors lives on. 3. A live cell with > 3 neighbors dies. 4. A dead cell (light) with exactly three live
neighbors becomes live.
Neighborhood
2-D Cellular Automaton: The Game of Life
1. A live cell (dark) with < 2 live neighbors dies. 2. A live cell with 2-3 live neighbors lives on. 3. A live cell with > 3 neighbors dies. 4. A dead cell (light) with exactly three live
neighbors becomes live.
Quick Quiz
Exercise: What is the next iteration of this board?
Stable Forms
Quick Quiz
Exercise: Carry out the next few iterations of this board. What happens?
Oscillators
Getting More Complicated …
The Strange Behavior of the R-Pentomino
John Conway’s Question
Think: Is it possible for the number of live cells to grow without bound as time goes on?
Courtesy: Thane Plambeck!
Bill Gosper’s Glider
Courtesy: Thane Plambeck!
Hacking the Game of Life
Game of Life Problem: • Input: An initial configuration of the Game of Life
board, and an integer n. • Output: All configurations of this board over n
generations.
Recall: Multidimensional Arrays
0 1 2
3 4 5 6
0 1 2 3 var a [7][4]int!a[1][2] = 19!y := a[3][0] // gives 0!x := len(a) // gives 7!
7 rows
4 columns
Think of as “an array of length 7, where each array contains an array of length 4”.
Creating a 2-D Slice
var field [][]bool = make([][]bool, m)!
2-D slices are also “slices of slices” – we must define the outer slice first.
0 1 2
3 4 5 6
0 1 2 3
m rows
n columns
Creating a 2-D Slice
var field [][]bool = make([][]bool, m)!
2-D slices are also “slices of slices” – we must define the outer slice first.
0 1 2
3 4 5 6
0 1 2 3
m rows
n columns
for row := range field {! field[row] = make([]bool, n)!}!
To initialize the slices in field, write an explicit loop.
Creating a 2-D Slice
var field [][]bool = make([][]bool, m)!
for row := range field {! field[row] = make([]bool, n)!}!
var x = len(field)/2!var y = len(field)/2!field[x][y] = true!
2-D slices are also “slices of slices” – we must define the outer slice first.
To initialize the slices in field, write an explicit loop.
Can use as 2-D array now
0 1 2
3 4 5 6
0 1 2 3
m rows
n columns
Storing all n Generations
Think: How do we store all n generations of a Game of Life Board (which we are storing as a 2-D slice of boolean variables)?
Storing all n Generations
Think: How do we store all n generations of a Game of Life Board (which we are storing as a 2-D slice of boolean variables)?
Answer: A 3-D slice!