Program Design Invasion Percolation: Neighbors

Slides:



Advertisements
Similar presentations
Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh This work is licensed under the Creative Commons Attribution.
Advertisements

Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Slicing Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Tuples Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Floating Point Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Control Flow Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Resistors are available in many different values and the colour bands on them are used to indicate what value the resistor is. Looking at this resistor,
Introduction Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
Mechanics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
Timers Exploring Computer Science Lesson Objectives The students will be able to: Create a timer.
Invasion Percolation: Assembly Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Pipes and Filters Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Interface and Implementation Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction Copyright © Software Carpentry This work is licensed under the Creative Commons Attribution License See
Rollback Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Invasion Percolation: Randomness Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Exceptions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Basics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Dictionaries Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Counting Stars Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Testing Programs with Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Patterns Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Introduction to Programming: Module 1 Blockly
Identify the Appropriate Method for Handling Repetition
Python Aliasing Copyright © Software Carpentry 2010
Making Finger Joints.
Program Design Invasion Percolation: Aliasing
Examining Two Pieces of Data
About this template This is a template to guide you through making your science fair display board for the What Makes Ice Melt Fastest? project. This.
Q 1 & 2 p23 1. Write the following measurements in scientific notation: a m d m b kg e km c L f.
Program Design Invasion Percolation: Testing
Sets and Dictionaries Examples Copyright © Software Carpentry 2010
Program Design Invasion Percolation: Resolving Ties
Topics The if Statement The if-else Statement Comparing Strings
Title Slide BEGIN GAME $100,000 RULES/CREDITS HOW TO PLAY/EDIT
7.4 Problem Solving with Recursion
Programming Abstractions
Topics The if Statement The if-else Statement Comparing Strings
Chinese Multiplication
While Loops and If-Else Structures
INTERMEDIATE PROGRAMMING LESSON
Black Box Software Testing Fall 2005 Overview – Part 1 of 3
Renaming and Oriented Manifolds
Version Control Basic Operation Copyright © Software Carpentry 2010
Using Arrays Completion of ideas needed for a general and complete program Final concepts needed for Final.
MATLAB Programming Indexing Copyright © Software Carpentry 2011
Rules of evaluation The value of a number is itself.
INTERMEDIATE PROGRAMMING LESSON
Control Structure Testing
Version Control Introduction Copyright © Software Carpentry 2010
Program Design Invasion Percolation: Bugs
Training for Test Administrators
Learning Intention I will learn about testing programs.
Exploring Computer Science Lesson 4-12
Test Taking Strategies
Version Control Conflict Copyright © Software Carpentry 2010
Introduction to Programming in MATLAB
Program Design Invasion Percolation: The Grid
Finding missing side-rhombus
Browsing Directories Using walk
Test Taking Strategies
Selections and Loops By Sarah, Melody, Teresa.
Finding missing side-quadrilateral
BEGINNER PROGRAMMING LESSON
2D Array and Matrix Application: Game of Life
Two Minute Drill – Residential 3
Exploring Computer Science Lesson 4-12
Presentation transcript:

Program Design Invasion Percolation: Neighbors Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information.

Need to find neighbors of marked cells 5 3 7 2 6 1 4 8 9

Need to find neighbors of marked cells ...which are marked with -1 5 3 7 2 6 1 4 8 9 -1 3

Blue cell is a neighbor if any of the green cells 5 -1 7 2 8 6 4 Blue cell is a neighbor if any of the green cells have already been filled 4

Those cells coordinates are the center cell's ±1 to either x or y (x, y+1) (x+1, y) 5 -1 7 2 8 6 4 (x-1, y) (x, y+1) Those cells coordinates are the center cell's ±1 to either x or y 5

We're assuming diagonal cells don't count (x-1, y+1) (x+1, y+1) 5 -1 7 2 8 6 4 (x-1, y-1) (x+1, y-11) We're assuming diagonal cells don't count 6

We're assuming diagonal cells don't count Need to check the science... (x-1, y+1) (x+1, y+1) 5 -1 7 2 8 6 4 (x-1, y-1) (x+1, y-11) We're assuming diagonal cells don't count Need to check the science... 7

# Is a cell a candidate for filling? # Version 1: has bugs! for x in range(N): for y in range(N): if is_filled(grid, x-1, y) \ or is_filled(grid, x+1, y) \ or is_filled(grid, x, y-1) \ or is_filled(grid, x, y+1): ...cell (x, y) is a candidate...

What do we do at the edges? 5 -1 7 6 9 2 8 4 3 ? What do we do at the edges? 9

[0-1] == -1 wraps around to the other edge 5 -1 7 6 9 2 8 4 3 [0-1] == -1 wraps around to the other edge 10

[(N-1)+1] == N is an out-of-bounds exception 5 -1 7 6 9 2 8 4 3 ! [(N-1)+1] == N is an out-of-bounds exception 11

# Is a cell a candidate for filling? # Version 2: long-winded for x in range(N): for y in range(N): if x > 0: if is_filled(grid, x-1, y): ...cell (x, y) is a candidate... ...repeat for the other three cases... 12

"Code that is repeated in two or more places # Is a cell a candidate for filling? # Version 2: long-winded for x in range(N): for y in range(N): if x > 0: if is_filled(grid, x-1, y): ...cell (x, y) is a candidate... ...repeat for the other three cases... "Code that is repeated in two or more places will eventually be wrong in at least one." 13

# Is a cell a candidate for filling? # Version 3: good enough for production for x in range(N): for y in range(N): if (x > 0) and is_filled(x-1, y)\ or (x < N-1) and is_filled(x+1, y)\ or (y > 0) and is_filled(x, y-1)\ or (y < N-1) and is_filled(x, y+1): ...cell (x, y) is a candidate... 14

if (x > 0) and is_filled(x-1, y) Not on the left edge 15

Not on the left edge Neighbor is already filled if (x > 0) and is_filled(x-1, y) Not on the left edge Neighbor is already filled 16

Short-circuit evaluation if (x > 0) and is_filled(x-1, y) Short-circuit evaluation Not on the left edge Neighbor is already filled 17

if sanity_check and some_other_test: Make sure the second test won't blow up Only execute if it's safe to do so Don't try second part if first part is False because the answer is already known

Greg Wilson created by May 2010 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information.