w3resource

Python: Restore the original string by entering the compressed string with specified rule

Python Basic - 1: Exercise-58 with Solution

When character are consecutive in a string , it is possible to shorten the character string by replacing the character with a certain rule. For example, in the case of the character string YYYYY, if it is expressed as # 5 Y, it is compressed by one character.

Write a Python program to restore the original string by entering the compressed string with this rule. However, the # character does not appear in the restored character string.

Note: The original sentences are uppercase letters, lowercase letters, numbers, symbols, less than 100 letters, and consecutive letters are not more than 9 letters.

Input:
The restored character string for each character on one line.
Original text: XY#6Z1#4023
XYZZZZZZ1000023
Original text: #39+1=1#30
999+1=1000

Sample Solution:

Python Code:

# Define a function 'restore_original_str' with parameter 'a1'
def restore_original_str(a1):
    # Initialize an empty string 'result' to store the restored original text
    result = ""
    # Initialize index 'ind' to 0
    ind = 0
    # Set 'end' to the length of the input string 'a1'
    end = len(a1)
    
    # Start a while loop until 'ind' reaches the end of the input string
    while ind < end:
        # Check if the current character is '#' (indicating a pattern)
        if a1[ind] == "#":
            # Append the character at the next position repeated by the integer at the position after that
            result += a1[ind + 2] * int(a1[ind + 1])
            # Move the index 'ind' to the position after the pattern
            ind += 3
        else:
            # If not a pattern, append the current character
            result += a1[ind]
            # Move the index 'ind' to the next position
            ind += 1
    
    # Return the restored original text
    return result

# Print a statement indicating the original text and the input string
print("Original text:", "XY#6Z1#4023")
# Call the 'restore_original_str' function with the input string and print the result
print(restore_original_str("XY#6Z1#4023"))

# Print another statement with a different input string
print("Original text:", "#39+1=1#30")
# Call the 'restore_original_str' function with the second input string and print the result
print(restore_original_str("#39+1=1#30"))

Sample Output:

Original text: XY#6Z1#4023
XYZZZZZZ1000023
Original text: #39+1=1#30
999+1=1000

Explanation:

Here is a breakdown of the above Python code:

  • Define a function "restore_original_str()" to restore the original text from a given pattern.
  • Initialize an empty string 'result' to store the restored original text.
  • Initialize index 'ind' to 0 and set 'end' to the length of the input string 'a1'.
  • Use a while loop to iterate through the characters in the input string.
  • Check if the current character is '#' indicating a pattern.
  • If it's a pattern, append the character at the next position repeated by the integer at the position after that.
  • Move the index 'ind' to the position after the pattern.
  • If it's not a pattern, append the current character and move the index 'ind' to the next position.
  • Continue the loop until 'ind' reaches the end of the input string.
  • Return the restored original text.

Visual Presentation:

Python: Restore the original string by entering the compressed string with specified rule.
Python: Restore the original string by entering the compressed string with specified rule.

Flowchart:

Flowchart: Python - Restore the original string by entering the compressed string with specified rule

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to sum of all numerical values (positive integers) embedded in a sentence.
Next: Write a Python program that compute the area of the polygon . The vertices have the names vertex 1, vertex 2, vertex 3, ... vertex n according to the order of edge connections.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.