0

How to determine the output of this problem? What is happening in it?

def fun(n): if (n>100): return n-5 return fun(fun(n+11)) print(fun(45))

13th Sep 2023, 5:41 PM
Optimus Prime
Optimus Prime - avatar
7 odpowiedzi
+ 2
Well, the code is actually working fine and returning the answer as 100
13th Sep 2023, 5:45 PM
Optimus Prime
Optimus Prime - avatar
+ 2
fun(45) returns 95
13th Sep 2023, 6:50 PM
JAY
JAY - avatar
+ 1
It will not run because 45 is less than 100 Also if the number was greter than 100 you will run into an infinite loop
13th Sep 2023, 5:44 PM
Alaa Aldeen Shammr
Alaa Aldeen Shammr - avatar
+ 1
Optimus Prime yes You are right But there is a syntax error Closing parenthesis is missing
13th Sep 2023, 5:49 PM
Alaa Aldeen Shammr
Alaa Aldeen Shammr - avatar
+ 1
Whoops! Typo my bad. Thanks for pointing out.
13th Sep 2023, 5:50 PM
Optimus Prime
Optimus Prime - avatar
+ 1
I think it must be 100 but a I asked the sam question to ChatGPT he replied Initial call: fun(45) Since 45 is not greater than 100, it goes to the recursive call fun(fun(45 + 11)). First recursive call: fun(56) 56 is not greater than 100, so it goes to the next recursive call fun(fun(56 + 11)). Second recursive call: fun(67) 67 is not greater than 100, so it goes to the next recursive call fun(fun(67 + 11)). Third recursive call: fun(78) 78 is not greater than 100, so it goes to the next recursive call fun(fun(78 + 11)). Fourth recursive call: fun(89) 89 is not greater than 100, so it goes to the next recursive call fun(fun(89 + 11)). Fifth recursive call: fun(100) 100 is not greater than 100, so it goes to the next recursive call fun(fun(100 + 11)). Sixth recursive call: fun(111) 111 is greater than 100, so it returns 111 - 5 = 106 to the previous call (fun(100)). Back to the fifth recursive call: fun(106) 106 is not greater than 100, so it goes to the next recursive call fun(fun(106 + 11)). Seventh recursive call: fun(117) 117 is greater than 100, so it returns 117 - 5 = 112 to the previous call (fun(106)). Back to the fourth recursive call: fun(112) 112 is greater than 100, so it returns 112 - 5 = 107 to the previous call (fun(89)). Back to the third recursive call: fun(107) 107 is greater than 100, so it returns 107 - 5 = 102 to the previous call (fun(78)). Back to the second recursive call: fun(102) 102 is greater than 100, so it returns 102 - 5 = 97 to the previous call (fun(67)). Back to the first recursive call: fun(97) 97 is not greater than 100, so it goes to the next recursive call fun(fun(97 + 11)). Eighth recursive call: fun(108) 108 is greater than 100, so it returns 108 - 5 = 103 to the previous call (fun(97)). Back to the initial call: fun(103) 103 is greater than 100, so it returns 103 - 5 = 98 Then I send the following messege: when the recursive function reached at step 6, it becomes 89 + 100. Then why not it stop here. when n became 100 it must not be greater than 100 same Ans
21st Nov 2023, 4:56 AM
Tariq Saleemi
0
When fun(45) is initially passed, the condition sets to be false, which will add it to 56, next fun(56), will result in 67, then again the same condition follows, resulting 78, then 89, then 100, then again 111, Now, since 111 is greater than 100, it will subtract 5 form it, 106, then again its greater than 100, resulting in 101, again its greater than 100, resulting in 96, now since its lesser than 100, the command again passes, adding 11, it would go for fun(96) giving 107, which is again greater than 100, resulting in 102, following it would give 97, The same lower condition passes, resulting in 108, giving 103, then 98....following the lower loop, will add five 109, then 104, then 99, again with the lower loop, 110, this will give 105 and at last, 100.....Since, 100 is not lesser or greater than 100 itself, it would not go for any loop and the final answer is 100.
19th Jan 2024, 10:08 AM
pranjal sharma
pranjal sharma - avatar