• 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«Word order»

you & mdash; Programmer at the flight control center. From our new Kuriositi-2 Marshore, an important message has come about the state of its systems. Unfortunately, due to a solar outbreak, the word order in the message was broken. The engineers, anticipating this situation, built into each word the number from 1 to 9, which indicates the correct position of the word in the sentence. Your task & mdash; Write a program that will restore the initial order of words and decipher the message.

rn

technical task:
One line is supplied to the input program consisting of words separated by spaces. Each word is guaranteed to contain one digit from 1 to 9. It is necessary to sort the words in the line in accordance with these numbers and derive the result.

rn

restrictions:

rn
    rn
  • rn
  • the words consist only of the letters of the Russian alphabet and one digit. 14pt; "> the numbers in the input line are always consistent and correct (for example, if words are 4, then they will have numbers 1, 2, 3, 4).
  • rn
  • You can use only basic designs: input-outdoor, conditional operators ( if/else ) and cycles & nbsp; for . You can not import modules or create functions.
  • rn

Input format

The encrypted message from the Marshod (string, Str).

Output format

Decrypted, meaningful message (string, str).

Example

Input

О2 B4

Output

e1tot O2

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
# Получаем зашифрованную строку от пользователя
scrambled_message = input()

# Проверяем, не является ли строка пустой. Если пустая, то и результат - пустая строка.
if not scrambled_message:
    print("")
else:
    # Разделяем строку на отдельные слова по пробелу
    words = scrambled_message.split()
    
    # Создаем пустой список, в который будем складывать слова в правильном порядке.
    # Его размер равен количеству слов во входной строке.
    result_list = [''] * len(words)
    
    # Начинаем цикл по каждому слову в списке перемешанных слов
    for word in words:
        # Внутри этого цикла ищем цифру в текущем слове
        for char in word:
            # Проверяем, является ли текущий символ цифрой
            if char.isdigit():
                # Если это цифра, превращаем ее в число
                position = int(char)
                # Так как нумерация в задаче с 1, а индексы в списке с 0, вычитаем единицу
                index = position - 1
                # Помещаем текущее слово в результирующий список на правильную позицию
                result_list[index] = word
                # Мы нашли цифру, можно прекратить проверку символов в этом слове и перейти к следующему
                break
    
    # Соединяем отсортированные слова из списка обратно в одну строку через пробел
    final_message = " ".join(result_list)
    
    # Выводим итоговое, расшифрованное сообщение
    print(final_message)

🎉 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, я могу рассказать о функциях, методах, обьяснить то, что тебе не понятно, а так же о текущей задаче!