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.
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.
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
Recursion, Complexity, and Sorting By Andrew Zeng.
RecursionRecursion Recursion You should be able to identify the base case(s) and the general case in a recursive definition To be able to write a 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.
30/10/ Iteration Loops Do While (condition is true) … Loop.
For Loop GCSE Computer Science – Python. For Loop The for loop iterates over the items in a sequence, which can be a string or a list (we will discuss.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
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
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
BIT116: Scripting Loops.
Topic: Python’s building blocks -> Statements
Topic: Python’s building blocks -> Variables, Values, and Types
CMPT 120 Topic:  Case Study.
Topic: Recursion – Part 1
CMPT 120 Topic: Functions – Part 4
Introduction to Programming
Topic: Functions – Part 2
Functions.
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.
REBOL Writing Functions.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Call Stacks, Arguments and Returns
Learning to Program in Python
Learning to Program in Python
Procedures.
Passing Parameters by value
Nate Brunelle Today: Functions again, Scope
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
Introduction to Programming
CS302 - Data Structures using C++
Basics of Recursion Programming with Recursion
Creative Commons Attribution Non-Commercial Share Alike License
Yan Shi CS/SE 2630 Lecture Notes
Data Structures & Algorithms
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Chapter 18 Recursion.
Running a Java Program using Blue Jay.
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
Introduction to Programming
Starter Activities GCSE Python.
CMPT 120 Lecture 2 - Introduction to Computing Science – Problem Solving, Algorithm and Programming.
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 4 – Unit 1 – Chatbots
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
CMPT 120 Lecture 18 – Unit 3 – Graphics and Animation
Lecture 20 – Practice Exercises 4
Lecture 17 – Practice Exercise 3 SOLUTIONS
Python Creating a calculator.
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(…)?

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?

Question 2 Problem Statement: Write a Python function that reverse a string iteratively -> using a loop (iteration) as opposed to recursion Requirements: You cannot call an already existing Python function that would do the reversing for you 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: 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