Question: Problem 4 digits.py: Number of digits [15 points] Write a program that accepts a positive integer N as command-line argument, and outputs its number of
Problem 4 digits.py: Number of digits [15 points] Write a program that accepts a positive integer N as command-line argument, and outputs its number of digits1(also an integer).
"In order to receive any credit, functions such asmath.log10ormath.ceil(or similar) should not be used. In general, you should implement the loop yourselves. Solutions that avoid implementing a loop will receive no credit.
Practice goal: Variation of powers of 10 example, even though the loop does not fall into the simple accumulation pattern (there is no term/parameter sequence that is completely known a priori).
Remark:You may notice that the answer is closely related to log10N, although it is not identical (specifically, it isequal toblog10Nc+1). However, as noted above, you shouldnotuse this fact in your solution.
Hint: In general, the integer (aka. truncating) division and remainder (aka. modulo) operators,//and% respectively, are often used to perform digit manipulation. Specifically, the division N // 10produces a new number which is, informally, equal to N but with its last (i.e., unit place value) decimal digit deleted. Similarly, the remainder N % 10produces an integer equal to the last decimal digit of N. With that in light, the overall strategy is simple: keep replacing N by N // 10, as long as N remains positive,i.e., as long as N > 0. (What if we used regular division (i.e.,N / 10) instead? Could the strategy be modified to work in this case?) The number of iterations (i.e., division updates of N) should be equal to the number of digits of N(given thatN1).
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
