""" Урок 21. Практикум 5 — весь код примеров одним файлом. Источник: subjects/python-fundamentals/course/lessons/21-practice-5/examples.html Файл собран автоматически (tools/build_lesson_examples.py): правьте страницу урока. Запуск: python lesson-21.py """ # ==================================================================== # Пример 1: сумма чисел из строки # ==================================================================== data = "1 2 3 4 5" numbers = [int(x) for x in data.split()] print(f"Сумма чисел: {sum(numbers)}") # ==================================================================== # Пример 2: обратный список # ==================================================================== data = "1 2 3 4 5" numbers = [int(x) for x in data.split()] reversed_numbers = numbers[::-1] print(f"Обратный список: {reversed_numbers}") # ==================================================================== # Пример 3: максимальное число без max() # ==================================================================== numbers = [3, 12, 8, 22, 9, 25, 6, 23, 8, 7] max_number = numbers[0] max_index = 0 for index, number in enumerate(numbers): if number > max_number: max_number = number max_index = index print("Список чисел:", numbers) print("Наибольшее число:", max_number) print("Индекс числа:", max_index) # ==================================================================== # Пример 4: перестановка соседних элементов # ==================================================================== numbers = [1, 2, 3, 4, 5] for i in range(0, len(numbers) - 1, 2): numbers[i], numbers[i + 1] = numbers[i + 1], numbers[i] print(numbers) # ==================================================================== # Пример 5: числа и слова # ==================================================================== text = "apple 42 banana 3 100 orange" result = [] for item in text.split(): if item.isdigit(): result.append(int(item)) else: result.append(item.title()) print(f"Изначальная строка: {text}") print(f"Результат: {result}") # ==================================================================== # Пример 6: кодирование строки (RLE) # ==================================================================== text = "aaaabbc" encoded = "" count = 1 for i in range(1, len(text)): if text[i] == text[i - 1]: count += 1 else: encoded += text[i - 1] + str(count) count = 1 encoded += text[-1] + str(count) print(f"Закодированная строка: {encoded}") # ==================================================================== # Пример 7: звёздочки вместо цифр # ==================================================================== text = "My number is 123-456-789" result = "".join("*" if char.isdigit() else char for char in text) print(f"Строка: {text}") print(f"Результат: {result}") # ==================================================================== # Пример 8: развернуть каждое слово # ==================================================================== text = "Python is fun" words = text.split() reversed_words = [] for word in words: reversed_words.append(word[::-1]) result = " ".join(reversed_words) print(f"Строка: {text}") print(f"Результат: {result}") # ==================================================================== # Пример 9: один пробел вместо нескольких # ==================================================================== text = " Hello, World! How are you? " result = " ".join(text.split()) print(f"Строка: {text!r}") print(f"Результат: {result!r}") # ==================================================================== # Пример 10: все позиции подстроки # ==================================================================== text = "Banana bAnana baNAna" substring = "ban" text_lower = text.lower() positions = [] start = 0 while True: position = text_lower.find(substring, start) if position == -1: break positions.append(position) start = position + 1 print("Строка:", text) print("Подстрока:", substring) for position in positions: print("Позиция:", position) # ==================================================================== # Пример 11: корректные имена # ==================================================================== names = ["Alice", "bob", "Charlie", "dave", "Eva", "Frank123"] valid_names = [] for name in names: if name.isalpha() and name[0].isupper(): valid_names.append(name) print("Имена с заглавной буквы и без цифр:") for name in valid_names: print(name)