• 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«Repeating characters»

💻 Python

you work in the quality control department at the electronics factory. Each device is assigned a unique serial number consisting of Latin letters and numbers. The hypothesis was recently put forward that serial numbers in which many repeating characters can be less reliable or more often faked.

rn

your task & mdash; Write a program for primary analysis. The program should determine how much & nbsp; of various & nbsp; symbols (letters or numbers) in the serial number are found more than once. This will help to automatically mark potentially “suspicious” numbers for further verification.

rn

technical requirements:

rn
    rn
  1. the program receives one line & mdash; Serial number.
  2. rn
  3. rn
  4. rn
  5. rn

    Input format

    serial number of the product (line, & nbsp; str ).

    Output format

    the number of different characters that are found in the line more than once (an integer, & nbsp; int ).

    Example

    Input

    abca11

    Output

    2

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

# Приводим всю строку к нижнему регистру, чтобы 'a' и 'A' считались одним и тем же символом
input_string = input_string.lower()

# Счетчик для символов, которые встречаются более одного раза
duplicate_count = 0
# Строка для хранения уже проверенных уникальных символов, чтобы не считать один и тот же символ дважды (например, в строке 'aaa' символ 'a' повторяется, но это всего 1 тип символа)
processed_characters = ""

# Начинаем цикл: перебираем каждый символ в полученной строке
for char in input_string:
    # Проверяем, обрабатывали ли мы уже этот тип символа ранее
    if char in processed_characters:
        # Если да, то мы его уже учли, поэтому переходим к следующему символу в строке
        continue

    # Если это новый для нас символ, считаем, сколько раз он встречается во всей строке
    if input_string.count(char) > 1:
        # Если символ встречается больше одного раза, увеличиваем наш счетчик дубликатов
        duplicate_count += 1
    
    # Добавляем текущий символ в строку уже обработанных, чтобы не проверять его снова
    processed_characters += char

# Выводим итоговый результат
print(duplicate_count)

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