Consecutive letter removal of 3 or MORE consecutive letters

Question:

The program asks for a string and outputs the string remaining after removing groups of 3 or more meaning it can be 3, 4, 5, 6, and so on, consecutive identical characters. The removal of the groups continues until no more groups are found to be removed. The program should allow repetition until the user does not want to continue. The code must only use while, if, elif, else and no other functions

#Message
print("Hello! Welcome to Candy Crush!") #Introduces user to the application
#Input data
while True:
    decision = str(input("Would you like to add a string? ")) #Asks the user if they would like to enter a string.
#Process data
    if decision == "Yes" or decision == "yes": #If the user answers yes, it continues but if the user answers something else other than yes and no, it repeats the option.
        stringInput = str(input("Please enter a string: ")) #Asks the user to enter the string.
        charactersInput = list(stringInput) #Stores the user input in a list.
        print("Your original list is", charactersInput)
        print("Your original string: ", stringInput)
        idx = 0  #This number helps to keep track of what letter the program is processing.
        while idx < len(charactersInput) - 2:
            if charactersInput[idx] == charactersInput[idx + 1] == charactersInput[idx + 2]:
                charactersInput.pop(idx) #The code removes the first index letter.
                charactersInput.pop(idx) #The code removes the second index letter.
                charactersInput.pop(idx) #The code removes the third index letter. Once the three letters have been removed the next letters take their place.
                idx = 0
            else:  
                idx += 1 #One is added to the index to see if the new index number is followed by repeated letters.
        result = "".join(charactersInput)#After the code has ran, the following code takes all the results and adds the strings to charactersInput. The information is stored in the variable result.
#Output data
        print("The output of your string is: ", result,"n") #Prints the information stored in result. The information does not include the consecutive letters.
    if decision == "No" or decision == "no":
        print("Thank you for playing! Have a great day!") #The user is shown a goodbye message and the code ends.
        exit() #The application ends if the user answers no

pop is not allowed. Use remove instead. Also, no use of break.

Asked By: Dark _Knight

||

Answers:

Code:-

#Message
print("Hello! Welcome to Candy Crush!") #Introduces user to the application
#Input data
while True:
    decision = str(input("Would you like to add a string? ")) #Asks the user if they would like to enter a string.
#Process data
    if decision == "Yes" or decision == "yes": #If the user answers yes, it continues but if the user answers something else other than yes and no, it repeats the option.
        stringInput = str(input("Please enter a string: ")) #Asks the user to enter the string.
        charactersInput = list(stringInput) #Stores the user input in a list.
        print("Your original list is", charactersInput)
        print("Your original string: ", stringInput)
        
        stack = []
        char_num=3
        for ch in stringInput:
            if stack and ch != stack[-1][0] and stack[-1][1] >= char_num:
                stack.pop()
    
            if not stack or ch != stack[-1][0]:
                stack.append([ch, 1])
            else:
                stack[-1][1] += 1
        if stack and stack[-1][1] >= char_num:
            stack.pop()
        result = ""
        while stack:
            item = stack.pop()
            result = item[0]*item[1] + result
        
#Output data
        print("The output of your string is: ", result,"n") #Prints the information stored in result. The information does not include the consecutive letters.
    if decision == "No" or decision == "no":
        print("Thank you for playing! Have a great day!") #The user is shown a goodbye message and the code ends.
        exit() #The application ends if the user answers no

Output:-

Hello! Welcome to Candy Crush!
Would you like to add a string? yes
Please enter a string: aaaabbbcccd
Your original list is ['a', 'a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd']
Your original string:  aaaabbbcccd
The output of your string is:  d 

Other changes i prefer you should do..

(1)No need to check like this if decision == "Yes" or decision == "yes": instead check like if decision.lower()=="yes": same goes for no one also..

Answered By: Yash Mehta
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.