diff --git a/Lab_python/lab2/lab2.1/task_1.py b/Lab_python/lab2/lab2.1/task_1.py new file mode 100644 index 0000000..235b86f --- /dev/null +++ b/Lab_python/lab2/lab2.1/task_1.py @@ -0,0 +1,23 @@ +text = input("Введите текст: ").strip() + +if not text: + print("Текст не введен!") +else: + + words = text.split() + word_count = {} + + for word in words: + word_lower = word.lower() + if word_lower in word_count: + word_count[word_lower] += 1 + else: + word_count[word_lower] = 1 + + unique_words_count = len(word_count) + + print("Словарь {слово: количество}:") + for word, count in word_count.items(): + print(f" '{word}': {count}") + + print(f"Количество уникальных слов: {unique_words_count}") \ No newline at end of file diff --git a/Lab_python/lab2/lab2.1/task_2.py b/Lab_python/lab2/lab2.1/task_2.py new file mode 100644 index 0000000..5e73e30 --- /dev/null +++ b/Lab_python/lab2/lab2.1/task_2.py @@ -0,0 +1,69 @@ +user_input = input("Введите числа через пробел: ") + +if not user_input: + print("Вы ничего не ввели!") +else: + items = user_input.split() + numbers = [] + + for item in items: + if item.replace('.', '').replace('-', '').isdigit(): + if '.' in item: + numbers.append(float(item)) + else: + numbers.append(int(item)) + else: + print(f"'{item}' - не число, пропускаем") + + if not numbers: + print("Нет чисел для анализа!") + else: + print("РЕЗУЛЬТАТЫ:") + + unique = [] + for num in numbers: + if num not in unique: + unique.append(num) + print(f"1. Уникальные числа: {unique}") + + repeats = [] + for num in unique: + if numbers.count(num) > 1: + repeats.append(num) + print(f"2. Повторяющиеся: {repeats if repeats else 'нет'}") + + even = [] + odd = [] + for num in numbers: + if isinstance(num, int): + if num % 2 == 0: + even.append(num) + else: + odd.append(num) + print(f"3. Четные: {even if even else 'нет'}") + print(f" Нечетные: {odd if odd else 'нет'}") + + negative = [] + for num in numbers: + if num < 0: + negative.append(num) + print(f"4. Отрицательные: {negative if negative else 'нет'}") + + floats = [] + for num in numbers: + if isinstance(num, float): + floats.append(num) + print(f"5. Дробные: {floats if floats else 'нет'}") + + multiples_5 = [] + for num in numbers: + if num % 5 == 0: + multiples_5.append(num) + total_5 = sum(multiples_5) + print(f"6. Сумма кратных 5: {total_5}") + print(f" Числа: {multiples_5}") + + print(f"7. Самое большое: {max(numbers)}") + print(f"8. Самое маленькое: {min(numbers)}") + + print(f"\nВсе числа: {numbers}") \ No newline at end of file diff --git a/Lab_python/lab2/lab2.1/task_3.py b/Lab_python/lab2/lab2.1/task_3.py new file mode 100644 index 0000000..e7c1b2f --- /dev/null +++ b/Lab_python/lab2/lab2.1/task_3.py @@ -0,0 +1,30 @@ +user_input = input("Введите числа через пробел: ") + +if not user_input: + print("Вы ничего не ввели") +else: + items = user_input.split() + numbers = [] + + for item in items: + if item.replace('.', '').replace('-', '').isdigit(): + if '.' in item: + numbers.append(float(item)) + else: + numbers.append(int(item)) + else: + print(f"'{item}' - не число") + + if not numbers: + print("Нет чисел") + else: + if len(numbers) < 2: + print("Нужно ввести хотя бы 2 числа") + else: + unique_sorted = sorted(set(numbers)) + + if len(unique_sorted) < 2: + print("Все числа одинаковые") + else: + second_largest = unique_sorted[-2] + print(f"Второе по величине число: {second_largest}") \ No newline at end of file diff --git a/Lab_python/lab2/lab2.1/task_4.py b/Lab_python/lab2/lab2.1/task_4.py new file mode 100644 index 0000000..52b1ed2 --- /dev/null +++ b/Lab_python/lab2/lab2.1/task_4.py @@ -0,0 +1,40 @@ +input1 = input("Введите первый набор чисел через пробел: ") +input2 = input("Введите второй набор чисел через пробел: ") + +numbers1 = [] +for item in input1.split(): + if item.replace('.', '').replace('-', '').isdigit(): + numbers1.append(float(item) if '.' in item else int(item)) + +numbers2 = [] +for item in input2.split(): + if item.replace('.', '').replace('-', '').isdigit(): + numbers2.append(float(item) if '.' in item else int(item)) + +if numbers1 and numbers2: + print("\nРЕЗУЛЬТАТЫ:") + + common = [] + for num in numbers1: + if num in numbers2 and num not in common: + common.append(num) + print(f"1. Общие числа: {common}") + + only1 = [] + for num in numbers1: + if num not in numbers2: + only1.append(num) + + only2 = [] + for num in numbers2: + if num not in numbers1: + only2.append(num) + + print(f"2. Только в первом: {only1}") + print(f" Только во втором: {only2}") + + all_except_common = only1 + only2 + print(f"3. Все кроме общих: {all_except_common}") + +else: + print("Нужно ввести числа в оба набора!") \ No newline at end of file diff --git a/Lab_python/lab2/lab2.1/task_5.py b/Lab_python/lab2/lab2.1/task_5.py new file mode 100644 index 0000000..d6e0392 --- /dev/null +++ b/Lab_python/lab2/lab2.1/task_5.py @@ -0,0 +1,12 @@ +word1 = input("Введите первое слово: ").strip().lower() +word2 = input("Введите второе слово: ").strip().lower() + +if word1 and word2: + if sorted(word1) == sorted(word2): + print("True") + print(f"Слова '{word1}' и '{word2}' являются анаграммами!") + else: + print("False") + print(f"Слова '{word1}' и '{word2}' НЕ являются анаграммами") +else: + print("Нужно ввести оба слова!") \ No newline at end of file diff --git a/Lab_python/lab2/lab2.1/task_6.py b/Lab_python/lab2/lab2.1/task_6.py new file mode 100644 index 0000000..d38c253 --- /dev/null +++ b/Lab_python/lab2/lab2.1/task_6.py @@ -0,0 +1,14 @@ +user_input = input("Введите элементы списка через пробел: ") + +if not user_input: + print("Вы ничего не ввели!") +else: + items = user_input.split() + unique_items = [] + + for item in items: + if item not in unique_items: + unique_items.append(item) + + print(f"Исходный список: {items}") + print(f"Список без дубликатов: {unique_items}") \ No newline at end of file diff --git a/Lab_python/lab2/lab2.1/task_7.py b/Lab_python/lab2/lab2.1/task_7.py new file mode 100644 index 0000000..053473f --- /dev/null +++ b/Lab_python/lab2/lab2.1/task_7.py @@ -0,0 +1,22 @@ +text = input("Введите строку: ") + +if not text: + print("Вы ничего не ввели!") +else: + if len(text) == 1: + print(f"Результат: {text}1") + else: + result = "" + count = 1 + + for i in range(len(text) - 1): + if text[i] == text[i + 1]: + count += 1 + else: + result += text[i] + str(count) + count = 1 + + result += text[-1] + str(count) + + print(f"Исходная строка: {text}") + print(f"Сжатая строка: {result}") \ No newline at end of file