Answer to Question #266404 in Python for Viswa

Question #266404

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

1
Expert's answer
2021-11-15T07:57:34-0500


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



Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS