3
\$\begingroup\$

Here is my code that finds the number of digits in a given integer (either positive or negative). The code works and results in expected output.

'''
Program that returns number of digits in a given integer (either positive or negative)
'''
def ndigits(x):
 # Assume for the input 0 the output is 0
 if(x == 0):
 return 0
 if(abs(x) / 10 == 0):
 return 1
 else:
 return 1 + ndigits(x / 10)
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Jun 30, 2016 at 8:10
\$\endgroup\$
4
  • \$\begingroup\$ def ndigits(x): return sum(int(i) for i in str(x)) Here are a few other solutions stackoverflow.com/questions/14939953/…. \$\endgroup\$ Commented Jun 30, 2016 at 8:20
  • \$\begingroup\$ @N3buchadnezzar: He is not interested in the sum of the digits only in the number of digits. But the solutions can be adapted to this difference. \$\endgroup\$ Commented Jun 30, 2016 at 10:14
  • \$\begingroup\$ @MrSmith42 def ndigits(x): return 1 + int(log(x, 10) Something I am still missing? \$\endgroup\$ Commented Jun 30, 2016 at 13:35
  • \$\begingroup\$ @N3buchadnezzar: The link to the 'few other solutions' is a link to where the sum of the digits is calculated. \$\endgroup\$ Commented Jul 1, 2016 at 8:28

1 Answer 1

1
\$\begingroup\$

pro:

  • code is clean and easy to understand

con:

  • a recursive solution may not be optimized to a loop by your interpreter so there might be a lot of memory (stack) waste because of the recursion overhead. (So you could implement it as a loop instead of a recursion)
  • You do not need the if(abs(x) / 10 == 0) branch. So to simplify your code you could remove it.

Simplified recursive code:

def ndigits(x):
 # Assume for the input 0 the output is 0
 if(x == 0):
 return 0
 else:
 return 1 + ndigits(abs(x) / 10)

Simplified tail-recursive code: end-recursive methods are likely to be detected by the interpreter and transformed to a loop. So this code might be faster and may nor waste memory.
For more information see wikipedia Tail call

def ndigits(x):
 return ndigits_(0, abs(x))
def ndigits_(s,x):
 if(x == 0):
 return s
 else:
 return ndigits(s+1, x / 10)
answered Jun 30, 2016 at 8:16
\$\endgroup\$
3
  • \$\begingroup\$ The python interpreter doesn't do that sort of optimization period. There is some sort of work going on in that area, but that is the case right now. \$\endgroup\$ Commented Jun 30, 2016 at 15:57
  • \$\begingroup\$ @TheBlackCat: tail recursion is a very common optimization, so we can expect it also from the python interpreter in the near future. But good to know that it is not the case yet. \$\endgroup\$ Commented Jul 1, 2016 at 8:26
  • \$\begingroup\$ Not going to happen. Guido has explicitly rejected adding tail recursion to Python. \$\endgroup\$ Commented Jul 1, 2016 at 15:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.