Download presentation
Presentation is loading. Please wait.
1
Lecture 20 – Practice Exercises 4
CMPT 120 Lecture 20 – Practice Exercises 4
2
Weekly Exercise 7 - Feedback
Feedback on our algorithms of last Friday! Question 1 - Problem Statement: Given 3 numbers, find the largest one. Solution – Algorithm: Get three numbers -> number1, number2, number3 Set maxNumber to number1 if number2 > maxNumber Set maxNumber to number2 if number3 > maxNumber Set maxNumber number3 To help you create a solution, use examples. Using 15, 30, 45, here are all possible permutations: number1 number2 number3 15 30 45
3
Weekly Exercise 7 - Feedback
These are NOT acceptable answers: Python program A solution that says: Get 3 numbers Compare these 3 numbers and select the largest Display/print the largest Assign 3 numbers to variables Include functions that convert the user’s response into a number Create conditions that seeks out the largest value out of the 3 Give the user 3 numbers: 3, 8, 2 Ask the user which number is the largest Let the user know whether s/he has selected correctly
4
Goals for Today! Get better acquainted with recursion by box tracing the execution of a recursive program Practise designing a solution (algorithm) to a problem Recursive as well as non-recursive solution Practise implementing a program (solution) described by an algorithm
5
Question 1 Go to https://repl.it/repls/EthicalSaddlebrownPentagon
Fork this link (press on the Fork button on the left of the Run button) -> what does this do? and answer the questions on the given sheet (next 2 slides) In order to answer these questions, you may wish to box trace the code by either Copying the program into the Python Code Visualizer and visualizing its execution OR Hand trace the execution of the program by drawing your own boxes (box tracing) Either way, the goal is to represent using a box the execution of the function every time it is called (called “frames” in the Python Code Visualizer) and use this box to keep track of the values of local variables, parameters and returned value
6
Name: ________________________________
Student number: ______________________ Question 1 1. What does the program produce (print on the screen) when it has completely executed? 2. What is the purpose of func(…)? Be as specific as possible. 3. What is the value of the argument of func(…) when it is called for the 3rd time? Remember: an argument is the value you put within the ( ). 4. What is the data type of the value the function func(…) returns? 5. What is the base case of the function func(…)? The base case is (occurs) when if len(a) > 0 : is false. Then, as part of the base case, the else and the return statements are executed Result: 3 in [5, 3, 8, 9] It returns the number of odd integers in its parameter a. a -> [8, 9] int (i.e., integer) Base case: if len(a) > 0 : ... else : b = 0 return b
7
Question 1 6. What value does the 6th execution of func(…) return?
7. What happens when the recursive case of func(…) is reached? 8. What happens when the base case of func(…) is reached? 9. What is the value of a[0] during the 4th execution of func(…)? 10. Which execution of func(…) adds the value 2 to the value 1? There is no 6th execution of func(…) . func( ) calls itself with a list that contains all elements of its parameter a except the first element. This way, the argument to func( ) is getting smaller at every call and eventually reaches the length 0. that is when the base case is reached. func( ) assigns the value 0 to the variable b and returns the value of b. The value of a[0] is 9. The value 2 is added to 1 during the first execution of func( ), i.e., the first time we call func( )).
8
Question 2 Problem Statement:
Possible solution: Problem Statement: Write a Python function that reverse a string iteratively -> using a loop (iteration) as opposed to recursion How to proceed – use Repl.it: Write a header Design your solution (algorithm) and write it as comment in your program Write down, as comments, the test cases you will use to test your program Translate your comments into Python statements Execute and test your program
9
Question 3 Problem Statement:
Possible solution: Problem Statement: Write a Python function that reverse a string recursively How to proceed – use Repl.it: As part of the header Write down the recursive description of a string (The reason you are asked to do this is to make clear in your mind the recursive nature of a string) As a comment in your program Write down the base case Write down how the string is getting smaller at every recursive call Write down the test cases you will use to test your program - use the test cases from Question 2 Then write your program and test it
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.