Adapted from slides by Marty Stepp and Stuart Reges

Slides:



Advertisements
Similar presentations
Topic 17 assertions and program logic
Advertisements

1 Various Methods of Populating Arrays Randomly generated integers.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Copyright 2008 by Pearson Education 1 Midterm announcements Next week on Friday May 8 Must bring an ID Open book, open notes, closed electronics Must attend.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: Arrays for Tallying; Text Processing reading: 4.3, 7.6.
1 Logical Assertions. 2 Logical assertions assertion: A statement that is either true or false. Examples: –Java was created in –The sky is purple.
Coding Design Tools Rachel Gauci. What are Coding Design Tools? IPO charts (Input Process Output) Input- Make a list of what data is required (this generally.
1 Building Java Programs Chapter 5 Lecture 5-2: Random Numbers reading: 5.1, 5.6.
1 Reasoning about assertions Readings: Assertions assertion: A statement that is either true or false. Examples:  Java was created in (true)
1 CSE 142 Midterm Review Problems These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or modified.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 17: Arrays for Tallying; Text Processing reading: 4.3, 7.6 (Slides adapted.
Topic 23 arrays - part 3 (tallying, text processing) Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Copyright 2009 by Pearson Education Building Java Programs Chapter 5 Lecture 5-3: Boolean Logic reading: 5.2 self-check: # exercises: #12 videos:
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
CSc 110, Autumn 2016 Lecture 9: input ; if/else Adapted from slides by Marty Stepp and Stuart Reges.
CSc 110, Autumn 2017 Lecture 13: Cumulative Sum and Boolean Logic
CSc 110, Autumn 2017 Lecture 17: while Loops and Sentinel Loops
Python: Control Structures
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.
CSc 110, Spring 2017 Lecture 14: Boolean Logic and Midterm Review
CSc 110, Autumn 2017 Lecture 24: Lists for Tallying; Text Processing
CSc 110, Autumn 2017 Lecture 18: While loops and File Input
Adapted from slides by Marty Stepp and Stuart Reges
Midterm Review Problems
CSc 110, Autumn 2016 Lecture 13: Random Numbers
CSc 110, Spring 2017 Lecture 8: input; if/else
CSc 110, Autumn 2016 Lecture 12: while Loops, Fencepost Loops, and Sentinel Loops Adapted from slides by Marty Stepp and Stuart Reges.
Writing Functions( ) (Part 5)
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
CSc 110, Spring 2017 Lecture 9: Advanced if/else; Cumulative sum
CSc 110, Spring 2018 Lecture 16: Fencepost Loops and while loops
CSc 110, Autumn 2016 Lecture 9: input; if/else
CSc 110, Autumn 2017 Lecture 9: Graphics and Nested Loops
Building Java Programs
Logical assertions assertion: A statement that is either true or false. Examples: Java was created in The sky is purple. 23 is a prime number. 10.
Lecture 7-3: Arrays for Tallying; Text Processing
Topic 23 arrays - part 3 (tallying, text processing)
Adapted from slides by Marty Stepp and Stuart Reges
Topic 17 assertions and program logic
CSc 110, Autumn 2016 Lecture 20: Lists for Tallying; Text Processing
CSc 110, Autumn 2016 Lecture 10: Advanced if/else; Cumulative sum
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Module 4 Loops.
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Building Java Programs
What does this do? def revList(L): if len(L) < = 1: return L x = L[0] LR = revList(L[1:]) return LR + x.
Chapter 6: Repetition Statements
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2018 Lecture 13: Cumulative Sum and Boolean Logic
CSc 110, Spring 2017 Lecture 20: Lists for Tallying; Text Processing
CSc 110, Spring 2018 Lecture 27: Lists that change size and File Output Adapted from slides by Marty Stepp and Stuart Reges.
CSc 110, Spring 2018 Lecture 8: input and Parameters
Building Java Programs
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Building Java Programs
Building Java Programs
Indefinite loop variations
Building Java Programs
CSc 110, Autumn 2016 Lecture 20: Lists for Tallying; Text Processing
This shows running the first guess number program which only allows one guess - I show the correct answer for testing purposes.
Starter Look at the hand-out.
Adapted from slides by Marty Stepp and Stuart Reges
Presentation transcript:

Adapted from slides by Marty Stepp and Stuart Reges CSc 110, Autumn 2017 Lecture 26: Assertions Adapted from slides by Marty Stepp and Stuart Reges 1

Section attendance question Read a file of section attendance (see next slide): yynyyynayayynyyyayanyyyaynayyayyanayyyanyayna ayyanyyyyayanaayyanayyyananayayaynyayayynynya yyayaynyyayyanynnyyyayyanayaynannnyyayyayayny And produce the following output: Section 1 Student points: [20, 16, 17, 14, 11] Student grades: [100.0, 80.0, 85.0, 70.0, 55.0] Section 2 Student points: [16, 19, 14, 14, 8] Student grades: [80.0, 95.0, 70.0, 70.0, 40.0] Section 3 Student points: [16, 15, 16, 18, 14] Student grades: [80.0, 75.0, 80.0, 90.0, 70.0] Students earn 3 points for each section attended up to 20.

Section input file Each line represents a section. student 123451234512345123451234512345123451234512345 Each line represents a section. A line consists of 9 weeks' worth of data. Each week has 5 characters because there are 5 students. Within each week, each character represents one student. a means the student was absent (+0 points) n means they attended but didn't do the problems (+1 points) y means they attended and did the problems (+3 points) week 1 2 3 4 5 6 7 8 9 section 1 section 2 section 3 yynyyynayayynyyyayanyyyaynayyayyanayyyanyayna ayyanyyyyayanaayyanayyyananayayaynyayayynynya yyayaynyyayyanynnyyyayyanayaynannnyyayyayayny

Logical assertions assertion: A statement that is either true or false. Examples: Python was created in 1995. The sky is purple. 23 is a prime number. 10 is greater than 20. x divided by 2 equals 7. (depends on the value of x) An assertion might be false ("The sky is purple" above), but it is still an assertion because it is a true/false statement.

Reasoning about assertions Suppose you have the following code: if x >= 3: # Point A x -= 1 else: # Point B x += 1 # Point C # Point D What do you know about x's value at the three points? Is x > 3? Always? Sometimes? Never?

Assertions in code number = input("Type a nonnegative number: ") We can make assertions about our code and ask whether they are true at various points in the code. Valid answers are ALWAYS, NEVER, or SOMETIMES. number = input("Type a nonnegative number: ") # Point A: is number < 0.0 here? while number < 0.0: # Point B: is number < 0.0 here? number = input("Negative; try again: ") # Point C: is number < 0.0 here? # Point D: is number < 0.0 here? (SOMETIMES) (ALWAYS) (NEVER)

Reasoning about assertions Right after a variable is initialized, its value is known: x = 3 # is x > 0? ALWAYS In general you know nothing about parameters' values: def mystery(a, b): # is a == 10? SOMETIMES But inside an if, while, etc., you may know something: if a < 0: # is a == 10? NEVER ...

Assertions and loops At the start of a loop's body, the loop's test must be True: while y < 10: # is y < 10? ALWAYS ... After a loop, the loop's test must be False: # is y < 10? NEVER Inside a loop's body, the loop's test may become False: y += 1 # is y < 10? SOMETIMES

"Sometimes" Things that cause a variable's value to be unknown (often leads to "sometimes" answers): reading from input reading a number from a random object a parameter's initial value to a function If you can reach a part of the program both with the answer being "yes" and the answer being "no", then the correct answer is "sometimes". If you're unsure, "Sometimes" is a good guess.

Assertion example 1 def mystery(x, y): z = 0 # Point A while x >= y: # Point B x = x - y z += 1 if x != y: # Point C z = z * 2 # Point D # Point E print(z) Which of the following assertions are true at which point(s) in the code? Choose ALWAYS, NEVER, or SOMETIMES. SOMETIMES ALWAYS NEVER x < y x == y z == 0 Point A Point B Point C Point D Point E

Assertion example 2 def mystery(): prev = 0 count = 0 next = input() # Point A while next != 0: # Point B if next == prev: # Point C count += 1 prev = next # Point D # Point E return count Which of the following assertions are true at which point(s) in the code? Choose ALWAYS, NEVER, or SOMETIMES. SOMETIMES ALWAYS NEVER next == 0 prev == 0 next == prev Point A Point B Point C Point D Point E Observation: You can base your knowledge of variable B on variable A if B's value is related to A's. In this slide, we know things about next, so we also know things about prev at certain points.

Assertion example 3 # Assumes y >= 0, and returns x^y def pow(x, y): prod = 1 # Point A while (y > 0): # Point B if (y % 2 == 0): # Point C x = x * x y = y // 2 # Point D else: # Point E prod = prod * x y -= 1 # Point F # Point G return prod Which of the following assertions are true at which point(s) in the code? Choose ALWAYS, NEVER, or SOMETIMES. y > 0 y % 2 == 0 Point A SOMETIMES Point B ALWAYS Point C Point D Point E NEVER Point F Point G y > 0 y % 2 == 0 Point A Point B Point C Point D Point E Point F Point G