• 1
    Input and Output Data
    • Tasks
  • 2
    Conditions
    • Tasks
  • 3
    For Loop
    • Tasks
  • 4
    Strings
    • Tasks
  • 5
    While Loop
    • Tasks
  • 6
    Lists
    • Tasks
  • 7
    Two-Dimensional Arrays
    • Tasks
  • 8
    Dictionaries
    • Tasks
  • 9
    Sets
    • Tasks
  • 10
    Functions and Recursion
    • Tasks
  • к

Занятие 3. FOR cycle

Difficulty level:

Task«The sum of a mixed array»

you will be given a line "packaged" according to a certain rule: & nbsp; k [subject] & nbsp; means that it means that it means that it means that Settings & nbsp; sub & nbsp; inside the square brackets, it is repeated exactly & nbsp; k & nbsp; since. The number & nbsp; k & nbsp; it is always a positive integer. Your task & mdash; “unpack” the initial line.

rn

note that the lines can be invested in each other. For example, & nbsp; 3 [a2 [c]] & nbsp; it means that the line & nbsp; a2 [c] & nbsp; repeated 3 times. First you need to unpack the inner part & nbsp; 2 [c] & nbsp; in & nbsp; cc , then substitute it in & nbsp; a ... , getting & nbsp; accc , and and At the end, repeat & nbsp; acc & nbsp; 3 times to get & nbsp; accaccaccacccaccccccaccccccccccacccccccccccaccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc Kid

Input format

One line without gaps containing the lowercase letters of the Latin alphabet, the numbers and square brackets & nbsp; [ & nbsp; and & nbsp; ] . It is guaranteed that the input line is always correct (there are no unclosed brackets, the figure always stands in front of an opening bracket, etc.).

Output format

One line & mdash; The result of complete unpacking.

Example

Input

3 [a] 2 [bc]

Output

aaabcbc

Hint

There will be no clue here, decide for yourself!

main.py
Test 1
Test 2
Test 3
Test 4
Test 5
Test 6
Test 7
Test 8
Test 9
Test 10
Developer’s solution
def unpack_string(s: str) -> str:
    """
    Распаковывает строку вида k[substring], включая вложенные структуры.
    """
    stack = []
    current_num = 0
    current_string = ''

    for char in s:
        if char.isdigit():
            # Собираем число, если оно многозначное (например, 10, 12)
            current_num = current_num * 10 + int(char)
        elif char == '[':
            # Начало новой подстроки. Сохраняем текущее состояние в стек.
            # Сохраняем текущую строку и число-повторитель для нее.
            stack.append(current_string)
            stack.append(current_num)
            # Сбрасываем переменные для нового уровня вложенности.
            current_string = ''
            current_num = 0
        elif char == ']':
            # Конец подстроки. Достаем из стека предыдущее состояние.
            num = stack.pop()
            prev_string = stack.pop()
            # Формируем новую текущую строку, повторяя распакованный фрагмент
            # и добавляя его к строке предыдущего уровня.
            current_string = prev_string + current_string * num
        else:
            # Просто добавляем букву к текущей строке.
            current_string += char
            
    return current_string


input_string = input()
result = unpack_string(input_string)
print(result)

🎉 Congratulations! 🎉

You did an excellent job with the task! It was a challenging problem, but you found the correct solution. You are one step closer to mastering programming! Keep up the good work, because every stage you pass makes you even stronger.

AD

Advertisement

red-snake blue-snake green-snake

Running your code...

Помощник ИИ

Привет! Я твой помощник по программированию. Задавай любые вопросы по Python, я могу рассказать о функциях, методах, обьяснить то, что тебе не понятно, а так же о текущей задаче!