Download presentation
Presentation is loading. Please wait.
Published bySilvia Reeves Modified over 9 years ago
1
TUTORIAL 8 Generative Recursion
2
Reminders Deadline of midterm remark Assignment 7 due Wed, Mar 18 th
3
Tips for python Don’t leave lines in the middle of your code Up arrow key in interactions window Make sure you are using Python 3 Do not add any special characters in the code
4
Generative recursion CQ: which one is a generative recursion?
5
Generative recursion CQ: which one is a generative recursion?
6
Generative recursion CQ: what does recursion3([1,2,2,1]) and recursion3([1,2,1]) produce? A) True B) False C) None
7
Generative recursion CQ: what does recursion3([1,2,2,1]) and recursion3([1,2,1]) produce? A) True B) False C) None
8
Generative Recursion Sorting algorithms Log(n) and nlog(n) runtimes Sort vs sorted functions Insertion, selection, merge https://www.youtube.com/watch?v=kPRA0W1kECg https://www.youtube.com/watch?v=kPRA0W1kECg
9
Question 1 - smaller Write a function smaller that consumes a string containing only numeric characters and produces the smallest digit in the string. The produced digit should be in the form of a string. For example, smaller(“4325”) => “2” smaller(“1”) => “1” smaller(“2325”) => “2”
10
Solution method: - If there is only one digit, produce that digit - Compare the first digit with the last digit - Remove the larger of the two digits from the string and repeat the process - If there is a tie, remove the last digit from the string and repeat the process YEAH!!
11
Question 2 - Quicksort Consider a different way of sorting a list L of distinct integers: - Let n be the first element of the list - Let lst1 be all the elements in the list smaller than n - Let lst2 be all the elements in the list larger than n - Sort lst1 and lst2 - Add lst1 and lst2 together with n in the middle
12
Example quicksort([2,3,1,4,0]) quicksort([1,0]) + [2] + quicksort([3,4]) (quicksort([0]) + [1]) + [2] + ([3] +quicksort([4])) ([0] + [1])+ [2] + ([3] + [4]) [0,1] + [2] +[3,4] [0,1,2,3,4]
13
Questions 6 – sum_columns Consider a new type: Table. A Table is a (listof (listof Int)), which is nonempty, and in which each list corresponds to a row of a Table. It is assumed that each row is nonempty and each row has the same number of entries as every other row. Write a function sum_columns that consumes a Table t, and produces a list containing the columns sums for t.
14
Example t0 = [[1]] t1 = [[1,2,3]] t2 = [[1],[2],[3]] t3 = [[1,2],[3,4],[5,6],[7,8]] sum_columns(t0) => [1] sum_columns(t1) => [1,2,3] sum_columns(t3) => [16, 20]
15
Question 4 – binary_gcd Consider a different algorithm for computing the greatest common divisors of two positive integers m and n: - If either m or n are 0, produce the other number - If both numbers are even, produce 2*binary_gcd(m/2,n/2) - If m is even and n is odd, produce binary_gcd(m/2,n) - If n is even and m is odd, produce binary_gcd(m,n/2) - If both m and n are odd and m>=n, produce binary_gcd((m- n)/2,n) - If both m and n are odd and m<n, produce binary_gcd((n- m)/2,m)
16
Question 3 – is_balanced We define a string to be “balanced” based on the following criteria: - Any string not containing any brackets (any of: (,),{,},[,] ) is considered balanced - If s is a balanced string then (s), {s}, and [s] are also balanced strings - If s is a balanced string and t is a string not containing any brackets, then s+t is a balanced string and so is t+s - For example, “(Wo)rd” is balanced as well as “rd{Wo}”
17
Examples Balanced strings: “” “(hi)” “wrong{}thing” “[cool(brackets)]” “{[([{()}])]}” Unbalanced strings: “NO)PE” “{no)”
18
Question 5 – skip_value Given a list L of positive integers, what is the skip- value of the list? - If L is empty, the skip value is 0 - If L is nonempty: - Add one to the current skip value - Move ahead in the list by n (where n is the first element in the list) and repeat the process
19
Example 1 skip_value([1,1,1]) 1+skip_value([1,1]) 1+(1+skip_value([1])) 1+(1+(1+skip_value([]))) 1+(1+(1+0)) 3
20
Example 2 skip_value([2,100,3,1,1,1]) 1+skip_value([3,1,1,1]) 1+(1+skip_value([1])) 1+(1+(1+skip_value([]))) 1+(1+(1+0)) 3
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.