Advertisement
Guest User

Untitled

a guest
Jun 30th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. '''
  2. Program that returns number of digits in a given integer (either positive or negative)
  3. '''
  4. def ndigits(x):
  5. # Assume for the input 0 the output is 0
  6. if(x == 0):
  7. return 0
  8. if(abs(x) / 10 == 0):
  9. return 1
  10. else:
  11. return 1 + ndigits(x / 10)
  12.  
  13. def ndigits(x):
  14. # Assume for the input 0 the output is 0
  15. if(x == 0):
  16. return 0
  17. else:
  18. return 1 + ndigits(abs(x) / 10)
  19.  
  20. def ndigits(x):
  21. return ndigits_(0, abs(x))
  22.  
  23. def ndigits_(s,x):
  24. if(x == 0):
  25. return s
  26. else:
  27. return ndigits(s+1, x / 10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement