• 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
  • к

Lesson 3. FOR cycle

Difficulty level:

Task«Filter of numbers»

💻 Python

imagine that you are working in the cybersecurity department. There was a failure on one of the critical servers, and as a result of this, part of the data was damaged. The biggest problem & mdash; A unique numerical identifier of a secret object was spoiled. Now it is a chaotic set of letters, numbers and symbols. Fortunately, it is known that the numbers themselves have not changed and their order has been preserved.

rn

your task & mdash; Write a program that can "clean" the damaged line and restore the original numerical code. The program should take one line with damaged data and extract all the numbers from it, preserving their initial order.

rn

Requirements for implementation:

To solve only input/output, cycle & nbsp; for & nbsp; and conditional operator & nbsp; if . You do not need to open files or use complex functions for processing strings.

Input format

damaged data line containing numbers, letters and any other characters (string, string).

Output format

a restored numerical code consisting only of numbers found in the original line in the same order (line, string). If there were no numbers in the original line, the program should derive an empty line.

Example

Input

ERW3423T!@#gfdg5567

Output

34235567

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
# Формат кода, с комментариями к строкам для объяснения

# Получаем на вход одну строку с поврежденными данными от пользователя
damaged_data = input()

# Создаем пустую строку, в которую будем собирать только найденные цифры
recovered_code = ""

# Запускаем цикл for, который перебирает каждый символ во входной строке
for symbol in damaged_data:
    # Внутри цикла используем условие if для проверки, является ли текущий символ цифрой.
    # Метод isdigit() возвращает True, если все символы в строке являются цифрами, и False в противном случае.
    # Так как мы проверяем по одному символу, он сработает как нам нужно.
    if symbol.isdigit():
        # Если символ является цифрой, мы добавляем (конкатенируем) его в конец нашей строки с результатом
        recovered_code = recovered_code + symbol

# После того как цикл завершится, в переменной recovered_code будет строка, состоящая только из цифр.
# Выводим итоговую восстановленную строку на экран.
print(recovered_code)

🎉 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...

AI Helper

Hi! I am your programming assistant. Ask me anything about Python — I can explain functions, methods, and help you understand the current task!