In: Computer Science
2.2 Decimal-to-Undecimal
Exercise Assign to undecimal_str the undecimal string that represents a non-negative integer decimal of any size.
Hint: For loop or while loop?
write the program:
def decimal_to_undecimal(decimal):
# YOUR CODE HERE
return undecimal_str
def decimal_to_undecimal(decimal):
result = []
while decimal > 0:
result.append(str(decimal % 10))
decimal = decimal // 10
return ''.join(result[::-1])
print(decimal_to_undecimal(10000))
print(type(decimal_to_undecimal(10000)))
Output:
(Class 'str' means that we have outputted a string instead of a number in this case).
Explanation:
In this code, we iterate over the number digit by digit. We know that the last digit of a number can be obtained by taking the remainder of the number by 10. We can use this method to iteratively find all the digits of the number, by repeatedly dividing the number by 10, and converting each remainder to a string. We add each string converted value of the remainder to a list called result.
For a sample input 1234, here's how this would work:
1. We take remainder of 1234 with 10, we get 4. We add '4' (integer converted to string using the str() function) to the list called result. We then divide the decimal input by 10, which gives us decimal = 123. Current value of result = ['4'].
2. We take remainder of 123 with 10, we get 3 as the result. We add '3' to the list result. We divide 123 by 10, we get 12. Current value of result = ['4', '3'].
3. We take remainder of 12 with 10, we get 2 as the result. We add '2' to the list result. We divide 12 by 10, we get the value 1 as the quotient. Current value of result = ['4','3','2'].
4. We take remainder of 1 with 10, we get 1 as the result. We add '1' to the list result. We divide 1 by 10, we get the quotient 0. Current value of result = ['4','3','2','1']. The while loop does not execute again since the value of decimal has now become zero and the condition for the while loop is no longer valid.
We then use a method called ''.join(list of characters) to convert the list of characters into a string. Now, when we simply output the ''.join(result), we get the inverse of the number we intend to have - this is because we are going from the first digit being added to the result list at the end (the one with the highest power of 10), and the first digit being added to the result list right at the beginning (the one with the highest power of 10). In our example, '4' is the last digit which gets added first, and '1' is the first digit which gets added last.
Hence, for our program to work correctly, we have to reverse our list, and then use the ''.join(result) command. This can be achieved without using a for loop through each of the characters in the list, by employing the list operation with index "::-1" -> this reverses the list in place.
We show the difference between not using ::-1 and using ::-1 as follows:
Output:
Code with the list reversing syntax:
Output:
Hence, we have successfully converted a non-negative decimal value into a string.
Here's the code:
def decimal_to_undecimal(decimal):
result = []
while decimal > 0:
result.append(str(decimal % 10))
decimal = decimal // 10
undecimal_str = ''.join(result[::-1])
return undecimal_str
print(decimal_to_undecimal(987654321))
(I have changed it at the end to meet the code structure requirement as shown in the question)
If you liked this answer, please consider giving it a thumbs up. Thank you!