Removal of 3 or more consecutive letters in a string given by the user

Question:

The program asks for a string and outputs the string remaining after removing groups of 3 or more 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.

Asked By: Dark _Knight

||

Answers:

Hi Dark_Knight and welcome to Stack Overflow!

Here is the solution I had in mind. Although this solves the problem, I wanted to make this a learning experience for you and other programmers who stumble upon this. I blocked off, with comments, the start and end of my changes as well as step by step comments on the decisions I made for the solution.

#Message
print("Hello! Welcome to Candy Crush!")
while True:
    decision = str(input("Would you like to add a string? "))
    if decision == "Yes" or decision == "yes":
        # Start of solution changes
        # Lets make variables more clear
        stringInput = str(input("Please enter a string: "))
        charactersInput = list(stringInput)
        print("Your original list is", charactersInput) # Still the same...
        print("Your original string: ", stringInput) # Also still the same
        # For our loop, idx will be used to track the index of the list
        idx = 0
        # Lets use a while loop instead of a for loop
        # We will exit the loop when there aren't 3 consecutive characters
        while idx < len(charactersInput) - 2:
            # Check if the current character is the same as the next 2
            if charactersInput[idx] == charactersInput[idx + 1] == charactersInput[idx + 2]:
                # If so, remove the current and next 2 characters
                # We are using pop() to remove the characters
                # So the next character will be at the current index
                charactersInput.pop(idx)
                charactersInput.pop(idx)
                charactersInput.pop(idx)
                # Okay, we made a change to the list, so we need to reset idx
                # As there might be a new set of 3 consecutive characters
                idx = 0
            else:
                # If the current character is not the same as the next two,
                # then increment idx by 1
                idx += 1
        result = "".join(charactersInput)
        # End of solution changes
        print("nThe output of your string is: ", result)
    if decision == "No" or decision == "no":
        print("Thank you for playing! Have a great day!")
        exit()

Here are some things to keep in mind that might make finding your own solution easier.

  • Make sure that all of your variables are clear and don’t utilize keywords used in Python (or any other programming language). Using list for a list isn’t descriptive and is the same terminology Python uses to work with lists
  • Break down the problems into smaller pieces. While I was working I first asked "What type of loop do I need?", then "At my current iteration in the loop, what do I need to check?", etc, etc,
  • Help out the community and include as many fixes you tried before creating a post. It not only helps people trying to help you out but also helps other users learn what solutions don’t work
Answered By: Matthew Moran
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.