Answer to Question #79863 in Python for saiprasad

Question #79863
What does g(31415927) return, for the following function definition?
def g(x):
(q,d) = (1,0)
while q <= x:
(q,d) = (q*10,d+1)
return(d)
1
Expert's answer
2018-08-17T04:42:53-0400
Solution:
Firstly, we justify the function in order to provide readability:
def g(x):
q = 1
d = 0
while q <= x:
q *= 10
d += 1
return(d)

Now we consider x as integer. Easy to see that this function returns number of digits in x (since q is multiplied by 10 every time when it’s smaller than x, so d += 1 executed exactly number of digits times). So the exact answer for x = 31415927 is 8.
Answer: 8

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS