Com S 127x - Lab 6 1. READING FLOWCHARTS WITH CONDITIONAL ACTIONS!

Similar documents
(I m not printing out these notes! Take your own.)

Using IDLE for

Use the Associative Property of Multiplication to find the product.

CMSC 201 Fall 2018 Lab 04 While Loops

Rev Name Date. . Round-off error is the answer to the question How wrong is the rounded answer?

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

To become familiar with array manipulation, searching, and sorting.

Review. Input, Processing and Output. Review. Review. Designing a Program. Typical Software Development cycle. Bonita Sharif

Python lab session 1

CS1 Lecture 3 Jan. 22, 2018

Text Input and Conditionals

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

IDENTIFY WAYS OF REPRESENTING ALGORITHMS.

CMSC 201 Spring 2017 Lab 12 Recursion

Downloaded from Chapter 2. Functions

15-110: Principles of Computing, Spring 2018

CS1 Lecture 5 Jan. 26, 2018

PIETRO, GIORGIO & MAX ROUNDING ESTIMATING, FACTOR TREES & STANDARD FORM

Python for Non-programmers

Chapter 2 Input, Processing and Output. Hong Sun COSC 1436 Spring 2017 Jan 30, 2017

CS1 Lecture 3 Jan. 18, 2019

T H E I N T E R A C T I V E S H E L L

CS1 Lecture 5 Jan. 25, 2019

rrr Cu 'Vt.-1 cal l-c.-<_ {CL/o,_

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING

CCBC Math 081 Order of Operations Section 1.7. Step 2: Exponents and Roots Simplify any numbers being raised to a power and any numbers under the

Lab 4: Strings/While Loops Due Sun, Apr 17 at midnight

Introduction to Programming in Turing. Input, Output, and Variables

SECTION 3. ROUNDING, ESTIMATING, AND USING A CALCULATOR

BRANCHING if-else statements

Decisions, Decisions. Testing, testing C H A P T E R 7

Programming with Python

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Unit 7: Algorithms and Python CS 101, Fall 2018

Part 6b: The effect of scale on raster calculations mean local relief and slope

CMSC201 Computer Science I for Majors

Question 1. Part (a) Simple Syntax [1 mark] Circle add_ints(), because it is missing arguments to the function call. Part (b) Simple Syntax [1 mark]

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

Math 6 Notes Unit 03 Notes: Decimals

CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point?

FOIA Request made by: Name: Address: Telephone: Request made on date: Library received request on date: Library staff who received request:

Integer Multiplication and Division

cs1114 REVIEW of details test closed laptop period

Real Numbers. Rational Numbers (0, 3, -1, ½⅔,.524, etc..) Fractions (1/2, -4/3, 10%,.25, etc..) Negative Integers {.

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 5. Repetition in Programs. Notes. Notes. Notes. Lecture 05 - Loops

CMSC 201 Spring 2017 Lab 05 Lists

Lecture Notes for Chapter 2: Getting Started

6.S189 Homework 2. What to turn in. Exercise 3.1 Defining A Function. Exercise 3.2 Math Module.

FLOW CHART AND PSEUDO CODE

CS 6353 Compiler Construction Project Assignments

Common Loop Algorithms 9/21/16 42

Lab 1: Input, Processing, and Output This lab accompanies Chapter 2 of Starting Out with Programming Logic & Design.

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

CS 115 Lecture 4. More Python; testing software. Neil Moore

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes

Solving Equations with Inverse Operations

Conditional Expressions and Decision Statements

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

Com S 127 Lab 2. For the first two parts of the lab, start up Wing 101 and use the Python shell window to try everything out.

Project 2: After Image

Ph3 Mathematica Homework: Week 1

Get to Know Your Calculator!

CHAPTER 4: DECIMALS. Image from Microsoft Office Clip Art CHAPTER 4 CONTENTS

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Mark Important Points in Margin. Significant Figures. Determine which digits in a number are significant.

The while Loop 4/6/16 4

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

Learning the Language - V

Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

CMSC 201 Fall 2016 Lab 13 More Recursion

Signed umbers. Sign/Magnitude otation

Programming for Engineers Introduction to C

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved

CS 1110, LAB 3: MODULES AND TESTING First Name: Last Name: NetID:

Data Representation Type of Data Representation Integers Bits Unsigned 2 s Comp Excess 7 Excess 8

Variables, expressions and statements

CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection

IT 1033: Fundamentals of Programming Loops

Lab 4 Fruitful Functions

An Introduction to Python

Python Programming Exercises 1

CS 115 Exam 3, Fall 2011

CMSC 201 Spring 2019 Lab 06 Lists

CSCI 204 Introduction to Computer Science II

Beginning and Intermediate Algebra Chapter 0: Arithmetic

Gateway Regional School District VERTICAL ARTICULATION OF MATHEMATICS STANDARDS Grades K-4

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

Imelda C. Go, South Carolina Department of Education, Columbia, SC

Part III Appendices 165

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

C++ Programming Language Lecture 2 Problem Analysis and Solution Representation

Only to be used for arranged hours. Order of Operations

Getting Started with Python

CSCE150A. Introduction. While Loop. Compound Assignment. For Loop. Loop Design. Nested Loops. Do-While Loop. Programming Tips CSCE150A.

Intermediate Algebra. Gregg Waterman Oregon Institute of Technology

Office 2016 Excel Basics 09 Video/Class Project #21 Excel Basics 9: Number Formatting and the ROUND Function

CT 229 Fundamentals of Java Syntax

Introduction to: Computers & Programming: Review prior to 1 st Midterm

Transcription:

Com S 127x - Lab 6 1. READING FLOWCHARTS WITH CONDITIONAL ACTIONS! The U.S. Postal Service has some complex rules for deciding how much it costs to mail a letter. We ll use a small part of these rules as an example of writing conditional statements. The rule is as follows: Rule 1: For an ordinary letter up to 3.5 ounces, the cost is.49 for the first ounce plus.22 for each additional ounce (or part of an ounce). Try some examples: A letter that is 1 ounce (or less) is just.49. For a 2.3 ounce letter, the cost is.49 + 2 *.22 =.93(.49 for the first ounce and for the remaining 1.3 ounce it is 2 *.22) For a 3 ounce letter the cost is also.49 + 2 *.22 =.93 For a 3.1 ounce letter the cost is.49 + 3 *.22 = 1.15 The process above could be described in pseudocode something like this: if the weight is less than or equal to 1.0 cost is.49 else subtract 1 from the weight, round up to the next whole number, and multiply by.22 add the result to.49 To translate this into Python code, we need to be able to take a float value and round "up" to the next whole number. Fortunately there is a function in the math module, called ceil() (short for ceiling ) that does exactly what we want. Try this: >>> import math >>> math.ceil(2.3) 3.0 >>> math.ceil(3.0) 3.0 >>> math.ceil(3.1) 4.0 So here is an example of a Python function that returns the postage for a given weight up to 3.5 ounces. (To try this in a script, remember to import the math module.) def postage(weight): if weight <= 1.0: else: + math.ceil(weight - 1) *.22

Now, to make things more interesting, let's add the rule for letters over 3.5 ounces: Rule 2: For letters over 3.5 ounces, the cost is.98 for the first ounce plus.22 for each additional ounce (or part of an ounce). For example, a 3.5 ounce letter would cost.49 + (3 *.22) = 1.15 [From rule 1] a 3.8 ounce letter would cost.98 + (3 *.22) = 1.64 [From rule 2] a 10 ounce letter would cost.98 + (9 *.22) = 2.96 [From rule 2] Now to calculate the charges for letter of any weight, you will have to make use of both the rules and calculate the charges. Notice you have to check the actual weight to see if it is bigger than 3.5, but then round up when you compute the extra ounces. Let us try to capture the above rules in a flowchart. Figures 1, 2, and 3 are flowcharts representing different attempts to program the rules above. They are not necessarily correct! Exercise 1 Trace through each flowchart by hand using some sample values as in the examples above. Decide which flowcharts are correct and which are incorrect. For each one that is incorrect, give a specific example in which the wrong answer is produced. Show the TA what you concluded. WRITING CONDITIONAL STATEMENTS; USING A UNIT TEST Now, for each flowchart, write a Python function that implements it. The function should be named postage and should have one parameter representing the weight. The function should return the postal charge calculated. Put the three functions in three separate files called postage1.py, postage2.py, and postage3.py. To test your functions, download the module postage_test.py from http://web.cs.iastate.edu/~adisak/cs127x/lab6/postage_test.py This is an example of a unit test.

WHAT IS A UNIT TEST It is just a short script that calls the postage() function multiple times with different values, so you can check the results. To make it easier to check the results, the expected (correct) result is printed after function is called. You can choose which version of the function is being tested by editing the import statement at the top. Verify that the results agree with your conclusions from reading the flowcharts. (Remember, agreeing with the flowchart does not necessarily mean it gives correct answers!) Reminders: 1. Your modules need to be in the same directory as the postage_test script in order to be imported. 2. Remember that Python uses indentation to determine which statements are grouped together, so watch the indentation levels carefully. For example, here is yet another version of a postagecalculating function (one that does not match any of the flowcharts) that has another conditional statement nested inside the else statement block: def postage(weight): if weight > 3.5: cost =.98 + math.ceil(weight - 1) *.22 else: if weight > 1: cost = cost + math.ceil(weight - 1) *.22 Indentation level for inner if block Indentation level for outer if and else blocks Indentation level for function body Exercise 2 Show the TA your three modules and the results of running the unit test on each version. Note that when you start testing you might see values like: 0.93000000000000003 for the postage on a 3 ounce letter. Why do we get that weird-looking number instead of just.93

Well, floating-point arithmetic is never exact, so we are seeing the natural result of rounding errors. All we can do here is mentally round the result to the nearest cent. (An alternative would be to do all calculations in pennies using integer arithmetic.) Start weight <= 1 weight <= 3.5 + ceil(weight 1) *.22 cost =.98 + ceil(weight 1) *.22 Figure 1

Start weight <= 1 weight > 1 + ceil(weight 1) *.22 weight > 3.5 cost =.98 + ceil(weight 1) *.22 + ceil(weight 1) *.22 Figure 2

Start cost = cost + ceil(weight 1) *.22 weight > 1 cost = cost +.49 weight > 3.5 Figure 3 CREATING A FLOWCHART FROM A PROBLEM STATEMENT Now we are going to construct a flowchart for a problem. The problem statement is as follows: Normally, labor laws require that employees who are paid an hourly wage have to be paid extra whenever they work more than 40 hours per week. The extra is usually 1½ times the regular hourly wage for all hours above 40. However, an exception is made for employees with exempt status, which is meant for positions such as managers and supervisors. They don t get paid any extra for overtime.

Work out the examples below using a pencil and paper, and make sure you get the results shown: 1. 20 hours, rate $10.00 per hour, non-exempt : total wages $200 2. 20 hours, rate $10.00 per hour, exempt : total wages $200 3. 44 hours, rate $10.00 per hour, non-exempt : total wages $460 4. 44 hours, rate $10.00 per hour, exempt : total wages $440 Next, sketch a flowchart representing the calculation. Assume that you have three variables, the hours, the hourly rate, and the status (assume the status is either the string exempt or the string non-exempt ). Finally, implement a Python function that has three parameters (again the hours, the hourly rate, and the status) that prints the total wages. Also write a short unit test for it similar to postage_test.py, based on the four sample calculations above. You should print what arguments are being tested, the expected result, and then the result you get from actually calling the function. Exercise 3 Show your flowchart and demonstrate your Python function to the TA.