+ 5

def convert(num): if num == 0: return 0 else: return (num % 2 + 10 * convert(num // 2))

It's recursion example in the lesson but i am unable to understand how it's working please help me

3rd Sep 2022, 6:01 PM
Mohammad Faiz
Mohammad Faiz - avatar
13 odpowiedzi
+ 11
Alright let's analyse this code together def convert(num): #here we create a function named convert which takes interger values represented by num in the bracket if num == 0: #this is the base case which means when the function is called the last value to be processed by the function will be will be 0 return 0 else: return (num % 2 + 10 * convert(num // 2)) #for the last step you'll better understand with an example If below the line we implemented print (convert(3)) As 3 is not equal to 0 The function will process the else statement: (3 % 2 + 10 * convert(3//2)) which is equal to (1 +10 * convert(1)) Now let's process convert(1) As 1 is not 0 the else statement is done : (1 % 2 + 10 * convert(1//2)) which is equal to (1 + 10 * convert(0)) Now we substitute the value of convert(1) in convert (3) and we get (1+ 10 * (1 + 10 *convert(0))) And lastly it's time for convert(0) to be processed As 0 is equal to 0 the recursion is ended Output: (1 + 10*(1 + 10 * 0)) = 11
3rd Sep 2022, 6:41 PM
Zen Ai
Zen Ai - avatar
+ 2
The purpose of this code is to convert from denary numbers to binary numbers
3rd Sep 2022, 6:42 PM
Zen Ai
Zen Ai - avatar
+ 2
[En]moVes well that's right, but I don't get the part where you're saying that he has an error in the code, I don't think he has any
3rd Sep 2022, 6:52 PM
Zen Ai
Zen Ai - avatar
+ 2
Take a look at the output of this code, I hope it helps you understand how the recursive function works. Good luck! ☺️ def convert(num): if num == 0: return 0 arg = num//2 print(num,'// 2 =',arg) rec = convert(arg) out = num % 2 + 10 * rec print(num,'% 2 + 10 *',rec,'=',out) return out print(convert(20))
4th Sep 2022, 8:49 PM
Solo
Solo - avatar
+ 1
You called this recursion, and with each function call, the value decreases so that there is no infinite loop...
3rd Sep 2022, 6:43 PM
Евгений Молдованов
Евгений Молдованов - avatar
+ 1
Zen Ai I'm sorry, it seemed to me that Enter is where the function call is
3rd Sep 2022, 7:01 PM
Евгений Молдованов
Евгений Молдованов - avatar
+ 1
mohsin raza this is what is done but that's a general formula used for it when using recursion , there's another way to do so that doesn't use it, well I'm sure with continuous learning you'll get it in the end
3rd Sep 2022, 8:20 PM
Zen Ai
Zen Ai - avatar
+ 1
Ok
3rd Sep 2022, 8:26 PM
Mohammad Faiz
Mohammad Faiz - avatar
+ 1
mohsin raza you can expand the recursive function to: print(convert(5)) print((5%2+10*(2%2+10*(1%2+10*0))))
5th Sep 2022, 4:53 AM
Bob_Li
Bob_Li - avatar
0
Thanks Zen Ai but i am still confused with algorithm of conversion decimal to binary
3rd Sep 2022, 8:11 PM
Mohammad Faiz
Mohammad Faiz - avatar
0
I have learned decimal to binary conversion is simply divide by 2 and note remainder
3rd Sep 2022, 8:12 PM
Mohammad Faiz
Mohammad Faiz - avatar
- 2
In a simple way. Recursion is a function that calls itself. And you have an error, the function convert call should be in else
3rd Sep 2022, 6:42 PM
Евгений Молдованов
Евгений Молдованов - avatar