welcome to cs 2welcome to cs 2 • lecture will start at 10:40. • as usual, lecture will be...
TRANSCRIPT
![Page 1: Welcome to CS 2Welcome to CS 2 • Lecture will start at 10:40. • As usual, lecture will be recorded. • If you have any question, please ask in the chat. • About CS1: Please](https://reader034.vdocuments.us/reader034/viewer/2022052616/60a4a4679641734308676a1d/html5/thumbnails/1.jpg)
Welcome to CS 2
• Lecture will start at 10:40.• As usual, lecture will be recorded.• If you have any question, please ask in the chat.
• About CS1: Please do not ask me for your grades; I have not yet finished correcting all submissions... (I am late and I need to hurry)
Next week, I will give a brief report about CS1.
1
![Page 2: Welcome to CS 2Welcome to CS 2 • Lecture will start at 10:40. • As usual, lecture will be recorded. • If you have any question, please ask in the chat. • About CS1: Please](https://reader034.vdocuments.us/reader034/viewer/2022052616/60a4a4679641734308676a1d/html5/thumbnails/2.jpg)
Introduction toComputer Science II
EN(CS2)
December 2020
François BONNETTokyo Institute of Technology
School of [email protected]
![Page 3: Welcome to CS 2Welcome to CS 2 • Lecture will start at 10:40. • As usual, lecture will be recorded. • If you have any question, please ask in the chat. • About CS1: Please](https://reader034.vdocuments.us/reader034/viewer/2022052616/60a4a4679641734308676a1d/html5/thumbnails/3.jpg)
Course Guidance• Instructor: François Bonnet
• TAs: Nesrine Berjab and Luthfan Anshar Lubis
• Day/Time: Mondays 3-4
• Classroom: GSIC, 3F, Practice Room 1 Zoom
3
![Page 4: Welcome to CS 2Welcome to CS 2 • Lecture will start at 10:40. • As usual, lecture will be recorded. • If you have any question, please ask in the chat. • About CS1: Please](https://reader034.vdocuments.us/reader034/viewer/2022052616/60a4a4679641734308676a1d/html5/thumbnails/4.jpg)
Course ScheduleW1 (Dec 7) RecursionW2 (Dec 14) Algorithms and ComplexityW3 (Dec 21) Sorting Algorithms
No class on Dec 28 and Jan 4 (winter break), Jan 11 (national holiday)
W4 (Jan 14, Thu) Sorting (and other) AlgorithmsW5 (Jan 18) TBDW6 (Jan 25) TBDW7 (Feb 1) TBDTBD = To Be Decided
4
![Page 5: Welcome to CS 2Welcome to CS 2 • Lecture will start at 10:40. • As usual, lecture will be recorded. • If you have any question, please ask in the chat. • About CS1: Please](https://reader034.vdocuments.us/reader034/viewer/2022052616/60a4a4679641734308676a1d/html5/thumbnails/5.jpg)
Course Information• Course webpage:
https://titechcomp.github.io/y20-bonnet/
• Evaluation (probably):3 homeworks, 3 projects, and no final exam
5
(3 x 10 pts) (3 x 25pts)
![Page 6: Welcome to CS 2Welcome to CS 2 • Lecture will start at 10:40. • As usual, lecture will be recorded. • If you have any question, please ask in the chat. • About CS1: Please](https://reader034.vdocuments.us/reader034/viewer/2022052616/60a4a4679641734308676a1d/html5/thumbnails/6.jpg)
1: Recursion
![Page 7: Welcome to CS 2Welcome to CS 2 • Lecture will start at 10:40. • As usual, lecture will be recorded. • If you have any question, please ask in the chat. • About CS1: Please](https://reader034.vdocuments.us/reader034/viewer/2022052616/60a4a4679641734308676a1d/html5/thumbnails/7.jpg)
Today• Recursion• Homework 1• “Bonus:” Mysterious function
7
![Page 8: Welcome to CS 2Welcome to CS 2 • Lecture will start at 10:40. • As usual, lecture will be recorded. • If you have any question, please ask in the chat. • About CS1: Please](https://reader034.vdocuments.us/reader034/viewer/2022052616/60a4a4679641734308676a1d/html5/thumbnails/8.jpg)
Recursion — Addition# Function add as seen in Q3def add1(x, y):
"""Compute the sum of x and yAssume y>=0"""
while y > 0:x += 1y -= 1
return x
• It is correct only for positive y• How to deal with negative values of y?• Option 1: Add new specific code• Option 2: Use math: x + y = - ( (-x) + (-y) )
def add2(x, y):"""Compute the sum of x and y"""if y >= 0:
while y > 0: # When y >= 0x += 1y -= 1
else: # When y < 0while y < 0:
x -= 1y += 1
return x
def add3(x, y):"""Compute the sum of x and y"""if y < 0:
return -add3(-x, -y)while y > 0:
x += 1y -= 1
return x
Recursive call: The function calls itself !
when y<0, (-y) > 0
8
![Page 9: Welcome to CS 2Welcome to CS 2 • Lecture will start at 10:40. • As usual, lecture will be recorded. • If you have any question, please ask in the chat. • About CS1: Please](https://reader034.vdocuments.us/reader034/viewer/2022052616/60a4a4679641734308676a1d/html5/thumbnails/9.jpg)
Recursion — Addition• Use recursive call to deal with
negative values of y• Also possible to use recursive calls to
compute the sum: x + y = (x+1) + (y-1)
def add3(x, y):"""Compute the sum of x and y"""if y < 0:
return -add3(-x, -y)while y > 0:
x += 1y -= 1
return x
def add4(x, y):"""Compute the sum of x and y"""if y == 0:
return xelif y < 0:
return -add4(-x, -y)else:
return add4(x+1, y-1)
# Other possible solution# Equivalent to add4# Less clear (in my opinion)def add5(x, y):
"""Compute the sum of x and y"""if y == 0: return xif y < 0: return -add5(-x, -y)return add5(x+1, y-1)
9
![Page 10: Welcome to CS 2Welcome to CS 2 • Lecture will start at 10:40. • As usual, lecture will be recorded. • If you have any question, please ask in the chat. • About CS1: Please](https://reader034.vdocuments.us/reader034/viewer/2022052616/60a4a4679641734308676a1d/html5/thumbnails/10.jpg)
Recursion — Factorial• Factorial (Wikipedia) • How to compute it?‣Opt. 1: With a for loop‣Opt. 2: With a while loop
• Opt. 3: With recursion
𝑛𝑛! = �𝑘𝑘=1
𝑛𝑛
𝑘𝑘 = 𝑛𝑛 ∗ 𝑛𝑛 − 1 ∗ ⋯∗ 2 ∗ 1
# Two iterative solutionsdef fact_iter_for(n):
res = 1for k in range(1, n+1):
res *= kreturn res
def fact_iter_while(n):res = 1k = nwhile k > 1:
res *= kk -= 1
return res
# A recursive solutiondef fact_rec(n):
if n == 0:return 1
else:return n * fact_rec(n-1)
Base case-no recursion-immediate result
Recursive case10
![Page 11: Welcome to CS 2Welcome to CS 2 • Lecture will start at 10:40. • As usual, lecture will be recorded. • If you have any question, please ask in the chat. • About CS1: Please](https://reader034.vdocuments.us/reader034/viewer/2022052616/60a4a4679641734308676a1d/html5/thumbnails/11.jpg)
Recursion — FactorialRecursion is “similar” to mathematical induction• Induction: To prove that a property P(n) holds for all natural
number:‣ Prove the base case: P(0)‣ Prove that if P(n) then P(n+1)
• Recursion:‣ Solve the base case(s) directly‣ Otherwise “simplify” the problem and call again‣ Be careful with termination (avoid infinite recursion)
11
![Page 12: Welcome to CS 2Welcome to CS 2 • Lecture will start at 10:40. • As usual, lecture will be recorded. • If you have any question, please ask in the chat. • About CS1: Please](https://reader034.vdocuments.us/reader034/viewer/2022052616/60a4a4679641734308676a1d/html5/thumbnails/12.jpg)
HW 1 – Fibonacci • Fibonacci numbers:‣ F(0) = 0, F(1) = 1‣ For n > 1: F(n) = F(n-1) + F(n-2)
• Write two functions that computes F(n):‣ A function fibo_iter(n) using an iterative approach‣ A function fibo_rec(n) using a recursive approach
• Submit via OCWi a single python file with both functionsDeadline: before next lecture (Dec 14, 10:30)
12
![Page 13: Welcome to CS 2Welcome to CS 2 • Lecture will start at 10:40. • As usual, lecture will be recorded. • If you have any question, please ask in the chat. • About CS1: Please](https://reader034.vdocuments.us/reader034/viewer/2022052616/60a4a4679641734308676a1d/html5/thumbnails/13.jpg)
Mysterious Function• For people who knew about recursion and already
finished the HW, study this mysterious function:‣ Without executing on a computer,
try to find the value of f(0)‣ Implement the function in Python
and check the result‣ Can you prove what this function computes?
How many recursive calls when computing f(0)?How to count the number of recursive calls in python?
• No need to submit, no point given here; just to keep you busy (and discover a nice function).
def f(n):if n > 100:
return n - 10else:
return f(f(n + 11))
13