110 likes | 263 Views
COMPSCI 101 Principles of Programming. Lecture 06 – Testing Functions. Learning outcomes. At the end of this lecture, students should be able to: Divide a problem up into different tasks Write a function to perform each task Create a solution to the problem using multiple functions. Recap.
E N D
COMPSCI 101Principles of Programming Lecture 06 – Testing Functions
Learning outcomes • At the end of this lecture, students should be able to: • Divide a problem up into different tasks • Write a function to perform each task • Create a solution to the problem using multiple functions COMPSCI 101 - Principles of Programming
Recap • We use test cases to determine if our code works • We automate the testing procedure where possible • Python provides Doctest as a way to automate testing • Doctest consists of two parts defsquare_area(side): """Returns the area of a square … documentation goes here >>> square_area(10) 100 >>> square_area(5.5) 30.25 """ return side ** 2 import doctest doctest.testmod() test cases function call that performs the tests COMPSCI 101 - Principles of Programming
Converting between types • Converting from integer (or string) to float • float( value ) • Converting from float (or string) to integer • int ( value ) • Converting from float or integer to string • str( value ) Math functions to convert float to an int • math.floor( value ) #returns the closest int <= value • math.ceil( value ) #returns the closest int >= value COMPSCI 101 - Principles of Programming
Developing a solution using functions • Two examples: • Calculate the weight of a steel tube, given the dimensions • Calculate the number of trucks required to ship a given volume of coffee beans COMPSCI 101 - Principles of Programming
Calculate the weight of a steel tube • First calculate the amount (volume) of steel • Multiply the volume by the density (weight per square metre) • Q1. How do we calculate the volume of a tube? • Calculate the area of the cross-section and multiply by the length • Q2. What is the density of steel? • Google? • Information needed: • exterior radius • interior radius • length of the tube • density of steel COMPSCI 101 - Principles of Programming
Cross-section of a tube (annulus) COMPSCI 101 - Principles of Programming
Development steps Calculate the weight of a steel tube Calculate the volume of the tube Multiply by the density Calculate the area of the cross-section Multiply by the length Calculate the area of the outer circle Calculate the area of the inner circle Subtract the inner from the outer COMPSCI 101 - Principles of Programming
Start with the building blocks • Start with a function that doesn't rely on other functions • Build up other functions using the pieces you have created • Area of a circle COMPSCI 101 - Principles of Programming
Example • Given the size of a box used to hold coffee beans, and the capacity of a truck used to carry the boxes, calculate the number of trucks required to ship a given volume of coffee beans. • box size: width, height, depth in centimetres • truck capacity: width, height, depth in metres • volume of coffee beans COMPSCI 101 - Principles of Programming
Development steps Calculate the number of trucks Need to know the total number of boxes Need to know how many boxes fit into one truck Calculate the number of boxes Need to know the dimensions of the box Need to know the total volume Calculate the number of boxes per truck Need to know the dimensions of the box Need to know the dimensions of the truck COMPSCI 101 - Principles of Programming