Learning to Program in Python

Slides:



Advertisements
Similar presentations
A Level Computing#BristolMet Session Objectives#U2 S8 MUST identify the difference between a procedure and a function SHOULD explain the need for parameters.
Advertisements

© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Recursion.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Program Design and Development
Copyright © 2003 Pearson Education, Inc. Slide 1.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Algorithmic Recursion. Recursion Alongside the algorithm, recursion is one of the most important and fundamental concepts in computer science as well.
M180: Data Structures & Algorithms in Java
Recursion.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
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.
Algorithms Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin,
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Recursion Version 1.0.
Computer Programming.
Fundamentals of Python: First Programs
Topic: Recursion – Part 2
Topic 6 Recursion.
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Recursion DRILL: Please take out your notes on Recursion
ALGORITHMS AND FLOWCHARTS
Java 4/4/2017 Recursion.
Programming Mehdi Bukhari.
Chapter 9 Recursion.
Topic: Functions – Part 2
Learning to Program in Python
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Learning to Program in Python
Learning to Program in Python
Learning to Program in Python
Applied Algorithms (Lecture 17) Recursion Fall-23
Learning to Program in Python
Learning to Program in Python
Learning to Program in Python
Unit# 9: Computer Program Development
Learning to Program in Python
Learning to Program in Python
Learning to Program in Python
Learning to Program in Python
Learning to Program in Python
Recursion Chapter 11.
Learning to Program in Python
Learning to Program in Python
Learning to Program in Python
Learning to Program in Python
Learning to Program in Python
Coding Concepts (Basics)
Chapter 5 Loops.
Learning to Program in Python
Global Challenge Walking for Water Lesson 2.
Global Challenge Walking for Water Lesson 2.
Global Challenge Walking for Water Lesson 2.
Global Challenge Walking for Water Lesson 2.
Global Challenge Walking for Water Lesson 2.
Chapter 18 Recursion.
Global Challenge Walking for Water Lesson 2.
Global Challenge Walking for Water Lesson 2.
Global Challenge Walking for Water Lesson 2.
Global Challenge Walking for Water Lesson 2.
Global Challenge Walking for Water Lesson 2.
CMPT 120 Lecture 19 – Unit 3 – Graphics and Animation
Presentation transcript:

Learning to Program in Python Concept 7 DEFINITIONS (Recursive) Recursive definitions are only on the Higher Level course. - The CT challenge requiring a recursive function, Paths and Nodes, is designated as HL. This requires Turtles to follow nodes on a graph to see if the path can be walked without repeating any nodes. - The Die Hard Water Beakers CT Challenge is also designated HL, though this is more for the complexity of the problem and the UI, since recursive functions are not necessary to solve this particular challenge.

Learning Intentions From this lesson the students will be able to: Write a program that calls itself. A recursive function. Define when a recursive function should be used. Use a recursive function to find the factorial of any integer. Recursive functions are only on the HL course. LO 2.9 students should be able to assemble existing algorithms or create new ones that use functions (including recursive), procedures, and modules

recall from FUNCTIONS We can pass in as many parameters to a function as we like: def fancyNumberOfInputs(a, b, c, x, y, z) BUT we can only ever have one (if any) return from a function. A quick recap on how to write definitions, and define inputs and outputs.

Recall this definition The command return sends back the variable to wherever the function was called. (usually in the main program) What data type will be returned from the halveMe function? It is a float because of the division by 2. The return command is essential to an understanding of the operation of recursive functions.

halveME in a LOOP The PRIMM method will encourage engagement. (Predict Run Investigate Modify Make) Encourage students to use pen and paper to write the output from each successive iteration of the while loop. There is a hint of the concept of recursion in this program, at the line : counter = halveME(counter). Beginning with 1024, predict the output of this program as it goes through each iteration of the loop. LO 1.6 students should be able to explain the operation of a variety of algorithms

halveME in a LOOP But what if the definition halveME called itself? So instead of returning halfMyNumber we wrote : halveME(halfMyNumber) ? A recursive definition is one that calls itself. Some of the questions are: When does it ever return a value? When does it stop calling itself?

Call Myself Using pen and paper, predict the output. Write the code and Let the output run. You will (eventually) get an error telling you there are too many recursions. How would you stop the program going below a value of 1? Try it out. In theory, the function should keep halving the starting number. Let the output run. You will (eventually) get an error telling you there are too many recursions. What is the exact wording of the error? PRIMM Encourage the students to attempt to include a terminating condition when myNumber goes below a value of 1. (You can interrupt the program as it runs with ctrl-c)

CALL and STOP Myself A recursive definition calls itself. A terminating condition makes it stop calling itself at some point. The terminating condition here is to check if myNumber is at the value of 1. If it is 1, the definition returns a number and stops calling itself. (The while loop could equally have been used here to control the terminating condition.) PRIMM Encourage the students to attempt to modify the terminating condition when myNumber goes below a value of say 0.01? (You can interrupt the program as it runs with ctrl-c)

A Basic Recursive Function A recursive function can often be used when: A problem can be broken down into an identical, just smaller, problem. (A sub-problem) There is a definite terminating condition. In other words as the sub-problems get smaller, there is eventually a definite answer. The sub-problem cannot be broken down any further. Also called a Base Case. Recursive Functions are only on the HL section of the course. LO 2.9 students should be able to assemble existing algorithms or create new ones that use functions (including recursive), procedures, and modules

A Basic Recursive Function Investigate the program on the next slide. Can you see the 2 characteristics of a problem solved using recursion? Students should be looking out for decomposition and a terminating (or stopping) condition. LO 1.3 students should be able to solve problems by deconstructing them into smaller units using a systematic approach in an iterative fashion

Sample Code Problem -> Sub-Problem ? Terminating Condition ? The PRIMM method will encourage engagement. (Predict Run Investigate Modify Make)

The FACTORIAL of an integer! The factorial of 3 is 3 x 2 x 1. The factorial of 6 is 6 x 5 x 4 x 3 x 2 x 1. The symbol for factorial n is n! 3! = 6. 6! = 720. n! = nx(n-1)x(n-2)….3x2x1 The PRIMM method will encourage engagement. (Predict Run Investigate Modify Make) Encourage students to use pseudocode to solve this problem, both non-recursively and recursively. LO 2.5 students should be able to use pseudo code to outline the functionality of an algorithm

CHALLENGE Write a program that Calculates the factorial of a number without using a recursive definition. Then write a program using a recursive definition. Encourage students to tackle the problem with both solutions : LO 1.2 students should be able to explain how the power of computing enables different solutions to difficult problems. Also, encourage a program in pseudocode first before programming the solution in Python. LO 2.5 students should be able to use pseudo code to outline the functionality of an algorithm

Sample Factorial Program non-recursive This is merely suggestive of code to calculate and print the factorial of a number. Encourage students to modify their programs, including this sample code. LO 1.2 students should be able to explain how the power of computing enables different solutions to difficult problems

Sample Factorial Program recursive This is merely suggestive of code to calculate and print the factorial of a number. Encourage students to modify their programs, including this sample code. LO 1.2 students should be able to explain how the power of computing enables different solutions to difficult problems

goes forwards and backwards CHALLENGE Write a function that Takes a word (string) from the user. Checks if the word is a Palindrome, non-recursively Does this recursively! There is much be gained from students executing this task both recursively and non-recursively. A cool way to reverse a string or list is : revPhrase = userPhrase[ : : -1] …… let the students figure out their own ways first. RACECAR goes forwards and backwards LO 2.20 students should be able to identify and fix/debug warnings and errors in computer code and modify as required

CT Challenge… Paths and Nodes Your task is to use a recursive function to figure out how to cross the graph, from node1 (N1) to node6 (N6). As the Turtles follow the nodes on the graph, the challenge is to find paths that can be walked without repeating any nodes. This is a difficult CT challenge on the ncca website, aligned to the concepts and tasks in this section (and on a html platform) Students should be encouraged to decompose the program and build up the functionality from working code. Students must write the algorithm first. The CT Challenge folder (html file) will guide the students through the process. The Recursive definitions are only on the Higher Level course. - The CT challenge requiring a recursive function, Paths and Nodes, is designated as HL. This requires Turtles to follow nodes on a graph to see if the path can be walked without repeating any nodes. - The Die Hard Water Beakers CT Challenge is also designated HL, though this is more for the complexity of the problem and the UI, since recursive functions are not necessary to solve this particular challenge.

Lesson Review As a result of this lesson: I can write a program that calls itself. A recursive function. I can define when a recursive function should be used. I have written recursive definitions for a variety of different problems and contexts. Recursive functions are HL material. LO 1.5 students should be able to evaluate alternative solutions to computational problems