Solutions (1.16)#

Solution to Exercise 1.16

Pseudocode#

This is one way to write the pseudocode. Yours might be different. If you gave your pseudocode to your friend, could they easily turn it into a Python program?

Set value of n
Set n_digits to 0
While n is greater than or equal to 1:
    Divide n by 10
    Add 1 to n_digits
Display n_digits

Implementation#

# Input n
n = 1234

# Set n_digits to 0
n_digits = 0

# Extra step to remember starting value
start = n

while n >= 1:
    n = n / 10                 # Divide n by 10
    n_digits = n_digits + 1    # Add 1 to n_digits

# Output n_digits
print(start, "has", n_digits, "digits.")
1234 has 4 digits.

Testing#

When we write a program it is important to check that it works as expected in different scenarios. To do this, we need to consider different scenarios the program might encounter, and how we would expect a ‘correct’ program to behave.

For this program, some scenarios we might like to test are:

  • Positive integers with different numbers of digits:

# Input n
n = 7

# Set n_digits to 0
n_digits = 0

# Extra step to remember starting value
start = n

while n >= 1:
    n = n / 10                 # Divide n by 10
    n_digits = n_digits + 1    # Add 1 to n_digits

# Output n_digits
print(start, "has", n_digits, "digits.")
7 has 1 digits.
# Input n
n = 9876543210

# Set n_digits to 0
n_digits = 0

# Extra step to remember starting value
start = n

while n >= 1:
    n = n / 10                 # Divide n by 10
    n_digits = n_digits + 1    # Add 1 to n_digits

# Output n_digits
print(start, "has", n_digits, "digits.")
9876543210 has 10 digits.

These answers are both correct, but maybe we could tweak the code so that if there is only one digit it prints "7 has 1 digit." instead of "7 has 1 digits."

  • Zero

# Input n
n = 0

# Set n_digits to 0
n_digits = 0

# Extra step to remember starting value
start = n

while n >= 1:
    n = n / 10                 # Divide n by 10
    n_digits = n_digits + 1    # Add 1 to n_digits

# Output n_digits
print(start, "has", n_digits, "digits.")
0 has 0 digits.

The program is behaving as we would expect here, but we could think about whether zero is a special case, and the program should output "0 has 1 digit" instead. In scenarios like this, the answer often depends on what the program will actually be used for.

  • Numbers with decimal places

# Input n
n = 1234.5678

# Set n_digits to 0
n_digits = 0

# Extra step to remember starting value
start = n

while n >= 1:
    n = n / 10                 # Divide n by 10
    n_digits = n_digits + 1    # Add 1 to n_digits

# Output n_digits
print(start, "has", n_digits, "digits.")
1234.5678 has 4 digits.

The program is still working as we would expect - remember that this program is written to count the number of digits (i.e. positive powers of 10), not the number of decimal places (i.e. negative powers of 10)!

  • Negative numbers

# Input n
n = -1

# Set n_digits to 0
n_digits = 0

# Extra step to remember starting value
start = n

while n >= 1:
    n = n / 10                 # Divide n by 10
    n_digits = n_digits + 1    # Add 1 to n_digits

# Output n_digits
print(start, "has", n_digits, "digits.")
-1 has 0 digits.

The number is less than one, so we don’t enter the while loop. In this case, -1 has 1 digit, so we need to change the program

Extending#

First, let’s think about how we can change the pseudo-code so that the program works for negative numbers.

We know that a negative number, \(-n\), has the same number of digits as the positive number \(n\). So to make the program work for negative numbers, we just need to check whether the input is negative, and if it is, turn it into a positive number with the same number of digits.

Input n

If n < 0 then
    Multiply n by -1

Set n_digits to 0
While n >= 1
    Divide n by 10
    Add 1 to n_digits
    
Output n_digits
# Input n
n = -1

# Extra step to remember starting value
start = n

# Check if n is negative
if (n < 0):
    n = n * -1 # Make n positive

# Set n_digits to 0
n_digits = 0

while n >= 1:
    n = n / 10                 # Divide n by 10
    n_digits = n_digits + 1    # Add 1 to n_digits

# Output n_digits
print(start, "has", n_digits, "digits.")
-1 has 1 digits.
# Input n
n = -112358

# Extra step to remember starting value
start = n

# Check if n is negative
if (n < 0):
    n = n * -1 # Make n positive

# Set n_digits to 0
n_digits = 0

while n >= 1:
    n = n / 10                 # Divide n by 10
    n_digits = n_digits + 1    # Add 1 to n_digits

# Output n_digits
print(start, "has", n_digits, "digits.")
-112358 has 6 digits.
# Input n
n = -112358

# Extra step to remember starting value
start = n

# Check if n is negative
if (n < 0):
    n = n * -1 # Make n positive

# Set n_digits to 0
n_digits = 0

while n >= 1:
    n = n / 10                 # Divide n by 10
    n_digits = n_digits + 1    # Add 1 to n_digits

# Output n_digits
print(start, "has", n_digits, "digits.")
-112358 has 6 digits.