Recursion Exercises.

Slides:



Advertisements
Similar presentations
Joshua Eckroth The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the.
Advertisements

For(int i = 1; i
What gets printed out? def f(n): if (n == 0): return (" ") else: print(“blug ") return f(n-1) f(3)
PYTHON FRUITFUL FUNCTIONS CHAPTER 6 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
Hand Trace and Output for: int digit = 0; int number = 1423; do { digit = number % 10; System.out.println(digit); number = number / 10; } while (number.
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
© 2004 Goodrich, Tamassia QuickSort1 Quick-Sort     29  9.
Order Statistics. Selection Problem Input: a set A of n distinct numbers and a number i where i
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Recursion. Sum a list of numbers Iterative def sum(L): total = 0 for i in L: total += i return total Recursive def sum(L): if len(L) == 0: return 0 else:
Math – Getting Information from the Graph of a Function 1.
1 Object-Oriented Programming Using C++ CLASS 2. 2 Linear Recursion Summing the Elements of an Array Recursively Algorithm LinearSum(A, n): Input: An.
Programming Training Main Points: - Lists / Arrays in Python. - Fundamental algorithms on Arrays.
Building Java Programs Chapter 13 Searching reading: 13.3.
More While Loop Examples CS303E: Elements of Computers and Programming.
The building blocks of functional computing data, sequences conditionals recursion CS 121 today List Comprehensions map and applications.
EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University
9/14/2015BCHB Edwards Introduction to Python BCHB Lecture 4.
Math – What is a Function? 1. 2 input output function.
EECS 110: Lec 3: Data Aleksandar Kuzmanovic Northwestern University
EECS 110: Lec 9: Review for the Midterm Exam Aleksandar Kuzmanovic Northwestern University
The last CS 5 lecture you’ll ever need! Inducing labor for the machine! == Reducing labor for humans! On Warner Brothers' insistence, we affirm that this.
Lists Victor Norman CS104. Reading Quiz Lists Our second collection data type – elements are in order (like strings) – indexed from 0 to n – 1 (like.
Review. Problem 1 What is the output of the following piece of code void increment(int x) { x++; } int main() { int y = 10; increment(y); cout
Haskell Chapter 4. Recursion  Like other languages  Base case  Recursive call  Author programs a number of built-in functions as examples.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Find LCM Least Common Multiple of 3 and 5: List the Multiples of each number, The multiples of 3 are 3, 6, 9, 12, 15, 18,... etc The multiples of 5 are.
CS 121 Today Fractals and Turtles! The Koch Curve how random…
Python Basics.
Chapter 2 Writing Simple Programs
Introduction to Python
CSc 110, Autumn 2016 Lecture 36: searching.
CS 550 Programming Languages Jeremy Johnson
EECS 110: Lec 9: Review for the Midterm Exam
Algorithmic complexity: Speed of algorithms
EECS 110: Lec 5: List Comprehensions
Haskell Chapter 4.
Computer Programming Fundamentals
Lesson 07: Strings Topic: Introduction to Programming, Zybook Ch 6, P4E Ch 6. Slides on website.
Adapted from slides by Marty Stepp and Stuart Reges
CS 270 Math Foundations of CS Jeremy Johnson
EECS 110: Lec 4: Functions and Recursion
HI !.
Advanced Sorting Methods: Shellsort
شاخصهای عملکردی بیمارستان
فرق بین خوب وعالی فقط اندکی تلاش بیشتر است
الوحدة الرابعة البرمجة وصياغة حل المسائل البرمجة وأهميتها أهداف الدرس الأول مفهوم البرمجة. الفرق بين المبرمج ومستخدم البرنامج. الحاجة إلى البرامج.
Programming Training Main Points:
Searching: linear & binary
Programming Training Main Points: - Working with Functions in Python
EECS 110: Lec 4: Functions and Recursion
Tuples.
From Recursion To Iteration: A Case Study
Strip me.
Adapted from slides by Marty Stepp and Stuart Reges
Python Basics with Jupyter Notebook
Introduction to Python
Nate Brunelle Today: Conditional Decision Statements
Searching and Sorting (2)
Exercise 5 1. We learned bubble sort during class. This problem requires you to modify the code for bubble sorting method to implement the selection sorting.
List Comprehensions Problem: given a list of prices, generate a new list that has a 20% discount to each. Formally: input: list of old prices; output:
More on recursion Last time we went through a workshop on recursive functions. Let’s look at some of the problems together.
IST256 : Applications Programming for Information Systems
Recursion Review Spring 2019 CS 1110
§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊
Challenge Guide Grade Code Type Slides
Python Reserved Words Poster
Unit Testing.
Lecture 6 - Recursion.
Presentation transcript:

Recursion Exercises

Exercise 1: find_list_max Write a recursive function called find_list_max that returns the maximum value in a list Examples: >>> find_list_max([4, 13, 21, 5, 2]) 21 >>> find_list_max([1, -3, 8, -5, 12]) 12

Exercise 1: find_list_max def find_list_max(t): """ input: a NONEMPTY list, t output: t's maximum element """ if : elif : else: base case another case… another case…

find_list_max def find_list_max(t): """ input: a NONEMPTY list, t output: t's maximum element """ if len(t) == 1: return t[0] elif t[0] < t[1]: # t[0] can’t be the max, remove it return find_list_max(t[1:]) else: # t[1] can’t be the max, remove it return find_list_max(t[0:1] + t[2:]) Pay close attention to L[0:1] – make sure they do not do t[0] ! Hey - do I get a slice?!

Try it out …

Exercise 2: extract a sub-list Write a recursive function called extract_list that returns a sub-list for a given range of index Examples: >>>extract_list([4, 13, 21, 5, 2], 2, 5) [21, 5, 2] >>>extract_list([‘hello’, ‘world’, ‘how’, ‘are’, you’, ‘?’], 2, 5) [‘how’, ‘are’, ‘you’]

extract_ist def extract_list(t, low, hi): """ input: list t, two ints, low and hi output: list from low up to, not including hi """ if hi <= low: # base case return [] else: return if (hi <= lo): return [] return [lo] + range(lo+1,hi)

extract_list def extract_list(t, low, hi): """ input: list t, two ints, low and hi output: list from low up to, not including hi “”” if hi <= low: return [] else: return [t[low]] + extract_list(t, low+1, hi) if hi <= lo: return [] return [lo] + range(lo+1,hi)

Try it out …

Recursion workshop Work on the problems in the workshop sheet in a group of two or three Show me the result when you complete a problem