HUAWEI OD machine test real questions - decompression message - 2023 OD unified test (B paper)

Title description:

In order to improve the efficiency of data transmission, the transmitted packets are compressed. Input a compressed message, please return its decompressed original message.

Compression rule: n[str], which means that the str inside the square brackets is repeated exactly n times. Note that n is a positive integer (0 < n <= 100), and str only contains lowercase English letters, regardless of abnormal conditions.

Enter a description:

Enter the compressed message:

1) Regardless of invalid input, the message has no extra spaces, and the square brackets always meet the format requirements;

2) The original message does not contain numbers, and all numbers only represent the number of repetitions n, for example, there will be no input like 5b or 3[8];

Output description:

Original message after decompression

Note:

1) The length of the original message will not exceed 1000, regardless of the abnormal situation

Supplementary note:

Example 1

enter:

3[k]2[mn]

output:

kkkmnmn

illustrate:

k is repeated 3 times, mn is repeated 2 times, and finally kkkmnmn is obtained

Example 2

enter:

3[m2[c]]

output:

mccmccmcc

illustrate:

m2[c] is mcc after decompression, mccmccmcc after repeating three times

import re
def tran(s):
    pattern=r'(\d+)\[([a-zA-Z]+)\]'
    while '[' in s:
        s = re.sub(pattern,lambda match: int(match.group(1))*match.group(2),s)
    return s
s= input()
print(tran(s))

Guess you like

Origin blog.csdn.net/2301_76848549/article/details/131457244