Lecture 20 – Practice Exercises 4

Slides:



Advertisements
Similar presentations
Basics of Recursion Programming with Recursion
Advertisements

Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Recursion.
1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Programming with Recursion
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 9 Iteration: Recursion 5/02/09 Python Mini-Course: Day 3 - Lesson 9 1.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
How to start Visual Studio 2008 or 2010 (command-line program)
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
Chapter 7 Programming with Recursion. What Is Recursion? Recursive call A method call in which the method being called is the same as the one.
Introduction to programming in java Lecture 22 Arrays – Part 2 and Assignment No. 3.
Program Development and Design Using C++, Third Edition
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
Recursion Powerful Tool
Recursion.
CMPT 120 Topic: Python’s building blocks -> More Statements
Topic: Python’s building blocks -> Variables, Values, and Types
3.1 Fundamentals of algorithms
Topic: Recursion – Part 2
Topic: Functions – Part 1
Topic 6 Recursion.
Topic: Iterative Statements – Part 1 -> for loop
Introduction to Programming
Recursion DRILL: Please take out your notes on Recursion
Topic: Python’s building blocks -> Statements
Topic: Recursion – Part 1
Java 4/4/2017 Recursion.
Topic: Functions – Part 2
10.2 Implementation and Execution of Recursive Code
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
University of Washington Computer Programming I
Programming with Recursion
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
CMSC201 Computer Science I for Majors Lecture 16 – Recursion
While loops.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Coding Concepts (Basics)
Introduction to Programming
CS302 - Data Structures using C++
Basics of Recursion Programming with Recursion
Java Programming: Chapter 9: Recursion Second Edition
Yan Shi CS/SE 2630 Lecture Notes
Data Structures & Algorithms
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Programming with Recursion
Chapter 18 Recursion.
Introduction to Programming
Starter Activities GCSE Python.
CMPT 120 Lecture 3 - Introduction to Computing Science – Programming language, Variables, Strings, Lists and Modules.
CMPT 120 Lecture 12 – Unit 2 – Cryptography and Encryption –
Topic: Iterative Statements – Part 2 -> for loop
CMPT 120 Lecture 15 – Unit 3 – Graphics and Animation
CMPT 120 Lecture 6 – Unit 1 – Chatbots
CMPT 120 Lecture 13 – Unit 2 – Cryptography and Encryption –
Lecture 31 – Practice Exercises 7
CMPT 120 Lecture 19 – Unit 3 – Graphics and Animation
Lecture 17 – Practice Exercises 3
Lecture 20 – Practice Exercises 4
Lecture 17 – Practice Exercise 3 SOLUTIONS
Lecture 6 - Recursion.
Presentation transcript:

Lecture 20 – Practice Exercises 4 CMPT 120 Lecture 20 – Practice Exercises 4

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

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

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

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

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

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( )).

Question 2 Problem Statement: Possible solution: https://repl.it/repls/WorriedNavajowhiteLicenses 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

Question 3 Problem Statement: Possible solution: https://repl.it/repls/WorriedNavajowhiteLicenses 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