CSC110 Lecture 4_ Defining Functions

.pdf

School

University of Toronto *

*We aren’t endorsed by this school

Course

110

Subject

Computer Science

Date

May 17, 2024

Type

pdf

Pages

9

Uploaded by CommodoreKnowledgeGoose32 on coursehero.com

CSC110 Lecture 4: Defining Functions Print this handout Exercise 1: Reviewing the parts of a function definition You are given the following function definition of a Python function. Answer the following questions about this definition. 1. What is the function name in this definition? 2. What is the function header in this definition? 3. How many parameters does this function have? What is the name and type of each parameter? 4. What is the function’s return type ? def calculate(x: int, y: int) -> list: """Return a list containing the sum and product of the two given numbers. """ return [x + y, x * y]
5. What is the part surrounded by triple-quotes ( """ ) called? What is its purpose? 6. What is the function body in this definition? 7. Compared to the example function definitions we looked at in lecture, what part is missing from this function definition? 8. Write down what you would add to complete this function definition. Exercise 2: The Function Design Recipe For your reference, here are the steps of the Function Design Recipe : 1. Write examples uses 2. Write the function header 3. Write the function description 4. Implement the function body 5. Test the function 1. Consider the following problem description: Given two lists, return whether they have the same length. We have started the Function Design Recipe for you. Complete the code below to solve this problem by following the steps of the Function Design Recipe.
2. Now consider this problem description: Given a float representing the price of a product and another float representing a tax rate (e.g., a 10% tax rate represented as the float value 0.1 ), calculate the after-tax cost of the product, rounded to two decimal places. Again, complete the code below to solve this problem by following the steps of the Function Design Recipe. 3. Finally, consider this problem description: Given a list of product prices (as float s) and a tax rate, calculate the total after-tax cost of all the products in the list rounded to two decimal places. To keep things simple, don’t worry about any rounding errors in your calculation. def is_same_length(list1: list, list2: list) -> bool: """ >>> is_same_length([1, 2, 3], [4, 5, 6]) True >>> is_same_length([1, 2, 3], []) False """ """ >>> after_tax_cost(5.0, 0.01) 5.05 """
This time, we haven’t provided you any code—follow the Function Design Recipe to write a complete function that solves this problem. Tip : to keep your doctest examples numerically simple, try using a tax rate of 0.5 or 1.0 . Exercise 3: Practice with methods Note : you might want to use Appendix A.2 Python Built-In Data Types Reference (https://www.teach.cs.toronto.edu/~csc110y/fall/notes/A-python-builtins/02-types.html) as a reference in this exercise. 1. Suppose we have executed the following assignment statements in the Python console: Write down what each of the following expressions evaluate to. Do this by hand first! (Then check your work in the Python console. Don’t worry if you wrote set elements in a different order that what appears in the Python console.) >>> wish = 'Happy Birthday' >>> set1 = { 1 , 2 , 3 } >>> set2 = { 2 , 4 , 5 , 10 } >>> strings = [ 'cat' , 'rabbit' , 'dog' , 'rabbit' ]
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help