1# Below uses constant space (converting to string and then gettings it's length does not)
2num = 12362
3count = 0
4while num > 0:
5 count += 1
6 num = num // 10
7print(count)
8
9# for negative numbers, take absolue value first:
10num = -12362
11num = abs(num)
12count = 0
13while num > 0:
14 count += 1
15 num = num // 10
16print(count)
1number_length = lambda s: len(str(s)) #number length without recursion
2
3#number length with recursion
4numlen = 1
5def num_len(s):
6 if s > 9:
7 numlen += 1
8 else:
9 s = numlen
10 numlen = 0
11 return s
12 return num_len(s//10)