DM550/DM857 Introduction to Programming. Peter Schneider-Kamp

Similar documents
DM502 Programming A. Peter Schneider-Kamp.

DM536 / DM550 Part 1 Introduction to Programming. Peter Schneider-Kamp.

DM536 Introduction to Programming. Peter Schneider-Kamp.

DM502 Programming A. Peter Schneider-Kamp.

DM536 Introduction to Programming. Peter Schneider-Kamp.

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

DM550/DM857 Introduction to Programming. Peter Schneider-Kamp

DM503 Programming B. Peter Schneider-Kamp.

DM536 Introduction to Programming. Peter Schneider-Kamp.

RECURSION 3. 1 Recursion COMPUTER SCIENCE 61A. June 30, 2016

Recursion CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2011

ITERATION AND RECURSION 3

Recursion. Overview. Mathematical induction. Hello recursion. Recursion. Example applications. Goal: Compute factorial N! = 1 * 2 * 3...

RECURSION, RECURSION, (TREE) RECURSION! 3

ENVIRONMENT DIAGRAMS AND RECURSION 2

Environment Diagrams and Recursion Fall 2017 Discussion 2: September 6, 2017 Solutions. 1 More Environment Diagrams

RECURSION: n. SEE RECURSION 3

Recursion. Fundamentals of Computer Science

RECURSION, RECURSION, (TREE) RECURSION! 3

RECURSION, RECURSION, (TREE) RECURSION! 2

ENVIRONMENT DIAGRAMS AND RECURSION 2

CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion

Iteration. # a and b are now equal # a and b are no longer equal Multiple assignment

Conditionals: Making Choices

RECURSION 7. 1 Recursion COMPUTER SCIENCE 61A. October 15, 2012

CS1 Recitation. Week 2

DM537 Object-Oriented Programming. Peter Schneider-Kamp.

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

Functions and Recursion

Answers to review questions from Chapter 2

Python lab session 1

CS1 Lecture 15 Feb. 19, 2018


Admin. How's the project coming? After these slides, read chapter 13 in your book. Quizzes will return

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

Control structure: Repetition - Part 2

Control Structures 1 / 17

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Identify recursive algorithms Write simple recursive algorithms Understand recursive function calling

Chapter 5: Recursion

This session. Recursion. Planning the development. Software development. COM1022 Functional Programming and Reasoning

At full speed with Python

Algorithm Design and Recursion. Search and Sort Algorithms

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

DM536 / DM550 Part 1 Introduction to Programming. Peter Schneider-Kamp.

ENGR 102 Engineering Lab I - Computation

Department of Mathematical Sciences, FAU. Instructions:

11.3 Function Prototypes

Software Development. Modular Design and Algorithm Analysis

Recursion CS GMU

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Measuring Efficiency

APCS-AB: Java. Recursion in Java December 12, week14 1

Loops / Repetition Statements

Emil Sekerinski, McMaster University, Winter Term 16/17 COMP SCI 1MD3 Introduction to Programming

Outline. Simple Recursive Examples Analyzing Recursion Sorting The Tower of Hanoi Divide-and-conquer Approach In-Class Work. 1 Chapter 6: Recursion

More Complicated Recursion CMPSC 122

34. Recursion. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Fundamentals of Programming (Python) Getting Started with Programming

An introduction to Scheme

Variable and Data Type I

Outline. 1 If Statement. 2 While Statement. 3 For Statement. 4 Nesting. 5 Applications. 6 Other Conditional and Loop Constructs 2 / 19

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

CSCI 121: Recursive Functions & Procedures

Standard Version of Starting Out with C++, 4th Edition. Chapter 19 Recursion. Copyright 2003 Scott/Jones Publishing

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

CONTROL AND HIGHER ORDER FUNCTIONS 1

2.3 Recursion. Overview. Mathematical Induction. What is recursion? When one function calls itself directly or indirectly.

CS1 Lecture 5 Jan. 25, 2019

COMP322 - Introduction to C++ Lecture 01 - Introduction

Data structure and algorithm in Python

The Practice of Computing Using PYTHON. Chapter 2. Control. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Reals 1. Floating-point numbers and their properties. Pitfalls of numeric computation. Horner's method. Bisection. Newton's method.

Intro. Scheme Basics. scm> 5 5. scm>

CS 112: Intro to Comp Prog

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

Flow Control: Branches and loops

Constructing Algorithms and Pseudocoding This document was originally developed by Professor John P. Russo

RECURSION AND LINKED LISTS 3

CMPT 120 Control Structures in Python. Summer 2012 Instructor: Hassan Khosravi

CSE100 Principles of Programming with C++

Recursive Definitions

CS 61A Higher Order Functions and Recursion Fall 2018 Discussion 2: June 26, 2018 Solutions. 1 Higher Order Functions. HOFs in Environment Diagrams

CS1 Lecture 5 Jan. 26, 2018

LOOPS. Repetition using the while statement

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

CS 105 Lab As a review of what we did last week a. What are two ways in which the Python shell is useful to us?

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

Fruitful Recursion (recursion that returns a value)

21-Loops Part 2 text: Chapter ECEGR 101 Engineering Problem Solving with Matlab Professor Henry Louie

TECH. Recurrence Equations vs. Recursive Procedures. Recursion and Induction. e.g. Fibonacci Function. The working of recursive procedure

Chapter 7. Iteration. 7.1 Multiple assignment

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

HW3: CS 110X C Domain Information. Final Version: 1/29/2014

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Exercise 1 ( = 24 points)

Repetition Through Recursion

What is Iteration? CMPT-101. Recursion. Understanding Recursion. The Function Header and Documentation. Recursively Adding Numbers

Transcription:

DM550/DM857 Introduction to Programming Peter Schneider-Kamp petersk@imada.sdu.dk http://imada.sdu.dk/~petersk/dm550/ http://imada.sdu.dk/~petersk/dm857/

Project Qualification Assessment first assessment on Monday, September 18, 12:15-14:00 3 assessments in total sum of points from all 3 assessments at least 50% of total in class assessment using your own computer please test BEFORE next Monday! Blackboard multiple choice Magic numbers generated using online python version at: http://lynx.imada.sdu.dk/ 2

Code Café manned Code Cafe for students first time Wednesday, September 6 last time Wednesday, December 20 closed in Week 42 (efterårsferie) Mondays, 15.00 17.00, Nicky Cordua Mattsson Wednesdays, 15.00 17.00, Troels Risum Vigsøe Frimer Nicky and Troels can help with any coding related issues issues have to be related to some IMADA course (fx this one) 3

RECURSION: SEE RECURSION 4

Recursion is Complete so far we know: values of type integer, float, string arithmetic expressions (recursive) function definitions (recursive) function calls conditional execution input/output ALL possible programs can be written using these elements! we say that we have a Turing complete language 5

Factorial in mathematics, the function is defined by 0! = 1 n! = n * (n-1)! such recursive definitions can trivially be expressed in Python Example: def (n): if n == 0: return 1 recurse = (n-1) result = n * recurse return result x = (3) 6

Stack Diagram for Factorial main n è 3 n è 2 n è 1 n è 0 7

Stack Diagram for Factorial main n è 3 n è 2 n è 1 n è 0 1 8

Stack Diagram for Factorial main n è 3 n è 2 n è 1 recurse è 1 n è 0 1 9

Stack Diagram for Factorial main n è 3 n è 2 n è 1 recurse è 1 result è 1 n è 0 1 10

Stack Diagram for Factorial main n è 3 n è 2 n è 1 recurse è 1 result è 1 n è 0 1 1 11

Stack Diagram for Factorial main n è 3 n è 2 recurse è 1 n è 1 recurse è 1 result è 1 n è 0 1 1 12

Stack Diagram for Factorial main n è 3 n è 2 recurse è 1 result è 2 n è 1 recurse è 1 result è 1 n è 0 1 1 13

Stack Diagram for Factorial main n è 3 n è 2 recurse è 1 result è 2 n è 1 recurse è 1 result è 1 n è 0 2 1 1 14

Stack Diagram for Factorial main n è 3 recurse è 2 n è 2 recurse è 1 result è 2 n è 1 recurse è 1 result è 1 n è 0 2 1 1 15

Stack Diagram for Factorial main n è 3 recurse è 2 result è 6 n è 2 recurse è 1 result è 2 n è 1 recurse è 1 result è 1 n è 0 2 1 1 16

Stack Diagram for Factorial main n è 3 recurse è 2 result è 6 n è 2 recurse è 1 result è 2 n è 1 recurse è 1 result è 1 n è 0 6 2 1 1 17

Stack Diagram for Factorial main x è 6 n è 3 recurse è 2 result è 6 n è 2 recurse è 1 result è 2 n è 1 recurse è 1 result è 1 n è 0 6 2 1 1 18

Leap of Faith following the flow of execution difficult with recursion alternatively take the leap of faith (induction) Example: def (n): if n == 0: return 1 recurse = (n-1) result = n * recurse return result x = (3) check the base case assume recursive call is correct check the step case 19

Control Flow Diagram Example: def (n): if n == 0: (n) return 1 recurse = (n- 1) n == 0 result = n * recurse False return result recurse = (n-1) True result = n * recurse return result return 1 20

Fibonacci Fibonacci numbers model for unchecked rabbit population rabbit pairs at generation n is sum of rabbit pairs at generation n-1 and generation n-2 mathematically: fib(0) = 0, fib(1) = 1, fib(n) = fib(n-1) + fib(n-2) Pythonically: def fib(n): if n == 0: return 0 elif n == 1: return 1 else: return fib(n-1) + fib(n-2) leap of faith required even for small n! 21

Control Flow Diagram Example: def fib(n): if n == 0: fib(n) return 0 elif n == 1: n == 0 return 1 else: False return fib(n-1) + fib(n-2) n == 1 True False True return fib(n-1) + fib(n-2) return 1 return 0 22

Types and Base Cases def (n): if n == 0: return 1 recurse = (n-1) result = n * recurse return result Problem: (1.5) exceeds recursion limit (0.5) (-0.5) (-1.5) 23

Types and Base Cases def (n): if n == 0: return 1 recurse = (n-1) result = n * recurse return result Idea: check type at beginning of function 24

Types and Base Cases def (n): if not isinstance(n, int): print("integer required"); return None if n == 0: return 1 recurse = (n-1) result = n * recurse return result Idea: check type at beginning of function 25

Types and Base Cases def (n): if not isinstance(n, int): print("integer required"); return None if n < 0: print("non-negative number expected"); return None if n == 0: return 1 recurse = (n-1) result = n * recurse return result Idea: check type at beginning of function 26

Debugging Interfaces interfaces simplify testing and debugging 1. test if pre-conditions are given: do the arguments have the right type? are the values of the arguments ok? 1. test if the post-conditions are given: does the return value have the right type? is the return value computed correctly? 1. debug function, if pre- or post-conditions violated 27

Debugging (Recursive) Functions to check pre-conditions: print values & types of parameters at beginning of function insert check at beginning of function (pre assertion) to check post-conditions: print values before return statements insert check before return statements (post assertion) side-effect: visualize flow of execution 28

ITERATION 29

Multiple Assignment Revisited as seen before, variables can be assigned multiple times assignment is NOT the same as equality it is not symmetric, and changes with time Example: a = 42 b = a a = 23 from here, a and b are equal from here, a and b are different 30

Updating Variables most common form of multiple assignment is updating a variable is assigned to an expression containing that variable Example: x = 23 for i in range(19): x = x + 1 adding one is called incrementing expression evaluated BEFORE assignment takes place thus, variable needs to have been initialized earlier! 31

Iterating with While Loops iteration = repetition of code blocks can be implemented using recursion (countdown, polyline) while statement: <while-loop> => while <cond>: <instr 1 >; <instr 2 >; <instr 3 > Example: def countdown(n): while n > 0: n == 3 2 1 0 print(n, "seconds left!") n = n - 1 print("ka-boom!") countdown(3) False True 32

Termination Termination = the condition is eventually False loop in countdown obviously terminates: while n > 0: n = n - 1 difficult for other loops: def collatz(n): while n!= 1: print(n,end="") if n % 2 == 0: # n is even n = n // 2 else: # n is odd n = 3 * n + 1 33

Termination Termination = the condition is eventually False loop in countdown obviously terminates: while n > 0: n = n - 1 can also be difficult for recursion: def collatz(n): if n!= 1: print(n,end="") if n % 2 == 0: # n is even collatz(n // 2) else: # n is odd collatz(3 * n + 1) 34

Breaking a Loop sometimes you want to force termination Example: while True: num = input('enter a number (or "exit"):\n') if num == "exit": break n = int(num) print("square of", n, "is:", n**2) print("thanks a lot!") 35

Approximating Square Roots Newton s method for finding root of a function f: 1. start with some value x 0 2. refine this value using x n+1 = x n f(x n ) / f (x n ) for square root of a: f(x) = x 2 a f (x) = 2x simplifying for this special case: x n+1 = (x n + a / x n ) / 2 Example 1: while True: print(xn) xnp1 = (xn + a / xn) / 2 if xnp1 == xn: break xn = xnp1 36

Approximating Square Roots Newton s method for finding root of a function f: 1. start with some value x 0 2. refine this value using x n+1 = x n f(x n ) / f (x n ) Example 2: def f(x): return x**3 - math.cos(x) def f1(x): while True: print xn return 3*x**2 + math.sin(x) xnp1 = xn - f(xn) / f1(xn) if xnp1 == xn: break xn = xnp1 37

Approximating Square Roots Newton s method for finding root of a function f: 1. start with some value x 0 2. refine this value using x n+1 = x n f(x n ) / f (x n ) Example 2: def f(x): return x**3 - math.cos(x) def f1(x): while True: print xn return 3*x**2 + math.sin(x) xnp1 = xn - f(xn) / f1(xn) if abs(xnp1 - xn) < epsilon: break xn = xnp1 38

Algorithms algorithm = mechanical problem-solving process usually given as a step-by-step procedure for computation Newton s method is an example of an algorithm other examples: addition with carrying subtraction with borrowing long multiplication long division directly using Pythagora s formula is not an algorithm 39

Divide et Impera latin, means divide and conquer (courtesy of Julius Caesar) Idea: break down a problem and recursively work on parts Example: guessing a number by bisection def guess(low, high): if low == high: print("got you! You thought of: ", low) else: mid = (low+high) // 2 ans = input("is "+str(mid)+" correct (>, =, <)?\n") if ans == ">": guess(mid+1,high) elif ans == "<": guess(low,mid-1) else: print("yeehah! Got you!") 40

Debugging Larger Programs assume you have large function computing wrong return value going step-by-step very time consuming Idea: use bisection, i.e., half the search space in each step 1. insert intermediate output (e.g. using print) at mid-point 2. if intermediate output is correct, apply recursively to 2 nd part 3. if intermediate output is wrong, apply recursively to 1 st part 41