You are given a string containing ten digits, write a program to print these digits in a human readable format.
There are some rules to how to read a number,
• use the following prefixes for consecutive digits
# Single numbers just read them seperately
# if two successive numbers use double
# if three successive numbers use triple
# if four successive numbers use quadruple
SOLUTION TO THE ABOVE QUESTION
SOLUTIN CODE
#Prompt the user to enter a string containing 10 digits
ten_digit_string = input("\nEnter a string conatining 10 digits: ")
#Now lets define a list conataing the names of all 10 digits
digit_names = ["zero","one","two","three","four","five","six","seven","eight","nine"]
#Now we will use a while llop to loop through the 10 digit string;
terminate_condition = 0;
print("\n")
while terminate_condition<10:
my_digit = ten_digit_string[terminate_condition]
digit_string = digit_names[int(my_digit)]
#define another while llop to check if the next digits are the same as the current one
successive = 1;
successive_cond = 1;
while successive_cond !=-1:
if terminate_condition==9:
successive_cond = -1;
terminate_condition = terminate_condition + 1;
elif my_digit == ten_digit_string[terminate_condition+successive_cond]:
successive = successive + 1;
#update terminate condition
terminate_condition = terminate_condition + 1
else:
successive_cond = -1
# update terminate condition
terminate_condition = terminate_condition + 1;
if(successive == 2):
print("double "+digit_string, end=" ")
elif(successive==3):
print("triple " + digit_string,end=" ")
elif(successive==4):
print("quadruple " + digit_string,end=" ")
else:
print(digit_string,end=" ")
print("\n")
SAMPLE PROGRAM OUTPUT
Comments
Leave a comment