A Slice of Life - Phillip...

34
A Slice of Life 02-201

Transcript of A Slice of Life - Phillip...

Page 1: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

A Slice of Life 02-201

Page 2: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

More on Slices

Page 3: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

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!

Page 4: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

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.

Page 5: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

Return to Prime Numbers

func PrintList(list []int) {!!for _, val := range list {!! !fmt.Println(val)!!}!

}!

This function now works for any slice of ints.

Page 6: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

Brainteasers

Exercise: Given a slice a, how could you delete the element at index i?

Page 7: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

Brainteasers

Exercise: Given a slice a, how could you delete the element at index i?

a = append(a[:i], a[i+1:])!

Page 8: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

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)?

Page 9: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

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:])!

Page 10: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

The Game of Life

Page 11: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],
Page 12: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

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

Page 13: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

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

Page 14: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

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

Page 15: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

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

Page 16: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

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

Page 17: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

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

Page 18: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

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

Page 19: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

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.

Page 20: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

Quick Quiz

Exercise: What is the next iteration of this board?

Page 21: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

Stable Forms

Page 22: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

Quick Quiz

Exercise: Carry out the next few iterations of this board. What happens?

Page 23: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

Oscillators

Page 24: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

Getting More Complicated …

Page 25: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

The Strange Behavior of the R-Pentomino

Page 26: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

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!

Page 27: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

Bill Gosper’s Glider

Courtesy: Thane Plambeck!

Page 28: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

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.

Page 29: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

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”.

Page 30: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

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

Page 31: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

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.

Page 32: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

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

Page 33: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

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)?

Page 34: A Slice of Life - Phillip Compeaucompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_13.pdfBrainteasers Exercise: Given a slice a, how could you delete the element at index i?a = append(a[:i],

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!