Program Design Invasion Percolation: Bugs

Slides:



Advertisements
Similar presentations
Invasion Percolation: Tuning Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Advertisements

Introduction 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
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Section 7.4: Closures of Relations Let R be a relation on a set A. We have talked about 6 properties that a relation on a set may or may not possess: reflexive,
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
 2002 Prentice Hall. All rights reserved Control Structures 3 control structures –Sequential structure Built into Python –Selection structure The.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Arrays After a while. Remember for loops? for(int i = 0; i < 10; i++){ System.out.println(i); } -But what if we are unsure of how many cycles we need?
CIS 234: LOOPS Adapted from materials by Dr. Donald Bell, 2000 (updated April 2007)
Invasion Percolation: Assembly Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
PHY281 Scientific Java Programming LoopsSlide 1 Loops In this section we will learn how to repeat a series of instructions using loops and to use this.
CS101 Computer Programming I Chapter 4 Extra Examples.
Interface and Implementation Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Debuggers in Python. The Debugger Every programming IDE has a tool called a debugger. This application does NOT locate or fix your bugs for you! It slows.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Invasion Percolation: Randomness Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
ITERATION. Iteration Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that.
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.
PYTHON WHILE LOOPS. What you know While something is true, repeat your action(s) Example: While you are not facing a wall, walk forward While you are.
Chapter 6 Controlling Program Flow with Looping Structures.
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
Python Aliasing Copyright © Software Carpentry 2010
Sound and more Animations
Branching Error (a.k.a. the VM Program Instruction Break Error)
Program Design Invasion Percolation: Aliasing
high-level operations on pictures
Selection (also known as Branching) Jumail Bin Taliba by
Program Design Invasion Percolation: Testing
Sets and Dictionaries Examples Copyright © Software Carpentry 2010
Control Structures II Chapter 3
CSE 311 Foundations of Computing I
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Program Design Invasion Percolation: Resolving Ties
Program Design Invasion Percolation: Neighbors
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Writing Functions( ) (Part 5)
Call Stacks, Arguments and Returns
Looping and Repetition
Graph Paper Programming
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Alternate Version of STARTING OUT WITH C++ 4th Edition
Copyright © Cengage Learning. All rights reserved.
IST256 : Applications Programming for Information Systems
Version Control Basic Operation Copyright © Software Carpentry 2010
CMPT 102 Introduction to Scientific Computer Programming
Halting Problem.
UMBC CMSC 104 – Section 01, Fall 2016
Copyright © Cengage Learning. All rights reserved.
Python programming exercise
Version Control Introduction Copyright © Software Carpentry 2010
Version Control Conflict Copyright © Software Carpentry 2010
Data Structures & Algorithms
Copyright © Cengage Learning. All rights reserved.
Program Design Invasion Percolation: The Grid
Copyright © Cengage Learning. All rights reserved.
Python While Loops.
Browsing Directories Using walk
Topic: Loops Loops Idea While Loop Introduction to ranges For Loop
Selections and Loops By Sarah, Melody, Teresa.
Copyright © Cengage Learning. All rights reserved.
Copyright © Cengage Learning. All rights reserved.
Review of Previous Lesson
Copyright © Cengage Learning. All rights reserved.
Looping and Repetition
Software Testing and QA Theory and Practice (Chapter 5: Data Flow Testing) © Naik & Tripathy 1 Software Testing and Quality Assurance Theory and Practice.
Presentation transcript:

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

Let's try running the program we just created

Let's try running the program we just created $ python invperc.py 3 10 17983 2 cells filled 3

Let's try running the program we just created $ python invperc.py 3 10 17983 2 cells filled Excellent: 2 cells should be filled in a 3×3 grid 4

Let's try running the program we just created $ python invperc.py 3 10 17983 2 cells filled Excellent: 2 cells should be filled in a 3×3 grid Let's try a larger grid 5

Let's try running the program we just created $ python invperc.py 3 10 17983 2 cells filled Excellent: 2 cells should be filled in a 3×3 grid Let's try a larger grid $ python invperc.py 5 10 27187 6

Let's try running the program we just created $ python invperc.py 3 10 17983 2 cells filled Excellent: 2 cells should be filled in a 3×3 grid Let's try a larger grid $ python invperc.py 5 10 27187 ...a minute passes... 7

Let's try running the program we just created $ python invperc.py 3 10 17983 2 cells filled Excellent: 2 cells should be filled in a 3×3 grid Let's try a larger grid $ python invperc.py 5 10 27187 ...a minute passes... ctrl-C Time to fire up the debugger... 8

The initial grid looks right 5 3 7 2 6 8 4 9 9

The initial grid looks right Still looks good after filling the middle cell 5 3 7 2 6 8 -1 4 9 10

The initial grid looks right Still looks good after filling the middle cell 5 3 7 2 6 8 -1 4 9 Remember, we're using -1 to mark filled cells 11

The initial grid looks right Still looks good after filling the middle cell Next cell filled correctly 5 3 7 2 6 8 -1 4 9 12

The initial grid looks right Still looks good after filling the middle cell Next cell filled correctly 5 3 7 2 6 8 -1 4 9 Then the program goes into an infinite loop 13

The initial grid looks right Still looks good after filling the middle cell Next cell filled correctly 5 3 7 2 6 8 -1 4 9 Then the program goes into an infinite loop In the find_candidates function 14

The initial grid looks right Still looks good after filling the middle cell Next cell filled correctly 5 3 7 2 6 8 -1 4 9 Then the program goes into an infinite loop In the find_candidates function min_set == {(2, 2), (1, 2)} min_val == -1 15

The initial grid looks right Still looks good after filling the middle cell Next cell filled correctly 5 3 7 2 6 8 -1 4 9 Then the program goes into an infinite loop In the find_candidates function min_set == {(2, 2), (1, 2)} min_val == -1 Uh oh... 16

Our marker value is less than all the actual values in the grid... 17

Our marker value is less than all the actual values in the grid... ...and once we have marked two cells... 18

Our marker value is less than all the actual values in the grid... ...and once we have marked two cells... ...each of those marked cells is adjacent to a marked cell 19

Our marker value is less than all the actual values in the grid... ...and once we have marked two cells... ...each of those marked cells is adjacent to a marked cell At least it's easy to fix 20

Old (buggy) code def find_candidates(grid): N = len(grid) min_val = sys.maxint min_set = set() for x in range(N): for y in range(N): if is_candidate(grid, x, y): ...handle == min_val and < min_val cases... 21

New (correct) code def find_candidates(grid): N = len(grid) min_val = sys.maxint min_set = set() for x in range(N): for y in range(N): if grid[x][y] == FILLED: continue # skip to next cell if is_candidate(grid, x, y): ...handle == min_val and < min_val cases... 22

Great—we found one bug 23

How many others haven't we found? Great—we found one bug How many others haven't we found? 24

How many others haven't we found? Great—we found one bug How many others haven't we found? How do we validate and verify this program? 25

"If x is either 0 or N-1, or y is either 0 or N-1" num_filled += 1 if x in (0, N-1) or y in (0, N-1): break "If x is either 0 or N-1, or y is either 0 or N-1"

"If x is either 0 or N-1, or y is either 0 or N-1" num_filled += 1 if x in (0, N-1) or y in (0, N-1): break "If x is either 0 or N-1, or y is either 0 or N-1" I.e., if either coordinate is on the grid's edge

Sound similar, but fails num_filled += 1 if x is (0, N-1) or y is (0, N-1): break Sound similar, but fails

Sound similar, but fails num_filled += 1 if x is (0, N-1) or y is (0, N-1): break Sound similar, but fails "If x is the tuple (0, N-1) or y is the tuple (0, N-1)"

Sound similar, but fails num_filled += 1 if x is (0, N-1) or y is (0, N-1): break Sound similar, but fails "If x is the tuple (0, N-1) or y is the tuple (0, N-1)" Neither x nor y will ever be a two-valued tuple

Sound similar, but fails num_filled += 1 if x is (0, N-1) or y is (0, N-1): break Sound similar, but fails "If x is the tuple (0, N-1) or y is the tuple (0, N-1)" Neither x nor y will ever be a two-valued tuple So the loop will never exit

Sounds like what we'd say to another person num_filled += 1 if x or y is (0, N-1): break Sounds like what we'd say to another person

Sounds like what we'd say to another person num_filled += 1 if x or y is (0, N-1): break Sounds like what we'd say to another person But how will the computer interpret it?

Interpretation #1 is or (0, N-1) x y

Interpretation #1 is True (0, N-1) x y

True isn't a two-integer tuple Interpretation #1 is True (0, N-1) x y True isn't a two-integer tuple

True isn't a two-integer tuple (Neither is False) Interpretation #1 is True (0, N-1) x y True isn't a two-integer tuple (Neither is False)

True isn't a two-integer tuple (Neither is False) Interpretation #1 is True (0, N-1) x y True isn't a two-integer tuple (Neither is False) So this definitely isn't right

Interpretation #1 Interpretation #2 is or or (0, N-1) x is x y y

y is never a two-integer tuple Interpretation #1 Interpretation #2 is or or (0, N-1) x is x y y (0, N-1) y is never a two-integer tuple

y is never a two-integer tuple So this is just "x or False" Interpretation #1 Interpretation #2 is or or (0, N-1) x is x y y (0, N-1) y is never a two-integer tuple So this is just "x or False"

y is never a two-integer tuple So this is just "x or False" Interpretation #1 Interpretation #2 is or or (0, N-1) x is x y y (0, N-1) y is never a two-integer tuple So this is just "x or False" Which is just "x is not 0"

y is never a two-integer tuple So this is just "x or False" Interpretation #1 Interpretation #2 is or or (0, N-1) x is x y y (0, N-1) y is never a two-integer tuple So this is just "x or False" Which is just "x is not 0" Not what we want either

This is what Python actually does Interpretation #1 Interpretation #2 is or or (0, N-1) x is x y y (0, N-1) This is what Python actually does

This is what Python actually does "a or b is c" binds like "x + y * z" Interpretation #1 Interpretation #2 is or or (0, N-1) x is x y y (0, N-1) This is what Python actually does "a or b is c" binds like "x + y * z"

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.