0

What does mystery(9870) return?

def mystery(n): m = " " while n>0: m += str(n %10) n // = 10 return m

5th Apr 2017, 12:32 PM
FatehSarb tambers
FatehSarb tambers - avatar
3 Answers
+ 3
m += str(n % 10) concatenates the last digit of n to m. n //= 10 divides n with 10. Iteration 1 : 9870 % 10 = 0 -> m = "0" and n = 9870 // 10 = 987. Iteration 2 : 987 % 10 = 7 -> m = "0" + "7" = "07" and n = 987 // 10 = 98. Iteration 3 : 98 % 10 = 8 -> m = "07" + "8" = "078" and n = 98 // 10 = 9. Iteration 4 : 9 % 10 = 9 -> m = "078" + "9" = "0789" and n = 9 // 10 = 0. Loop condition fails. So, the output is "0789". Note that proper indentation must be done for the code to work. def mystery(n): m="" while(n>0): m+=str(n%10) n//=10 return m
5th Apr 2017, 3:09 PM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
0
with n //= 10 and proper indents: " 0789"
5th Apr 2017, 1:34 PM
Quack
Quack - avatar
0
can you please explain it completely?
5th Apr 2017, 1:42 PM
FatehSarb tambers
FatehSarb tambers - avatar