CSc 110, Autumn 2017 Lecture 16: Fencepost Loops and Review

Slides:



Advertisements
Similar presentations
Building Java Programs
Advertisements

Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops; Procedural Design reading: 5.1 – 5.2; 4.5.
Topic 14 while loops and loop patterns Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
1 Fencepost loops suggested reading: The fencepost problem Problem: Write a static method named printNumbers that prints each number from 1 to a.
Building java programs, chapter 5 Program logic and indefinite loops.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Building Java Programs Program Logic and Indefinite Loops.
1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 5.1 – 5.2.
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2016 Lecture 15: lists Adapted from slides by Marty Stepp and Stuart Reges.
1 Building Java Programs Chapter 4: Conditional Execution These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted,
CSc 110, Autumn 2017 Lecture 13: Cumulative Sum and Boolean Logic
CSc 110, Autumn 2017 Lecture 15: Strings and Fencepost Loops
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Categories of loops definite loop: Executes a known number of times.
CSc 110, Autumn 2017 Lecture 17: while Loops and Sentinel Loops
CSc 110, Autumn 2017 Lecture 19: While loops and File Input
Lecture 7: Input and Miscellaneous
CSc 110, Autumn 2017 Lecture 18: While loops and File Input
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2016 Lecture 12: Advanced if/else; Cumulative sum
CSc 110, Autumn 2017 Lecture 31: Dictionaries
CSc 110, Autumn 2016 Lecture 12: while Loops, Fencepost Loops, and Sentinel Loops Adapted from slides by Marty Stepp and Stuart Reges.
CSc 110, Spring 2017 Lecture 11: while Loops, Fencepost Loops, and Sentinel Loops Adapted from slides by Marty Stepp and Stuart Reges.
CSc 110, Autumn 2016 Lecture 14: Boolean Logic
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Fencepost loops reading: 4.1.
CSc 110, Spring 2018 Lecture 33: Dictionaries
CSc 110, Spring 2017 Lecture 9: Advanced if/else; Cumulative sum
CSc 110, Spring 2018 Lecture 16: Fencepost Loops and while loops
Topic 14 while loops and loop patterns
CSc 110, Spring 2018 Lecture 9: Parameters, Graphics and Random
CSc 110, Spring 2018 Lecture 17: while Loops and decomposition
Adapted from slides by Marty Stepp and Stuart Reges
Lecture 15: lists Adapted from slides by Marty Stepp and Stuart Reges
Lecture 22: lists Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2017 Lecture 9: Graphics and Nested Loops
CSc 110, Autumn 2016 Lecture 5: Loop Figures and Constants
CSc 110, Spring 2017 Lecture 4: Nested Loops and Loop Figures
CSc 110, Autumn 2016 Lecture 10: Advanced if/else; Cumulative sum
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2018 Lecture 13: Cumulative Sum and Boolean Logic
Lecture 19: lists Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2018 Lecture 5: Loop Figures and Constants
CSc 110, Spring 2018 Lecture 8: input and Parameters
CSc 110, Spring 2018 Lecture 10: More Graphics
CSc 110, Autumn 2016 Programming feel like that?
Loop problem Write a method printLetters that prints each letter from a word separated by commas. For example, the call: printLetters("Atmosphere") should.
Building Java Programs
CIS 110: Introduction to Computer Programming
CSc 110, Autumn 2017 Lecture 8: More Graphics
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
CSE 142, Spring 2013 Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 5.1 – 5.2.
Building Java Programs
Building Java Programs
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Presentation transcript:

CSc 110, Autumn 2017 Lecture 16: Fencepost Loops and Review Adapted from slides by Marty Stepp and Stuart Reges

A deceptive problem... Write a method print_letters that prints each letter from a word separated by commas. For example, the call: print_letters("Atmosphere") should print: A, t, m, o, s, p, h, e, r, e

Flawed solutions def print_letters(word): for i in range(0, len(word)): print(word[i] + ", ", end='') print() # end line Output: A, t, m, o, s, p, h, e, r, e, def print_letters(word): for i in range(0, len(word)): print(", " + word[i], end='') print() # end line Output: , A, t, m, o, s, p, h, e, r, e

Fence post analogy We print n letters but need only n - 1 commas. Similar to building a fence with wires separated by posts: If we use a flawed algorithm that repeatedly places a post + wire, the last post will have an extra dangling wire. for length of fence : place a post. place some wire.

Fencepost loop Add a statement outside the loop to place the initial "post." Also called a fencepost loop or a "loop-and-a-half" solution. place a post. for length of fence – 1: place some wire.

Fencepost function solution def print_letters(word): print(word[0]) for i in range(1, len(word)): print(", " + word[i], end='') print() # end line Alternate solution: Either first or last "post" can be taken out: def print_letters(word): for i in range(0, len(word) - 1): print(word[i] + ", ", end='') last = len(word) – 1 print(word[last]) # end line

Fencepost question Write a function print_primes that prints all prime numbers up to a max. Example: print_primes(50) prints 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 If the maximum is less than 2, print no output. To help you, write a function count_factors which returns the number of factors of a given integer. count_factors(20) returns 6 due to factors 1, 2, 4, 5, 10, 20.

Fencepost answer # Prints all prime numbers up to the given max. def print_primes(max): if (max >= 2): print("2", end='') for i in range(3, max + 1): if (count_factors(i) == 2): print(", " + str(i)) print() # Returns how many factors the given number has. def count_factors(number): count = 0 for i in range(1, number + 1): if (number % i == 0): count += 1 # i is a factor of number return count

Review question Write a function random_triangle that prints a triangle of the passed in string that is random height between 1 and 10. It should return the total number of stars printed. Example: random_triangle("*") might print * ** *** **** In this case it would return 10

Review question Write a function average_temp that prompts the user for temperatures and prints the average. The average should be rounded to one number after the decimal point. Example: average_temp() might print How many temperatures? 4 temperature? 92 temperature? 90 temperature? 85 temperature? 95 Average temperature: 90.5