1 |
def bubble_sort(lst): n = len(lst) for i in range(n): for j in range(0, n-i-1): if lstj > lstj+1: lstj, lstj+1 = lstj+1, lstj return lst lst = 64, 34, 25, 12, 22, 11, 90 lst = bubble_sort(lst) print("Отсортированный список:", lst) |
2 |
def is_anagram(str1, str2): if len(str1) != len(str2): return False else: str1 = sorted(str1) str2 = sorted(str2) return str1 == str2 str1 = input("Введите первую строку: ") str2 = input("Введите вторую строку: ") if is_anagram(str1, str2): print("Строки являются анаграммами.") else: print("Строки не являются анаграммами.") |
3 |
def count_words(text): words = text.lower().split() word_count = {} for word in words: if word in word_count: word_countword += 1 else: word_countword = 1 return word_count text = input("Введите текст: ") word_count = count_words(text) print("Частота слов:") for word, count in word_count.items(): print(f"{word}: {count}") |
4 |
def is_prime(n): if n <= 1 or n % 1 != 0: return False else: for i in range(2, n): if n % i == 0: return False return True start, end = map(int, input("Введите диапазон (через пробел): ").split()) for num in range(start, end + 1): if is_prime(num): print(num, end=" ") |
5 |
def sort_dict_by_values(d): return {k: v for k, v in sorted(d.items(), key=lambda item: item1)} d = { "a": 3, "b": 1, "c": 4, "d": 2 } sorted_d = sort_dict_by_values(d) print("Словарь, отсортированный по значениям:") print(sorted_d) |
6 |
def unique_subsets(lst): n = len(lst) subsets = * (2**n) for i in range(2**n): subset = for j in range(n): if i & (1 << j): subset.append(lstj) subsetsi = subset return subsets lst = 1, 2, 3 unique_subsets = unique_subsets(lst) print("Уникальные подмножества:") for subset in unique_subsets: print(subset) |
7 |
def permutations(s): if len(s) == 1: return s else: permutations = for i in range(len(s)): for perm in permutations(s:i + si+1:)): permutations.append(si + perm) return permutations s = input("Введите строку: ") permutations = permutations |
8 |
def quick_sort(lst): if len(lst) < 2: return lst else: pivot = lst0 left = [i for i in lst if i < pivot] right = [i for i in lst if i > pivot] return quick_sort(left) + [pivot] + quick_sort(right) lst = 64, 34, 25, 12, 22, 11, 90 lst = quick_sort(lst) print("Отсортированный список:", lst) |
9 |
def unique_subsets_recursive(lst): subsets = [] subsets.append(set()) subsets.append(set(lst0)) for i in range(1, len(lst)): for subset in subsets: new_subset = subset | set(lsti) new_subset = new_subset - set(lsti) subsets.append(new_subset) return subsets lst = 1, 2, 3 unique_subsets = unique_subsets_recursive(lst) print("Уникальные подмножества:") for subset in unique_subsets: print(subset) |
10 |
def count_words(text): words = text.lower().split() word_count = {} for word in words: if word in word_count: word_countword += 1 else: word_countword = 1 return word_count text = input("Введите текст: ") word_count = count_words(text) print("Частота слов:") for word, count in word_count.items(): print(f"{word}: {count}") |
11 |
def is_prime(n): if n <= 1 or n % 1 != 0: return False else: for i in range(2, n): if n % i == 0: return False return True def generate_primes(start, end): for num in range(start, end + 1): if is_prime(num): yield num start, end = map(int, input("Введите диапазон (через пробел): ").split()) primes = generate_primes(start, end) print("Простые числа в диапазоне:", end=" ") for prime in primes: print(prime, end=" ") |
12 |
def sort_dict_by_values(d, reverse=False): def comparator(x, y): if x > y: return 1 elif x < y: return -1 return 0 return {k: v for k, v in sorted(d.items(), key=lambda item: item1, reverse=reverse)} d = { "a": 3, "b": 1, "c": 4, "d": 2 } sorted_d = sort_dict_by_values(d) print("Словарь, отсортированный по значениям:") print(sorted_d) |
13 |
def permutations(s): if len(s) == 1: return [s] else: permutations = [] for i in range(len(s)): for perm in permutations(s:i + si+1:)): permutations.append(si + perm) return permutations s = input("Введите строку: ") permutations = permutations(s) print("Все перестановки строки:", end=" ") for perm in permutations: print(perm, end=" ") |
14 |
|
Комментарии