SIC - C - P - DS - Chapter 3 - Unit18 - L
SIC - C - P - DS - Chapter 3 - Unit18 - L
Number 1, A -> C
Number 2, A -> B
Number 1, C -> B
Number 3, A -> C
Number 1, B -> A
Number 2, B -> C
Number 1, A -> C
UNIT 18
Start
[1] Start
hanoi(n, from_, via_, to_)
[2] Call the function hanoi(n, from_, to_, via_)
NO
if number_of_disks_to_move == 1
[3] if there is only one disk then {
[4] Move the disk from from_ to to_} YES
Call the function hanoi(n-1, via_, to_, from_) } hanoi(number_of_disks_to_move-1, via_, to_,
from_)
[8] End
End
UNIT 18
Enter a number : 5
5! = 120
Line 3, 4
• From 1 to n, the fact variable is multiplied by accumulating by i.
UNIT 18
def factorial(5):
if n <= 1 :
return 1
else :
return n * factorial(n-1)
3 * factorial(2)
2 * factorial(1)
UNIT 18
factorial(5)
return 4 * factorial(3) = 24
return 3 * factorial(2) = 6
return 2 * factorial(1) = 2
1
UNIT 18
𝐹𝑛 = 𝐹𝑛−1 + 𝐹𝑛−2
• 0, 1, 1, 2, 3, 5, 8, 13, 21,...
fib(5)
fib(4) fib(3)
fib(1) fib(0)
Called Function
UNIT 18
Line 7
• The value is added as it is called recursively until the termination condition is met.
UNIT 18
Called Function
Function call steps before memoization Function call steps after memoization
UNIT 18
Memoization Function
Call fib(3) : return fib(2) + fib(1)
Function call steps after memoization Call fib(3): return dic[1] + dic[2], which becomes 2
dic = {0:0, 1:1, 2:1, 3:2} state updated
...
Try to fully understand the basic concept before moving on to the next step.
Lack of understanding basic concepts will increase your burden in learning this course, which
may make you fail the course.
It may be difficult now, but for successful completion of this course we suggest you to fully
understand the concept and move on to the next step.
UNIT 18
Time
Write the entire code and the expected output results in the note.
UNIT 18
Enter x : 2
Condition for
Enter n : 10
Execution
1024
Time
Write the entire code and the expected output results in the note.
UNIT 18
input 1 function
input 3
UNIT 18
function
input output
‘ABCD’ len() 4
UNIT 18
# Sort string
Line 1, 2
• The sorted function receives a string and returns the characters that make up the string sorted in alphabetical order.
• The sorted function also has a function to sort and return a list.
• reverse = true to sort in descending order.
UNIT 18
# The Unicode value 65 is the alphabet ‘A’, which the chr() function returns
Output example
eular(20) = 2.71828