├── README.md ├── basic_constructions_review ├── back_front_revert.py ├── bigger_than_previous.py ├── coordinate_quaters.py ├── developmental_shift.py ├── different_elements.py ├── heads_tails.py ├── josif_flaville_task.py ├── mass_body_idx.py ├── multiply_numbers.py ├── na_easy.py ├── revert_number.py ├── rock_paper_scissors.py ├── rock_paper_scissors_reptile_spok.py ├── roskomnadzor.py ├── silicon_valley.py ├── standard_american_convention.py ├── string_value.py ├── words_amount.py └── zodiac.py ├── bool_nontype_datatypes └── divisible_predicate.py ├── decimal_fraction_complex_module ├── complex_1.py ├── complex_operations.py ├── conjugate_nums.py ├── decimal_1.py ├── decimal_2.py ├── decimal_3.py ├── fraction_operations.py ├── fractions_1.py ├── fractions_2.py ├── fractions_sum_1.py ├── fractions_sum_2.py ├── math_expression.py ├── ordered_fractions.py ├── reduce_fraction.py └── young_mathematician.py ├── dicts ├── anagrams_1.py ├── anagrams_2.py ├── countries_and_towns.py ├── courses_info.py ├── dicts_1.py ├── dicts_10.py ├── dicts_11.py ├── dicts_12.py ├── dicts_13.py ├── dicts_14.py ├── dicts_15.py ├── dicts_16.py ├── dicts_17.py ├── dicts_18.py ├── dicts_19.py ├── dicts_2.py ├── dicts_3.py ├── dicts_4.py ├── dicts_5.py ├── dicts_6.py ├── dicts_7.py ├── dicts_8.py ├── dicts_9.py ├── duplicates_fix.py ├── messages_set.py ├── morse_code.py ├── phonebook.py ├── programming_dict.py ├── rarest_word.py ├── secret_word.py ├── string_representation.py └── synonyms_dict.py ├── dicts_exam ├── dangerous_virus.py ├── dicts_exam_1.py ├── dicts_exam_2.py ├── dicts_merge.py ├── genetics_minute.py ├── internet_shopping.py ├── request_string.py ├── scrabble_game.py └── serial_number.py ├── functions ├── anonymos_funcs_1.py ├── anonymos_funcs_10.py ├── anonymos_funcs_11.py ├── anonymos_funcs_12.py ├── anonymos_funcs_2.py ├── anonymos_funcs_3.py ├── anonymos_funcs_4.py ├── anonymos_funcs_5.py ├── anonymos_funcs_6.py ├── anonymos_funcs_7.py ├── anonymos_funcs_8.py ├── anonymos_funcs_9.py ├── built_in_funcs_1.py ├── built_in_funcs_2.py ├── correct_ip.py ├── excellent_students.py ├── functions_as_objects_1.py ├── functions_as_objects_2.py ├── functions_as_objects_3.py ├── good_password.py ├── high_order_funcs_1.py ├── high_order_funcs_2.py ├── high_order_funcs_3.py ├── high_order_funcs_4.py ├── high_order_funcs_5.py ├── inside_the_ball.py ├── interesting_nums.py ├── interesting_sort_1.py ├── interesting_sort_2.py ├── math_funcs.py ├── opposite_color.py ├── polynominal_value.py ├── sort_as_you_want.py ├── unnecessary_args.py ├── variable_nums_of_args_funcs_1.py ├── variable_nums_of_args_funcs_2.py ├── variable_nums_of_args_funcs_3.py ├── variable_nums_of_args_funcs_4.py ├── variable_nums_of_args_funcs_5.py └── variable_nums_of_args_funcs_6.py ├── functions_exam ├── exam_letter.py ├── functions_exam_1.py ├── functions_exam_2.py ├── functions_exam_3.py ├── functions_exam_4.py ├── functions_exam_5.py ├── functions_exam_6.py ├── functions_exam_7.py ├── functions_exam_8.py ├── in_one_string.py ├── ip_sort.py ├── pretty_print.py └── word_gematry.py ├── nested_lists ├── chess_board.py ├── chunk_splitting.py ├── collateral_diag.py ├── cols_changing.py ├── diag_filling.py ├── diags_swap.py ├── duplicate_packaging.py ├── filling_1.py ├── filling_2.py ├── filling_3.py ├── filling_4.py ├── filling_5.py ├── horse_move.py ├── magic_square.py ├── matrix_addition.py ├── matrix_multiplication.py ├── matrix_power.py ├── matrix_trace.py ├── matrix_turn.py ├── max_in_region_1.py ├── max_in_region_2.py ├── max_in_table.py ├── more_than_average.py ├── multiplication_table.py ├── nested_lists_1.py ├── nested_lists_2.py ├── nested_lists_3.py ├── nested_lists_4.py ├── nested_lists_5.py ├── pascal_triangle_1.py ├── pascal_triangle_2.py ├── print_matrix_1.py ├── print_matrix_2.py ├── reflection.py ├── snake_filling.py ├── spiral_filling.py ├── sublists_of_lists.py ├── sum_of_quaters.py ├── symmetric_matrix.py ├── template_list_1.py └── template_list_2.py ├── nested_lists_exam ├── diags_parallel_to_main.py ├── every_nth_elem.py ├── latin_square.py ├── matrix_transporting.py ├── max_in_region_2.py ├── queen_moves.py ├── snowflake.py └── symmetric_matrix.py ├── random_string ├── monte_carlo_1.py ├── monte_carlo_2.py ├── password_generator_1.py ├── password_generator_2.py ├── random_string_1.py ├── random_string_10.py ├── random_string_2.py ├── random_string_3.py ├── random_string_4.py ├── random_string_5.py ├── random_string_6.py ├── random_string_7.py ├── random_string_8.py ├── random_string_9.py └── secret_friend.py ├── sets ├── all_10_digits.py ├── all_digits.py ├── amount_of_words_in_text.py ├── biology_lesson.py ├── books_to_read.py ├── common_digits.py ├── common_numbers.py ├── dif_symbols_amount.py ├── first_string_sums.py ├── informatics_lesson.py ├── is_repeated_num.py ├── maths_lesson.py ├── non_repeated_digits.py ├── physics_lesson.py ├── repeated_amount.py ├── same_digits.py ├── same_sets.py ├── sets_1.py ├── sets_2.py ├── sets_3.py ├── sets_4.py ├── sets_generator_1.py ├── sets_generator_2.py ├── sets_generator_3.py ├── sets_generator_4.py ├── sets_generator_5.py ├── three_words.py ├── timur_team.py ├── unique_symbols_1.py └── unique_symbols_2.py ├── sets_exam ├── beegeek_school_1.py ├── beegeek_school_2.py ├── beegeek_school_3.py ├── beegeek_school_4.py ├── beegeek_school_5.py ├── beegeek_school_6.py ├── books_to_read.py ├── hometask.py ├── sets_exam_1.py ├── strange_infatuation.py ├── sunrise.py └── towns.py ├── tuples ├── competitive_selection.py ├── parabola_vertex.py ├── tribonacci.py ├── tuples_1.py ├── tuples_10.py ├── tuples_11.py ├── tuples_12.py ├── tuples_13.py ├── tuples_14.py ├── tuples_15.py ├── tuples_16.py ├── tuples_17.py ├── tuples_2.py ├── tuples_3.py ├── tuples_4.py ├── tuples_5.py ├── tuples_6.py ├── tuples_7.py ├── tuples_8.py └── tuples_9.py ├── work_with_files ├── christmas_present.py ├── csv_files.py ├── file_concat.py ├── file_content.py ├── file_statistics.py ├── flip_line.py ├── input_string.py ├── jacque_fresco_task.py ├── line_numbering.py ├── log_file.py ├── long_lines.py ├── penultimate_line.py ├── random_name_surname.py ├── random_nums.py ├── random_string.py ├── reverse_order.py ├── sum_of_nums_in_file.py ├── sum_of_nums_in_lines.py ├── sum_of_two_1.py ├── sum_of_two_2.py ├── total_cost.py └── unusual_countries.py └── work_with_files_exam ├── forbidden_words.py ├── good_students.py ├── missed_comments.py ├── num_of_lines_in_file.py ├── sum_cost.py ├── tail_of_a_file.py ├── the_longest_word_in_file.py └── transliteration.py /README.md: -------------------------------------------------------------------------------- 1 | # python_advanced_course 2 | Solutions "Python advanced" https://stepik.org/course/68343/syllabus 3 | -------------------------------------------------------------------------------- /basic_constructions_review/back_front_revert.py: -------------------------------------------------------------------------------- 1 | def swapPositions(list, pos1, pos2): 2 | 3 | list[pos1], list[pos2] = list[pos2], list[pos1] 4 | return list 5 | 6 | s = input() 7 | 8 | l = s.split() 9 | 10 | for i in range(len(l)//2): 11 | l = swapPositions(l, i*2, i*2 + 1) 12 | 13 | print(*l) -------------------------------------------------------------------------------- /basic_constructions_review/bigger_than_previous.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | counter = 0 3 | 4 | for i in range(len(s.split()) - 1): 5 | if int(s.split()[i]) < int(s.split()[i + 1]): 6 | counter += 1 7 | 8 | print(counter) -------------------------------------------------------------------------------- /basic_constructions_review/coordinate_quaters.py: -------------------------------------------------------------------------------- 1 | numDots = int(input()) 2 | 3 | dotsQuarters = {"Первая четверть" : 0, "Вторая четверть" : 0, "Третья четверть" : 0, "Четвертая четверть" : 0} 4 | 5 | for i in range(numDots): 6 | [x,y] = input().split() 7 | x = int(x) 8 | y = int(y) 9 | if x > 0 and y > 0: 10 | dotsQuarters["Первая четверть"] += 1 11 | elif x < 0 and y > 0: 12 | dotsQuarters["Вторая четверть"] += 1 13 | elif x < 0 and y < 0: 14 | dotsQuarters["Третья четверть"] += 1 15 | elif x > 0 and y < 0: 16 | dotsQuarters["Четвертая четверть"] += 1 17 | 18 | for key, value in dotsQuarters.items(): 19 | print(key + ": " + str(value)) -------------------------------------------------------------------------------- /basic_constructions_review/developmental_shift.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | 3 | print(s.split()[-1],*s.split()[:-1]) -------------------------------------------------------------------------------- /basic_constructions_review/different_elements.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | 3 | print(len(set(s.split()))) -------------------------------------------------------------------------------- /basic_constructions_review/heads_tails.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | 3 | counter = 0 4 | maxCounter = 0 5 | 6 | for elem in s: 7 | if elem == 'О': 8 | if (counter > maxCounter): 9 | maxCounter = counter 10 | counter = 0 11 | else: 12 | counter += 1; 13 | 14 | print(maxCounter if maxCounter > counter else counter) -------------------------------------------------------------------------------- /basic_constructions_review/josif_flaville_task.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | k = int(input()) 3 | 4 | people = [i for i in range(1, n+1)] 5 | popIdx = 0 6 | 7 | while (len(people) > 1): 8 | popIdx = (popIdx + k) % len(people)-1 if (popIdx + k) % len(people)-1 != -1 else len(people) - 1; 9 | people.pop(popIdx) 10 | 11 | print(people[0]) -------------------------------------------------------------------------------- /basic_constructions_review/mass_body_idx.py: -------------------------------------------------------------------------------- 1 | mass = float(input()) 2 | height = float(input()) 3 | 4 | bmi = mass / pow(height, 2) 5 | 6 | if bmi < 18.5: 7 | print('Недостаточная масса') 8 | elif bmi <= 25: 9 | print('Оптимальная масса') 10 | else: 11 | print('Избыточная масса') -------------------------------------------------------------------------------- /basic_constructions_review/multiply_numbers.py: -------------------------------------------------------------------------------- 1 | def findMult(arr): 2 | for i in range(len(arr)): 3 | for j in range(len(arr)): 4 | if (i != j) and arr[i] * arr[j] == num: 5 | print('ДА') 6 | return 7 | print('НЕТ') 8 | 9 | n = int(input()) 10 | arr = [] 11 | 12 | for i in range(n): 13 | arr.append(int(input())) 14 | 15 | num = int(input()) 16 | 17 | findMult(arr) -------------------------------------------------------------------------------- /basic_constructions_review/na_easy.py: -------------------------------------------------------------------------------- 1 | from math import sqrt 2 | 3 | a = int(input()) 4 | b = int(input()) 5 | 6 | print(a+b) 7 | print(a-b) 8 | print(a*b) 9 | print(a/b) 10 | print(a//b) 11 | print(a%b) 12 | print(sqrt(pow(a,10) + pow(b,10))) -------------------------------------------------------------------------------- /basic_constructions_review/revert_number.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | 3 | print(int(s[:-5] + s[:-6:-1])) -------------------------------------------------------------------------------- /basic_constructions_review/rock_paper_scissors.py: -------------------------------------------------------------------------------- 1 | def StoneScissorsPaper(first, second): 2 | if first == 'камень' and second == 'бумага': 3 | print('Руслан') 4 | elif first == 'камень' and second == 'ножницы': 5 | print('Тимур') 6 | elif first == 'бумага' and second == 'камень': 7 | print('Тимур') 8 | elif first == 'бумага' and second == 'ножницы': 9 | print('Руслан') 10 | elif first == 'ножницы' and second == 'камень': 11 | print('Руслан') 12 | elif first == 'ножницы' and second == 'бумага': 13 | print('Тимур') 14 | 15 | timurChoice = input() 16 | ruslanChoice = input() 17 | 18 | if (timurChoice == ruslanChoice): 19 | print('ничья') 20 | 21 | StoneScissorsPaper(timurChoice, ruslanChoice) -------------------------------------------------------------------------------- /basic_constructions_review/rock_paper_scissors_reptile_spok.py: -------------------------------------------------------------------------------- 1 | d = {'камень-камень': 'ничья', 'камень-ножницы': 'Тимур', 'камень-бумага': 'Руслан', 2 | 'камень-ящерица': 'Тимур', 'камень-Спок': 'Руслан', 'ножницы-ножницы': 'ничья', 3 | 'ножницы-бумага': 'Тимур', 'ножницы-камень': 'Руслан', 'ножницы-ящерица': 'Тимур', 4 | 'ножницы-Спок': 'Руслан', 'бумага-бумага': 'ничья', 'бумага-камень': 'Тимур', 5 | 'бумага-ножницы': 'Руслан', 'бумага-ящерица': 'Руслан', 'бумага-Спок': 'Руслан', 6 | 'ящерица-ящерица': 'ничья', 'ящерица-Спок': 'Тимур', 'ящерица-ножницы': 'Руслан', 7 | 'ящерица-бумага': 'Тимур', 'ящерица-камень': 'Руслан', 'Спок-Спок': 'ничья', 8 | 'Спок-ножницы': 'Тимур', 'Спок-бамага': 'Руслан', 'Спок-камень': 'Тимур', 9 | 'Спок-ящерица': 'Руслан'} 10 | 11 | timurChoice = input() 12 | ruslanChoice = input() 13 | 14 | print(d[timurChoice + '-' + ruslanChoice]) -------------------------------------------------------------------------------- /basic_constructions_review/roskomnadzor.py: -------------------------------------------------------------------------------- 1 | word = input() + ' запретил букву' 2 | 3 | letters = ['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ы', 'ь', 'э', 'ю', 'я'] 4 | 5 | for elem in letters: 6 | if elem in word: 7 | print(word + ' ' + elem) 8 | word = word.replace(elem, "") 9 | word = word.replace(" ", " ") 10 | if word.startswith(' '): 11 | word = word[1:] 12 | if word.endswith(' '): 13 | word = word[:-1] 14 | -------------------------------------------------------------------------------- /basic_constructions_review/silicon_valley.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | def FindAnton(name, num): 4 | if re.match(r'[\w\W]*a[\w\W]*n[\w\W]*t[\w\W]*o[\w\W]*n[\w\W]*', name): 5 | return str(num) 6 | return '' 7 | 8 | n = int(input()) 9 | s = '' 10 | 11 | for i in range(n): 12 | fridgeName = input() 13 | s += FindAnton(fridgeName, i + 1) 14 | 15 | print(' '.join(s)) -------------------------------------------------------------------------------- /basic_constructions_review/standard_american_convention.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | result = '' 3 | 4 | for i in range(len(s)): 5 | result += s[::-1][i] 6 | if i%3==2 and i+1 != len(s): 7 | result += ',' 8 | 9 | print(result[::-1]) -------------------------------------------------------------------------------- /basic_constructions_review/string_value.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | value = len(s) * 0.6 3 | 4 | print(str(int(value)) + ' р. ' + str(round(value%1*100)) + ' коп.') -------------------------------------------------------------------------------- /basic_constructions_review/words_amount.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | 3 | print(len(s.split())) -------------------------------------------------------------------------------- /basic_constructions_review/zodiac.py: -------------------------------------------------------------------------------- 1 | d = {8: 'Дракон', 9: 'Змея', 10: 'Лошадь', 11: 'Овца', 0: 'Обезьяна', 1: 'Петух', 2: 'Собака', 3: 'Свинья', 4: 'Крыса', 5: 'Бык', 6: 'Тигр', 7: 'Заяц'} 2 | 3 | year = int(input()) 4 | 5 | print(d[year%12]) -------------------------------------------------------------------------------- /bool_nontype_datatypes/divisible_predicate.py: -------------------------------------------------------------------------------- 1 | # объявление функции 2 | def func(num1, num2): 3 | if num1 % num2 == 0: 4 | return True 5 | return False 6 | 7 | # считываем данные 8 | num1, num2 = int(input()), int(input()) 9 | 10 | # вызываем функцию 11 | if func(num1, num2): 12 | print('делится') 13 | else: 14 | print('не делится') -------------------------------------------------------------------------------- /decimal_fraction_complex_module/complex_1.py: -------------------------------------------------------------------------------- 1 | numbers = [3 + 4j, 3 + 1j, -7 + 3j, 4 + 8j, -8 + 10j, -3 + 2j, 3 - 2j, -9 + 9j, -1 - 1j, -1 - 10j, -20 + 15j, -21 + 1j, 1j, -3 + 8j, 4 - 6j, 8 + 2j, 2 + 3j] 2 | 3 | maxModule = 0 4 | maxElem = complex() 5 | 6 | for elem in numbers: 7 | if abs(elem) > maxModule: 8 | maxElem = elem 9 | maxModule = abs(elem) 10 | 11 | print(maxElem) 12 | print(maxModule) 13 | -------------------------------------------------------------------------------- /decimal_fraction_complex_module/complex_operations.py: -------------------------------------------------------------------------------- 1 | z1 = complex(input()) 2 | z2 = complex(input()) 3 | 4 | print(str(z1) + ' + ' + str(z2) + ' =', z1 + z2) 5 | print(str(z1) + ' - ' + str(z2) + ' =', z1 - z2) 6 | print(str(z1) + ' * ' + str(z2) + ' =', z1 * z2) 7 | -------------------------------------------------------------------------------- /decimal_fraction_complex_module/conjugate_nums.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | z1 = complex(input()) 3 | z2 = complex(input()) 4 | 5 | print(pow(z1, n) + pow(z2, n) + pow(z1.conjugate(), n) + pow(z2.conjugate(), n + 1)) 6 | -------------------------------------------------------------------------------- /decimal_fraction_complex_module/decimal_1.py: -------------------------------------------------------------------------------- 1 | from decimal import * 2 | 3 | s = '0.77 4.03 9.06 3.80 7.08 5.88 0.23 4.65 2.79 0.90 4.23 2.15 3.24 8.57 0.10 8.57 1.49 5.64 3.63 8.36 1.56 6.67 1.46 5.26 4.83 7.23 1.22 1.02 7.82 9.97 5.40 9.79 9.82 2.78 2.96 0.07 1.72 7.24 7.84 9.23 1.71 6.24 5.78 5.37 0.03 9.60 8.86 2.73 5.83 6.50' 4 | 5 | 6 | arr = [Decimal(elem) for elem in s.split()] 7 | print((max(arr) + min(arr))) 8 | -------------------------------------------------------------------------------- /decimal_fraction_complex_module/decimal_2.py: -------------------------------------------------------------------------------- 1 | from decimal import * 2 | 3 | s = '9.73 8.84 8.92 9.60 9.32 8.97 8.53 1.26 6.62 9.85 1.85 1.80 0.83 6.75 9.74 9.11 9.14 5.03 5.03 1.34 3.52 8.09 7.89 8.24 8.23 5.22 0.30 2.59 1.25 6.24 2.14 7.54 5.72 2.75 2.32 2.69 9.32 8.11 4.53 0.80 0.08 9.36 5.22 4.08 3.86 5.56 1.43 8.36 6.29 5.13' 4 | 5 | arr = [Decimal(elem) for elem in s.split()] 6 | 7 | print(sum(arr)) 8 | 9 | print(*[str(elem) for elem in sorted(arr, reverse=True)[:5]]) 10 | -------------------------------------------------------------------------------- /decimal_fraction_complex_module/decimal_3.py: -------------------------------------------------------------------------------- 1 | from decimal import * 2 | num = Decimal(input()) 3 | 4 | arr = sorted([int(elem) for elem in str(num) if elem.isdigit()]) 5 | 6 | print(arr[0] + arr[-1]) 7 | -------------------------------------------------------------------------------- /decimal_fraction_complex_module/fraction_operations.py: -------------------------------------------------------------------------------- 1 | from fractions import Fraction 2 | 3 | m = input() 4 | n = input() 5 | 6 | print(m + ' + ' + n + ' = ' + str(Fraction(m) + Fraction(n))) 7 | print(m + ' - ' + n + ' = ' + str(Fraction(m) - Fraction(n))) 8 | print(m + ' * ' + n + ' = ' + str(Fraction(m) * Fraction(n))) 9 | print(m + ' / ' + n + ' = ' + str(Fraction(m) / Fraction(n))) 10 | -------------------------------------------------------------------------------- /decimal_fraction_complex_module/fractions_1.py: -------------------------------------------------------------------------------- 1 | from fractions import Fraction 2 | 3 | numbers = ['6.34', '4.08', '3.04', '7.49', '4.45', '5.39', '7.82', '2.76', '0.71', '1.97', '2.54', '3.67', '0.14', '4.29', '1.84', '4.07', '7.26', '9.37', '8.11', '4.30', '7.16', '2.46', '1.27', '0.29', '5.12', '4.02', '6.95', '1.62', '2.26', '0.45', '6.91', '7.39', '0.52', '1.88', '8.38', '0.75', '0.32', '4.81', '3.31', '4.63', '7.84', '2.25', '1.10', '3.35', '2.05', '7.87', '2.40', '1.20', '2.58', '2.46'] 4 | 5 | for elem in numbers: 6 | print(elem + ' = ' + str(Fraction(elem))) 7 | -------------------------------------------------------------------------------- /decimal_fraction_complex_module/fractions_2.py: -------------------------------------------------------------------------------- 1 | from fractions import Fraction 2 | 3 | s = '0.78 4.3 9.6 3.88 7.08 5.88 0.23 4.65 2.79 0.90 4.23 2.15 3.24 8.57 0.10 8.57 1.49 5.64 3.63 8.36 1.56 6.67 1.46 5.26 4.83 7.13 1.22 1.02 7.82 9.97 5.40 9.79 9.82 2.78 2.96 0.07 1.72 7.24 7.84 9.23 1.71 6.24 5.78 5.37 0.03 9.60 8.86 2.73 5.83 6.50 0.123 0.00021' 4 | 5 | arr = [float(elem) for elem in s.split()] 6 | print(Fraction(str(max(arr) + min(arr)))) 7 | -------------------------------------------------------------------------------- /decimal_fraction_complex_module/fractions_sum_1.py: -------------------------------------------------------------------------------- 1 | from fractions import Fraction 2 | 3 | summa = Fraction(0) 4 | n = int(input()) 5 | 6 | for i in range(1, n+1): 7 | summa += Fraction(1, pow(i, 2)) 8 | print(summa) 9 | -------------------------------------------------------------------------------- /decimal_fraction_complex_module/fractions_sum_2.py: -------------------------------------------------------------------------------- 1 | from fractions import Fraction 2 | 3 | def factorial(i): 4 | if i <=1: 5 | return 1 6 | else: 7 | return factorial(i - 1) * i 8 | 9 | summa = Fraction(0) 10 | n = int(input()) 11 | 12 | for i in range(1, n+1): 13 | summa += Fraction(1, factorial(i)) 14 | print(summa) 15 | -------------------------------------------------------------------------------- /decimal_fraction_complex_module/math_expression.py: -------------------------------------------------------------------------------- 1 | from decimal import * 2 | num = Decimal(input()) 3 | 4 | print(num.exp() + num.ln() + num.log10() + num.sqrt()) 5 | -------------------------------------------------------------------------------- /decimal_fraction_complex_module/ordered_fractions.py: -------------------------------------------------------------------------------- 1 | from math import gcd 2 | from fractions import Fraction 3 | 4 | n = int(input()) 5 | result = [] 6 | 7 | for i in range(1, n+1): 8 | for j in range(1, i): 9 | if gcd(i, j) == 1: 10 | result.append(Fraction(j, i)) 11 | 12 | for elem in sorted(result): 13 | print(elem) 14 | -------------------------------------------------------------------------------- /decimal_fraction_complex_module/reduce_fraction.py: -------------------------------------------------------------------------------- 1 | from fractions import Fraction 2 | 3 | m = int(input()) 4 | n = int(input()) 5 | 6 | print(Fraction(m, n)) 7 | -------------------------------------------------------------------------------- /decimal_fraction_complex_module/young_mathematician.py: -------------------------------------------------------------------------------- 1 | from math import gcd 2 | from fractions import Fraction 3 | 4 | n = int(input()) 5 | 6 | for i in range(1, n//2+1): 7 | if gcd(i, n - i) == 1: 8 | result = Fraction(i, n-i) 9 | print(result) 10 | -------------------------------------------------------------------------------- /dicts/anagrams_1.py: -------------------------------------------------------------------------------- 1 | word_1 = {} 2 | word_2 = {} 3 | 4 | for elem in input(): 5 | if elem in word_1: 6 | word_1[elem] += 1 7 | else: 8 | word_1[elem] = 1 9 | 10 | for elem in input(): 11 | if elem in word_2: 12 | word_2[elem] += 1 13 | else: 14 | word_2[elem] = 1 15 | 16 | print('YES' if word_1 == word_2 else 'NO') 17 | -------------------------------------------------------------------------------- /dicts/anagrams_2.py: -------------------------------------------------------------------------------- 1 | word_1 = {} 2 | word_2 = {} 3 | 4 | for word in [elem.strip('.,!?:;-') for elem in input().lower().split()]: 5 | for elem in word: 6 | if elem in word_1: 7 | word_1[elem] += 1 8 | else: 9 | word_1[elem] = 1 10 | 11 | for word in [elem.strip('.,!?:;-') for elem in input().lower().split()]: 12 | for elem in word: 13 | if elem in word_2: 14 | word_2[elem] += 1 15 | else: 16 | word_2[elem] = 1 17 | 18 | print('YES' if word_1 == word_2 else 'NO') 19 | -------------------------------------------------------------------------------- /dicts/countries_and_towns.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | result = {} 3 | 4 | for i in range(n): 5 | arr = input().split() 6 | for j in range(1, len(arr)): 7 | result[arr[j]] = arr[0] 8 | 9 | m = int(input()) 10 | 11 | for i in range(m): 12 | print(result[input()]) 13 | -------------------------------------------------------------------------------- /dicts/courses_info.py: -------------------------------------------------------------------------------- 1 | d = { 2 | "CS101": "CS101: 3004, Хайнс, 8:00", 3 | "CS102": "CS102: 4501, Альварадо, 9:00", 4 | "CS103": "CS103: 6755, Рич, 10:00", 5 | "NT110": "NT110: 1244, Берк, 11:00", 6 | "CM241": "CM241: 1411, Ли, 13:00" 7 | } 8 | 9 | print(d[input()]) 10 | -------------------------------------------------------------------------------- /dicts/dicts_1.py: -------------------------------------------------------------------------------- 1 | my_dict = {1.12: 'aa', 67.9: 45, 3.11: 'ccc', 7.9: 'dd', 9.2: 'ee', 7.1: 'ff', 0.12: 'qq', 1.91: 'aa', 10.12: [1, 2, 3], 99.0: {9, 0, 1}} 2 | 3 | print(min(my_dict) + max(my_dict)) 4 | -------------------------------------------------------------------------------- /dicts/dicts_10.py: -------------------------------------------------------------------------------- 1 | colors = {'c1': 'Red', 'c2': 'Grey', 'c3': None, 'c4': 'Green', 'c5': 'Yellow', 'c6': 'Pink', 'c7': 'Orange', 'c8': None, 'c9': 'White', 'c10': 'Black', 'c11': 'Violet', 'c12': 'Gold', 'c13': None, 'c14': 'Amber', 'c15': 'Azure', 'c16': 'Beige', 'c17': 'Bronze', 'c18': None, 'c19': 'Lilac', 'c20': 'Pearl', 'c21': None, 'c22': 'Sand', 'c23': None} 2 | 3 | result = {} 4 | 5 | for elem in colors: 6 | if colors[elem] != None: 7 | result[elem] = colors[elem] 8 | -------------------------------------------------------------------------------- /dicts/dicts_11.py: -------------------------------------------------------------------------------- 1 | favorite_numbers = {'timur': 17, 'ruslan': 7, 'larisa': 19, 'roman': 123, 'rebecca': 293, 'ronald': 76, 'dorothy': 62, 'harold': 36, 'matt': 314, 'kim': 451, 'rosaly': 18, 'rustam': 89, 'soltan': 111, 'amir': 654, 'dima': 390, 'amiran': 777, 'geor': 999, 'sveta': 75, 'rita': 909, 'kirill': 404, 'olga': 271, 'anna': 55, 'madlen': 876} 2 | 3 | result = {} 4 | 5 | for elem in favorite_numbers: 6 | if favorite_numbers[elem] // 100 == 0 and favorite_numbers[elem] // 10 != 0: 7 | result[elem] = favorite_numbers[elem] 8 | -------------------------------------------------------------------------------- /dicts/dicts_12.py: -------------------------------------------------------------------------------- 1 | months = {1: 'January', 2: 'February', 3: 'March', 4: 'April', 5: 'May', 6: 'June', 7: 'July', 8: 'August', 9: 'September', 10: 'October', 11: 'November', 12: 'December'} 2 | 3 | result = {} 4 | 5 | for elem in months: 6 | result[months[elem]] = elem 7 | -------------------------------------------------------------------------------- /dicts/dicts_13.py: -------------------------------------------------------------------------------- 1 | s = '1:men 2:kind 90:number 0:sun 34:book 56:mountain 87:wood 54:car 3:island 88:power 7:box 17:star 101:ice' 2 | 3 | result = {} 4 | 5 | for elem in s.split(): 6 | arr = elem.split(':') 7 | result[int(arr[0])] = arr[1] 8 | -------------------------------------------------------------------------------- /dicts/dicts_14.py: -------------------------------------------------------------------------------- 1 | numbers = [34, 10, 4, 6, 10, 23, 90, 100, 21, 35, 95, 1, 36, 38, 19, 1, 6, 87, 1000, 13456, 360] 2 | 3 | result = {} 4 | 5 | for elem in numbers: 6 | result[elem] = [] 7 | for i in range(1, elem + 1): 8 | if elem%i == 0: 9 | result[elem].append(i) 10 | -------------------------------------------------------------------------------- /dicts/dicts_15.py: -------------------------------------------------------------------------------- 1 | words = ['hello', 'bye', 'yes', 'no', 'python', 'apple', 'maybe', 'stepik', 'beegeek'] 2 | 3 | result = {} 4 | 5 | for elem in words: 6 | result[elem] = [] 7 | for i in elem: 8 | result[elem].append(ord(i)) 9 | -------------------------------------------------------------------------------- /dicts/dicts_16.py: -------------------------------------------------------------------------------- 1 | letters = {0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E', 5: 'F', 6: 'G', 7: 'H', 8: 'I', 9: 'J', 10: 'K', 11: 'L', 12: 'M', 13: 'N', 14: 'O', 15: 'P', 16: 'Q', 17: 'R', 18: 'S', 19: 'T', 20: 'U', 21: 'V', 22: 'W', 23: 'X', 24: 'Y', 26: 'Z'} 2 | 3 | remove_keys = [1, 5, 7, 12, 17, 19, 21, 24] 4 | 5 | result = {} 6 | 7 | for elem in letters: 8 | if elem not in remove_keys: 9 | result[elem] = letters[elem] 10 | -------------------------------------------------------------------------------- /dicts/dicts_17.py: -------------------------------------------------------------------------------- 1 | students = {'Timur': (170, 75), 'Ruslan': (180, 105), 'Soltan': (192, 68), 'Roman': (175, 70), 'Madlen': (160, 50), 'Stefani': (165, 70), 'Tom': (190, 90), 'Jerry': (180, 87), 'Anna': (172, 67), 'Scott': (168, 78), 'John': (186, 79), 'Alex': (195, 120), 'Max': (200, 110), 'Barak': (180, 89), 'Donald': (170, 80), 'Rustam': (186, 100), 'Alice': (159, 59), 'Rita': (170, 80), 'Mary': (175, 69), 'Jane': (190, 80)} 2 | 3 | result = {} 4 | 5 | for elem in students: 6 | if students[elem][0] > 167 and students[elem][1] < 75: 7 | result[elem] = students[elem] 8 | -------------------------------------------------------------------------------- /dicts/dicts_18.py: -------------------------------------------------------------------------------- 1 | tuples = [(1, 2, 3), (4, 5, 6), (7, 8, 9), (10, 11, 12), (13, 14, 15), (16, 17, 18), (19, 20, 21), (22, 23, 24), (25, 26, 27), (28, 29, 30), (31, 32, 33), (34, 35, 36)] 2 | 3 | result = {elem[0] : elem[1:] for elem in tuples} 4 | -------------------------------------------------------------------------------- /dicts/dicts_19.py: -------------------------------------------------------------------------------- 1 | student_ids = ['S001', 'S002', 'S003', 'S004', 'S005', 'S006', 'S007', 'S008', 'S009', 'S010', 'S011', 'S012', 'S013'] 2 | student_names = ['Camila Rodriguez', 'Juan Cruz', 'Dan Richards', 'Sam Boyle', 'Batista Cesare', 'Francesco Totti', 'Khalid Hussain', 'Ethan Hawke', 'David Bowman', 'James Milner', 'Michael Owen', 'Gary Oldman', 'Tom Hardy'] 3 | student_grades = [86, 98, 89, 92, 45, 67, 89, 90, 100, 98, 10, 96, 93] 4 | 5 | result = [] 6 | 7 | for i in range(len(student_ids)): 8 | result.append({}) 9 | arr = {} 10 | arr[student_names[i]] = student_grades[i] 11 | result[i][student_ids[i]] = arr 12 | -------------------------------------------------------------------------------- /dicts/dicts_2.py: -------------------------------------------------------------------------------- 1 | users = [{'name': 'Todd', 'phone': '551-1414', 'email': 'todd@gmail.com'}, 2 | {'name': 'Helga', 'phone': '555-1618', 'email': 'helga@mail.net'}, 3 | {'name': 'Olivia', 'phone': '449-3141', 'email': ''}, 4 | {'name': 'LJ', 'phone': '555-2718', 'email': 'lj@gmail.net'}, 5 | {'name': 'Ruslan', 'phone': '422-145-9098', 'email': 'rus-lan.cha@yandex.ru'}, 6 | {'name': 'John', 'phone': '233-421-32', 'email': ''}, 7 | {'name': 'Lara', 'phone': '+7998-676-2532', 'email': 'g.lara89@gmail.com'}, 8 | {'name': 'Alina', 'phone': '+7948-799-2434', 'email': 'ali.ch.b@gmail.com'}, 9 | {'name': 'Robert', 'phone': '420-2011', 'email': ''}, 10 | {'name': 'Riyad', 'phone': '128-8890-128', 'email': 'r.mahrez@mail.net'}, 11 | {'name': 'Khabib', 'phone': '+7995-600-9080', 'email': 'kh.nurmag@gmail.com'}, 12 | {'name': 'Olga', 'phone': '6449-314-1213', 'email': ''}, 13 | {'name': 'Roman', 'phone': '+7459-145-8059', 'email': 'roma988@mail.ru'}, 14 | {'name': 'Maria', 'phone': '12-129-3148', 'email': 'm.sharapova@gmail.com'}, 15 | {'name': 'Fedor', 'phone': '+7445-341-0545', 'email': ''}, 16 | {'name': 'Tim', 'phone': '242-449-3141', 'email': 'timm.ggg@yandex.ru'}] 17 | 18 | arr = sorted([elem['name'] for elem in users if elem['phone'].endswith('8')]) 19 | 20 | print(*arr) 21 | -------------------------------------------------------------------------------- /dicts/dicts_3.py: -------------------------------------------------------------------------------- 1 | users = [{'name': 'Todd', 'phone': '551-1414', 'email': 'todd@gmail.com'}, 2 | {'name': 'Helga', 'phone': '555-1618'}, 3 | {'name': 'Olivia', 'phone': '449-3141', 'email': ''}, 4 | {'name': 'LJ', 'phone': '555-2718', 'email': 'lj@gmail.net'}, 5 | {'name': 'Ruslan', 'phone': '422-145-9098', 'email': 'rus-lan.cha@yandex.ru'}, 6 | {'name': 'John', 'phone': '233-421-32', 'email': ''}, 7 | {'name': 'Lara', 'phone': '+7998-676-2532', 'email': 'g.lara89@gmail.com'}, 8 | {'name': 'Alina', 'phone': '+7948-799-2434'}, 9 | {'name': 'Robert', 'phone': '420-2011', 'email': ''}, 10 | {'name': 'Riyad', 'phone': '128-8890-128', 'email': 'r.mahrez@mail.net'}, 11 | {'name': 'Khabib', 'phone': '+7995-600-9080', 'email': 'kh.nurmag@gmail.com'}, 12 | {'name': 'Olga', 'phone': '6449-314-1213', 'email': ''}, 13 | {'name': 'Roman', 'phone': '+7459-145-8059'}, 14 | {'name': 'Maria', 'phone': '12-129-3148', 'email': 'm.sharapova@gmail.com'}, 15 | {'name': 'Fedor', 'phone': '+7445-341-0545', 'email': ''}, 16 | {'name': 'Tim', 'phone': '242-449-3141', 'email': 'timm.ggg@yandex.ru'}] 17 | 18 | arr = sorted([elem['name'] for elem in users if elem.get('email') == '' or elem.get('email') == None]) 19 | 20 | print(*arr) 21 | -------------------------------------------------------------------------------- /dicts/dicts_4.py: -------------------------------------------------------------------------------- 1 | result = {elem : pow(elem, 2) for elem in range(1, 16)} 2 | -------------------------------------------------------------------------------- /dicts/dicts_5.py: -------------------------------------------------------------------------------- 1 | dict1 = {'a': 100, 'z': 333, 'b': 200, 'c': 300, 'd': 45, 'e': 98, 't': 76, 'q': 34, 'f': 90, 'm': 230} 2 | dict2 = {'a': 300, 'b': 200, 'd': 400, 't': 777, 'c': 12, 'p': 123, 'w': 111, 'z': 666} 3 | 4 | result = dict1 5 | for item in dict2: 6 | if item in dict1: 7 | result[item] += dict2[item] 8 | else: 9 | result[item] = dict2[item] 10 | -------------------------------------------------------------------------------- /dicts/dicts_6.py: -------------------------------------------------------------------------------- 1 | text = 'footballcyberpunkextraterritorialityconversationalistblockophthalmoscopicinterdependencemamauserfff' 2 | 3 | result = {} 4 | for elem in text: 5 | if elem in result: 6 | result[elem] += 1 7 | else: 8 | result[elem] = 1 9 | -------------------------------------------------------------------------------- /dicts/dicts_7.py: -------------------------------------------------------------------------------- 1 | s = 'orange strawberry barley gooseberry apple apricot barley currant orange melon pomegranate banana banana orange barley apricot plum grapefruit banana quince strawberry barley grapefruit banana grapes melon strawberry apricot currant currant gooseberry raspberry apricot currant orange lime quince grapefruit barley banana melon pomegranate barley banana orange barley apricot plum banana quince lime grapefruit strawberry gooseberry apple barley apricot currant orange melon pomegranate banana banana orange apricot barley plum banana grapefruit banana quince currant orange melon pomegranate barley plum banana quince barley lime grapefruit pomegranate barley' 2 | 3 | result = {} 4 | 5 | for elem in s.split(): 6 | if elem in result: 7 | result[elem] += 1 8 | else: 9 | result[elem] = 1 10 | 11 | n = list(result.values()).count(max(result.values())) 12 | 13 | 14 | result = dict(sorted(result.items())) 15 | 16 | print(list(result.keys())[list(result.values()).index(max(result.values()))]) 17 | -------------------------------------------------------------------------------- /dicts/dicts_8.py: -------------------------------------------------------------------------------- 1 | pets = [('Hatiko', 'Parker', 'Wilson', 50), 2 | ('Rusty', 'Josh', 'King', 25), 3 | ('Fido', 'John', 'Smith', 28), 4 | ('Butch', 'Jake', 'Smirnoff', 18), 5 | ('Odi', 'Emma', 'Wright', 18), 6 | ('Balto', 'Josh', 'King', 25), 7 | ('Barry', 'Josh', 'King', 25), 8 | ('Snape', 'Hannah', 'Taylor', 40), 9 | ('Horry', 'Martha', 'Robinson', 73), 10 | ('Giro', 'Alex', 'Martinez', 65), 11 | ('Zooma', 'Simon', 'Nevel', 32), 12 | ('Lassie', 'Josh', 'King', 25), 13 | ('Chase', 'Martha', 'Robinson', 73), 14 | ('Ace', 'Martha', 'Williams', 38), 15 | ('Rocky', 'Simon', 'Nevel', 32)] 16 | 17 | result = {} 18 | for elem in pets: 19 | if elem[1:] in result: 20 | result[elem[1:]].append(elem[0]) 21 | else: 22 | result[elem[1:]] = [elem[0]] 23 | -------------------------------------------------------------------------------- /dicts/dicts_9.py: -------------------------------------------------------------------------------- 1 | numbers = [34, 10, -4, 6, 10, 23, -90, 100, 21, -35, -95, 1, 36, -38, -19, 1, 6, 87] 2 | 3 | result = {} 4 | 5 | for i in range(len(numbers)): 6 | result[i] = pow(numbers[i], 2) 7 | -------------------------------------------------------------------------------- /dicts/duplicates_fix.py: -------------------------------------------------------------------------------- 1 | arr = input().split() 2 | result = {} 3 | resultArr = [] 4 | 5 | for elem in arr: 6 | if elem in result: 7 | resultArr.append(elem + '_' + str(result[elem])) 8 | result[elem] += 1 9 | else: 10 | resultArr.append(elem) 11 | result[elem] = 1 12 | 13 | print(*resultArr) 14 | -------------------------------------------------------------------------------- /dicts/messages_set.py: -------------------------------------------------------------------------------- 1 | d = { 2 | "1": ".,?!:", 3 | "2": "ABC", 4 | "3": "DEF", 5 | "4": "GHI", 6 | "5": "JKL", 7 | "6": "MNO", 8 | "7": "PQRS", 9 | "8": "TUV", 10 | "9": "WXYZ", 11 | "0": " " 12 | } 13 | 14 | s = input().upper() 15 | result = '' 16 | 17 | for sym in s: 18 | for elem in d.items(): 19 | if sym in elem[1]: 20 | result += elem[0] * (elem[1].find(sym) + 1) 21 | print(result) 22 | -------------------------------------------------------------------------------- /dicts/morse_code.py: -------------------------------------------------------------------------------- 1 | letters = [c for c in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'] 2 | morse = ['.-', '-...', '-.-.', '-..', '.', '..-.', '--.', '....', '..', '.---', '-.-', '.-..', '--', '-.', '---', '.--.', '--.-', '.-.', '...', '-', '..-', '...-', '.--', '-..-', '-.--', '--..', '-----', '.----', '..---', '...--', '....-', '.....', '-....', '--...', '---..', '----.'] 3 | 4 | arr = [morse[letters.index(elem)] for elem in input().upper() if elem in letters] 5 | 6 | print(*arr) 7 | -------------------------------------------------------------------------------- /dicts/phonebook.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | result = {} 3 | 4 | for i in range(n): 5 | arr = input().split() 6 | for j in range(0, len(arr) - 1): 7 | if arr[-1].lower() in result: 8 | result[arr[-1].lower()].append(arr[j]) 9 | else: 10 | result[arr[-1].lower()] = [arr[j]] 11 | 12 | m = int(input()) 13 | 14 | for i in range(m): 15 | request = input().lower() 16 | if request in result: 17 | print(*result[request]) 18 | else: 19 | print('абонент не найден') 20 | -------------------------------------------------------------------------------- /dicts/programming_dict.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | result = {} 3 | 4 | for i in range(n): 5 | arr = [elem.strip() for elem in input().split(':')] 6 | result[arr[0].lower()] = arr[1] 7 | 8 | m = int(input()) 9 | 10 | for i in range(m): 11 | s = input().lower() 12 | print(result[s] if s in result else 'Не найдено') 13 | -------------------------------------------------------------------------------- /dicts/rarest_word.py: -------------------------------------------------------------------------------- 1 | arr = [elem.strip('.,!?:;-').lower() for elem in input().split()] 2 | result = {} 3 | 4 | for elem in arr: 5 | if elem in result: 6 | result[elem] += 1 7 | else: 8 | result[elem] = 1 9 | 10 | result = dict(sorted(result.items())) 11 | 12 | print(list(result.keys())[list(result.values()).index(min(result.values()))]) 13 | -------------------------------------------------------------------------------- /dicts/secret_word.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | n = int(input()) 3 | result_1 = {} 4 | result_2 = {} 5 | 6 | for i in range(n): 7 | arr = [elem.strip() for elem in input().split(':')] 8 | result_2[int(arr[1])] = arr[0] 9 | 10 | for elem in s: 11 | if elem in result_1: 12 | result_1[elem] += 1 13 | else: 14 | result_1[elem] = 1 15 | 16 | result = '' 17 | 18 | for elem in s: 19 | result += result_2[result_1[elem]] 20 | 21 | print(result) 22 | -------------------------------------------------------------------------------- /dicts/string_representation.py: -------------------------------------------------------------------------------- 1 | d = { 2 | 0: "zero", 3 | 1: "one", 4 | 2: "two", 5 | 3: "three", 6 | 4: "four", 7 | 5: "five", 8 | 6: "six", 9 | 7: "seven", 10 | 8: "eight", 11 | 9: "nine" 12 | } 13 | 14 | arr = [d[int(elem)] for elem in input()] 15 | print(*arr) 16 | -------------------------------------------------------------------------------- /dicts/synonyms_dict.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | result = {} 3 | 4 | for i in range(n): 5 | arr = input().split() 6 | result[arr[0]] = arr[1] 7 | result[arr[1]] = arr[0] 8 | 9 | print(result[input()]) 10 | -------------------------------------------------------------------------------- /dicts_exam/dangerous_virus.py: -------------------------------------------------------------------------------- 1 | s = {'write': 'W', 'read': 'R','execute': 'X'} 2 | d = {} 3 | 4 | n = int(input()) 5 | 6 | for i in range(n): 7 | arr = input().split() 8 | d[arr[0]] = arr[1:] 9 | 10 | m = int(input()) 11 | 12 | for i in range(m): 13 | arr = input().split() 14 | if s[arr[0]] in d[arr[1]]: 15 | print('OK') 16 | else: 17 | print('Access denied') 18 | -------------------------------------------------------------------------------- /dicts_exam/dicts_exam_1.py: -------------------------------------------------------------------------------- 1 | my_dict = {'C1': [10, 20, 30, 7, 6, 23, 90], 'C2': [20, 30, 40, 1, 2, 3, 90, 12], 'C3': [12, 34, 20, 21], 'C4': [22, 54, 209, 21, 7], 'C5': [2, 4, 29, 21, 19], 'C6': [4, 6, 7, 10, 55], 'C7': [4, 8, 12, 23, 42], 'C8': [3, 14, 15, 26, 48], 'C9': [2, 7, 18, 28, 18, 28]} 2 | 3 | for elem in my_dict: 4 | my_dict[elem] = [i for i in my_dict[elem] if i <= 20] 5 | -------------------------------------------------------------------------------- /dicts_exam/dicts_exam_2.py: -------------------------------------------------------------------------------- 1 | emails = {'nosu.edu': ['timyr', 'joseph', 'svetlana.gaeva', 'larisa.mamuk'], 2 | 'gmail.com': ['ruslan.chaika', 'rustam.mini', 'stepik-best'], 3 | 'msu.edu': ['apple.fruit', 'beegeek', 'beegeek.school'], 4 | 'yandex.ru': ['surface', 'google'], 5 | 'hse.edu': ['tomas-henders', 'cream.soda', 'zivert'], 6 | 'mail.ru': ['angel.down', 'joanne', 'the.fame.moster']} 7 | 8 | arr = [] 9 | 10 | for elem in emails: 11 | for address in emails[elem]: 12 | arr.append(address + '@' + elem) 13 | 14 | for elem in sorted(arr): 15 | print(elem) 16 | -------------------------------------------------------------------------------- /dicts_exam/dicts_merge.py: -------------------------------------------------------------------------------- 1 | def merge(values): 2 | result = {} 3 | for elem in values: 4 | for i in elem: 5 | if i in result: 6 | result[i].add(elem[i]) 7 | else: 8 | result[i] = set() 9 | result[i].add(elem[i]) 10 | 11 | return result 12 | -------------------------------------------------------------------------------- /dicts_exam/genetics_minute.py: -------------------------------------------------------------------------------- 1 | d = {'A': 'U', 'C': 'G', 'G': 'C', 'T': 'A'} 2 | arr = [d[elem] for elem in input()] 3 | 4 | print(''.join(arr)) 5 | -------------------------------------------------------------------------------- /dicts_exam/internet_shopping.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | d = {} 3 | 4 | for i in range(n): 5 | arr = input().split() 6 | if arr[0] in d: 7 | if arr[1] in d[arr[0]]: 8 | d[arr[0]][arr[1]] += int(arr[2]) 9 | else: 10 | d[arr[0]][arr[1]] = int(arr[2]) 11 | else: 12 | d[arr[0]] = {arr[1] : int(arr[2])} 13 | 14 | for name in sorted(d): 15 | print(name + ':') 16 | for product in sorted(d[name]): 17 | print(product + ' ' + str(d[name][product])) 18 | -------------------------------------------------------------------------------- /dicts_exam/request_string.py: -------------------------------------------------------------------------------- 1 | def build_query_string(params): 2 | result = '' 3 | for elem in sorted(params): 4 | result += elem 5 | result += '=' 6 | result += str(params[elem]) 7 | result += '&' 8 | return result[:-1] 9 | -------------------------------------------------------------------------------- /dicts_exam/scrabble_game.py: -------------------------------------------------------------------------------- 1 | d = {'A': 1, 'E': 1, 'I': 1, 'L': 1, 'N': 1, 'O': 1, 'R': 1, 'S': 1, 'T': 1, 'U': 1, 'D': 2, 'G': 2, 'B': 3, 'C': 3, 'M': 3, 'P': 3, 'F': 4, 'H': 4, 'V': 4, 'W': 4, 'Y': 4, 'K': 5, 'J': 8, 'X': 8, 'Q': 10, 'Z': 10} 2 | 3 | summa = 0 4 | 5 | for elem in input(): 6 | summa += d[elem] 7 | 8 | print(summa) 9 | -------------------------------------------------------------------------------- /dicts_exam/serial_number.py: -------------------------------------------------------------------------------- 1 | s = input().split() 2 | arr = [] 3 | d = {} 4 | 5 | for elem in s: 6 | if elem in d: 7 | d[elem] += 1 8 | arr.append(d[elem]) 9 | else: 10 | d[elem] = 1 11 | arr.append(d[elem]) 12 | 13 | print(*arr) 14 | -------------------------------------------------------------------------------- /functions/anonymos_funcs_1.py: -------------------------------------------------------------------------------- 1 | from functools import reduce 2 | 3 | floats = [4.35, 6.09, 3.25, 9.77, 2.16, 8.88, 4.59, 34.23, 12.12, 4.67, 2.45, 9.32] 4 | words = ['racecar', 'akinremi', 'deed', 'temidayo', 'omoseun', 'civic', 'TATTARRATTAT', 'malayalam', 'nun'] 5 | numbers = [4, 6, 9, 23, 5] 6 | 7 | # Исправьте этот код 8 | map_result = list(map(lambda num: round(pow(num, 2), 1), floats)) 9 | filter_result = list(filter(lambda name: name == name[::-1] and len(name) > 4, words)) 10 | reduce_result = reduce(lambda num1, num2: num1 * num2, numbers, 1) 11 | 12 | print(map_result) 13 | print(filter_result) 14 | print(reduce_result) 15 | -------------------------------------------------------------------------------- /functions/anonymos_funcs_10.py: -------------------------------------------------------------------------------- 1 | data = ['год', 'человек', 'время', 'дело', 'жизнь', 'день', 'рука', 'раз', 'работа', 'слово', 'место', 'лицо', 'друг', 'глаз', 'вопрос', 'дом', 'сторона', 'страна', 'мир', 'случай', 'голова', 'ребенок', 'сила', 'конец', 'вид', 'система', 'часть', 'город', 'отношение', 'женщина', 'деньги'] 2 | 3 | func = lambda x: (len(x), x) 4 | 5 | data.sort(key=func) 6 | 7 | print(*data) 8 | -------------------------------------------------------------------------------- /functions/anonymos_funcs_11.py: -------------------------------------------------------------------------------- 1 | mixed_list = ['tuesday', 'abroad', 'abuse', 'beside', 'monday', 'abate', 'accessory', 'absorb', 1384878, 'sunday', 'about', 454805, 'saturday', 'abort', 2121919, 2552839, 977970, 1772933, 1564063, 'abduct', 901271, 2680434, 'bicycle', 'accelerate', 1109147, 942908, 'berry', 433507, 'bias', 'bestow', 1875665, 'besides', 'bewilder', 1586517, 375290, 1503450, 2713047, 'abnormal', 2286106, 242192, 701049, 2866491, 'benevolent', 'bigot', 'abuse', 'abrupt', 343772, 'able', 2135748, 690280, 686008, 'beyond', 2415643, 'aboard', 'bet', 859105, 'accident', 2223166, 894187, 146564, 1251748, 2851543, 1619426, 2263113, 1618068, 'berth', 'abolish', 'beware', 2618492, 1555062, 'access', 'absent', 'abundant', 2950603, 'betray', 'beverage', 'abide', 'abandon', 2284251, 'wednesday', 2709698, 'thursday', 810387, 'friday', 2576799, 2213552, 1599022, 'accept', 'abuse', 'abound', 1352953, 'bid', 1805326, 1499197, 2241159, 605320, 2347441] 2 | 3 | func = lambda x: True if type(x)==int else False 4 | 5 | print(max(list(filter(func, mixed_list)))) 6 | -------------------------------------------------------------------------------- /functions/anonymos_funcs_12.py: -------------------------------------------------------------------------------- 1 | mixed_list = ['beside', 48, 'accelerate', 28, 'beware', 'absorb', 'besides', 'berry', 15, 65, 'abate', 'thursday', 76, 70, 94, 35, 36, 'berth', 41, 'abnormal', 'bicycle', 'bid', 'sunday', 'saturday', 87, 'bigot', 41, 'abort', 13, 60, 'friday', 26, 13, 'accident', 'access', 40, 26, 20, 75, 13, 40, 67, 12, 'abuse', 78, 10, 80, 'accessory', 20, 'bewilder', 'benevolent', 'bet', 64, 38, 65, 51, 95, 'abduct', 37, 98, 99, 14, 'abandon', 'accept', 46, 'abide', 'beyond', 19, 'about', 76, 26, 'abound', 12, 95, 'wednesday', 'abundant', 'abrupt', 'aboard', 50, 89, 'tuesday', 66, 'bestow', 'absent', 76, 46, 'betray', 47, 'able', 11] 2 | 3 | print(*sorted(list(filter(lambda x: True if type(x)==int else False, mixed_list))) + sorted(list(filter(lambda x: True if type(x)==str else False, mixed_list)))) 4 | -------------------------------------------------------------------------------- /functions/anonymos_funcs_2.py: -------------------------------------------------------------------------------- 1 | from functools import reduce 2 | 3 | data = [['Tokyo', 35676000, 'primary'], 4 | ['New York', 19354922, 'nan'], 5 | ['Mexico City', 19028000, 'primary'], 6 | ['Mumbai', 18978000, 'admin'], 7 | ['Sao Paulo', 18845000, 'admin'], 8 | ['Delhi', 15926000, 'admin'], 9 | ['Shanghai', 14987000, 'admin'], 10 | ['Kolkata', 14787000, 'admin'], 11 | ['Los Angeles', 12815475, 'nan'], 12 | ['Dhaka', 12797394, 'primary'], 13 | ['Buenos Aires', 12795000, 'primary'], 14 | ['Karachi', 12130000, 'admin'], 15 | ['Cairo', 11893000, 'primary'], 16 | ['Rio de Janeiro', 11748000, 'admin'], 17 | ['Osaka', 11294000, 'admin'], 18 | ['Beijing', 11106000, 'primary'], 19 | ['Manila', 11100000, 'primary'], 20 | ['Moscow', 10452000, 'primary'], 21 | ['Istanbul', 10061000, 'admin'], 22 | ['Paris', 9904000, 'primary']] 23 | 24 | result = list(filter(lambda data: data[2] == 'primary' and data[1] > 10000000, data)) 25 | 26 | print("Cities: " + ', '.join(sorted([elem[0] for elem in result]))) 27 | -------------------------------------------------------------------------------- /functions/anonymos_funcs_3.py: -------------------------------------------------------------------------------- 1 | func = lambda x: True if x%19 == 0 or x%13 == 0 else False 2 | -------------------------------------------------------------------------------- /functions/anonymos_funcs_4.py: -------------------------------------------------------------------------------- 1 | func = lambda x: True if x.lower().startswith('a') and x.lower().endswith('a') else False 2 | -------------------------------------------------------------------------------- /functions/anonymos_funcs_5.py: -------------------------------------------------------------------------------- 1 | import string 2 | 3 | is_non_negative_num = lambda x: False if any([letter in x for letter in string.ascii_lowercase]) or '-' in x or x.count('.') > 1 else True 4 | -------------------------------------------------------------------------------- /functions/anonymos_funcs_6.py: -------------------------------------------------------------------------------- 1 | def is_digit(string): 2 | if string.isdigit(): 3 | return True 4 | else: 5 | try: 6 | float(string) 7 | return True 8 | except ValueError: 9 | return False 10 | 11 | is_num = lambda x: True if is_digit(x) else False 12 | -------------------------------------------------------------------------------- /functions/anonymos_funcs_7.py: -------------------------------------------------------------------------------- 1 | words = ['beverage', 'monday', 'abroad', 'bias', 'abuse', 'abolish', 'abuse', 'abuse', 'bid', 'wednesday', 'able', 'betray', 'accident', 'abduct', 'bigot', 'bet', 'abandon', 'besides', 'access', 'friday', 'bestow', 'abound', 'absent', 'beware', 'abundant', 'abnormal', 'aboard', 'about', 'accelerate', 'abort', 'thursday', 'tuesday', 'sunday', 'berth', 'beyond', 'benevolent', 'abate', 'abide', 'bicycle', 'beside', 'accept', 'berry', 'bewilder', 'abrupt', 'saturday', 'accessory', 'absorb'] 2 | 3 | func = lambda x: True if len(x) == 6 else False 4 | 5 | print(*sorted(list(filter(func, words)))) 6 | -------------------------------------------------------------------------------- /functions/anonymos_funcs_8.py: -------------------------------------------------------------------------------- 1 | numbers = [46, 61, 34, 17, 56, 26, 93, 1, 3, 82, 71, 37, 80, 27, 77, 94, 34, 100, 36, 81, 33, 81, 66, 83, 41, 80, 80, 93, 40, 34, 32, 16, 5, 16, 40, 93, 36, 65, 8, 19, 8, 75, 66, 21, 72, 32, 41, 59, 35, 64, 49, 78, 83, 27, 57, 53, 43, 35, 48, 17, 19, 40, 90, 57, 77, 56, 80, 95, 90, 27, 26, 6, 4, 23, 52, 39, 63, 74, 15, 66, 29, 88, 94, 37, 44, 2, 38, 36, 32, 49, 5, 33, 60, 94, 89, 8, 36, 94, 46, 33] 2 | 3 | func = lambda x: False if x%2 == 1 and x > 47 else True 4 | func2 = lambda x: x//2 if x%2 == 0 else x 5 | 6 | print(*map(func2,filter(func, numbers))) 7 | -------------------------------------------------------------------------------- /functions/anonymos_funcs_9.py: -------------------------------------------------------------------------------- 1 | data = [(19542209, 'New York'), (4887871, 'Alabama'), (1420491, 'Hawaii'), (626299, 'Vermont'), (1805832, 'West Virginia'), (39865590, 'California'), (11799448, 'Ohio'), (10711908, 'Georgia'), (10077331, 'Michigan'), (10439388, 'Virginia'), (7705281, 'Washington'), (7151502, 'Arizona'), (7029917, 'Massachusetts'), (6910840, 'Tennessee')] 2 | 3 | func = lambda x: x[1][-1] 4 | 5 | data.sort(key=func, reverse=True) 6 | 7 | for elem in data: 8 | print(elem[1] + ':', elem[0]) 9 | -------------------------------------------------------------------------------- /functions/built_in_funcs_1.py: -------------------------------------------------------------------------------- 1 | def ignore_command(command): 2 | ignore = ['alias', 'configuration', 'ip', 'sql', 'select', 'update', 'exec', 'del', 'truncate'] 3 | return any([elem in command for elem in ignore]) 4 | -------------------------------------------------------------------------------- /functions/built_in_funcs_2.py: -------------------------------------------------------------------------------- 1 | countries = ['Russia', 'USA', 'UK', 'Germany', 'France', 'India'] 2 | capitals = ['Moscow', 'New York', 'London', 'Berlin', 'Paris', 'Delhi'] 3 | population = [145_934_462, 331_002_651, 80_345_321, 67_886_011, 65_273_511, 1_380_004_385] 4 | 5 | for elem in list(zip(countries, capitals, population)): 6 | print(elem[1] + ' is the capital of ' + elem[0] + ', population equal ' + str(elem[2]) + ' people.') 7 | -------------------------------------------------------------------------------- /functions/correct_ip.py: -------------------------------------------------------------------------------- 1 | def isIPAddressCorrect(address): 2 | for elem in address.split('.'): 3 | if not elem.isdigit(): 4 | return False 5 | if int(elem) < 0 or int(elem) > 255: 6 | return False 7 | return True 8 | 9 | print(isIPAddressCorrect(input())) 10 | -------------------------------------------------------------------------------- /functions/excellent_students.py: -------------------------------------------------------------------------------- 1 | classNum = int(input()) 2 | 3 | def isTopScorerInClass(classNum): 4 | for i in range(classNum): 5 | pupilsInClass = int(input()) 6 | result = False 7 | for j in range(pupilsInClass): 8 | if int(input().split()[1]) == 5: 9 | result = True 10 | if not result: 11 | return False 12 | result = False 13 | return True 14 | 15 | result = 'YES' if isTopScorerInClass(classNum) else 'NO' 16 | print(result) 17 | -------------------------------------------------------------------------------- /functions/functions_as_objects_1.py: -------------------------------------------------------------------------------- 1 | numbers = [(10, 10, 10), (30, 45, 56), (81, 39), (1, 2, 3), (12,), (-2, -4, 100), (1, 2, 99), (89, 9, 34), (10, 20, 30, -2), (50, 40, 50), (34, 78, 65), (-5, 90, -1, -5), (1, 2, 3, 4, 5, 6), (-9, 8, 4), (90, 1, -45, -21)] 2 | 3 | minimum = 1000 4 | maximum = -1 5 | 6 | minimum_arr = tuple() 7 | maxiumum_arr = tuple() 8 | 9 | for elem in numbers: 10 | if sum(elem) / len(elem) > maximum: 11 | maxiumum_arr = elem 12 | maximum = sum(elem) / len(elem) 13 | if sum(elem) / len(elem) < minimum: 14 | minimum_arr = elem 15 | minimum = sum(elem) / len(elem) 16 | 17 | print(minimum_arr) 18 | print(maxiumum_arr) 19 | -------------------------------------------------------------------------------- /functions/functions_as_objects_2.py: -------------------------------------------------------------------------------- 1 | from math import sqrt 2 | 3 | points = [(-1, 1), (5, 6), (12, 0), (4, 3), (0, 1), (-3, 2), (0, 0), (-1, 3), (2, 0), (3, 0), (-9, 1), (3, 6), (8, 8)] 4 | 5 | func = lambda x: sqrt(pow(x[0], 2) + pow(x[1], 2)) 6 | 7 | print(sorted(points, key=func)) 8 | -------------------------------------------------------------------------------- /functions/functions_as_objects_3.py: -------------------------------------------------------------------------------- 1 | numbers = [(10, 10, 10), (30, 45, 56), (81, 80, 39), (1, 2, 3), (12, 45, 67), (-2, -4, 100), (1, 2, 99), (89, 90, 34), (10, 20, 30), (50, 40, 50), (34, 78, 65), (-5, 90, -1)] 2 | 3 | func = lambda x: min(x) + max(x) 4 | 5 | print(sorted(numbers, key=func)) 6 | -------------------------------------------------------------------------------- /functions/good_password.py: -------------------------------------------------------------------------------- 1 | password = input() 2 | 3 | def predicate(password): 4 | if len(password) < 7: 5 | return False 6 | result = [False, False, False] 7 | for sym in password: 8 | if sym.islower(): 9 | result[0] = True 10 | if sym.isdigit(): 11 | result[1] = True 12 | if sym.isupper(): 13 | result[2] = True 14 | return all(result) 15 | 16 | print('YES' if predicate(password) else 'NO') 17 | -------------------------------------------------------------------------------- /functions/high_order_funcs_1.py: -------------------------------------------------------------------------------- 1 | def map(function, items): 2 | result = [] 3 | for item in items: 4 | result.append(function(item)) 5 | return result 6 | 7 | 8 | numbers = [3.56773, 5.57668, 4.00914, 56.24241, 9.01344, 32.12013, 23.22222, 90.09873, 45.45, 314.1528, 2.71828, 1.41546] 9 | 10 | result = map(lambda x: round(x, 2), numbers) 11 | 12 | for elem in result: 13 | print(elem) 14 | -------------------------------------------------------------------------------- /functions/high_order_funcs_2.py: -------------------------------------------------------------------------------- 1 | def map(function, items): 2 | result = [] 3 | for item in items: 4 | result.append(function(item)) 5 | return result 6 | 7 | 8 | def filter(function, items): 9 | result = [] 10 | for item in items: 11 | if function(item): 12 | result.append(item) 13 | return result 14 | 15 | 16 | numbers = [1014, 1321, 675, 1215, 56, 1386, 1385, 431, 1058, 486, 1434, 696, 1016, 1084, 424, 1189, 475, 95, 1434, 1462, 815, 776, 657, 1225, 912, 537, 1478, 1176, 544, 488, 668, 944, 207, 266, 1309, 1027, 257, 1374, 1289, 1155, 230, 866, 708, 144, 1434, 1163, 345, 394, 560, 338, 232, 182, 1438, 1127, 928, 1309, 98, 530, 1013, 898, 669, 105, 130, 1363, 947, 72, 1278, 166, 904, 349, 831, 1207, 1496, 370, 725, 926, 175, 959, 1282, 336, 1268, 351, 1439, 186, 273, 1008, 231, 138, 142, 433, 456, 1268, 1018, 1274, 387, 120, 340, 963, 832, 1127] 17 | 18 | filtered_result = list(filter(lambda x: x//1000 == 0 and x//100 > 0 and x%5==2, numbers)) 19 | result = map(lambda x: pow(x, 3), filtered_result) 20 | 21 | for elem in result: 22 | print(elem) 23 | -------------------------------------------------------------------------------- /functions/high_order_funcs_3.py: -------------------------------------------------------------------------------- 1 | def reduce(operation, items, initial_value): 2 | acc = initial_value 3 | for item in items: 4 | acc = operation(acc, item) 5 | return acc 6 | 7 | numbers = [97, 42, 9, 32, 3, 45, 31, 77, -1, 11, -2, 75, 5, 51, 34, 28, 46, 1, -8, 84, 16, 51, 90, 56, 65, 90, 23, 35, 11, -10, 70, 90, 90, 12, 96, 58, -8, -4, 91, 76, 94, 60, 72, 43, 4, -6, -5, 51, 58, 60, 30, 38, 67, 62, 36, 72, 34, 82, 62, -1, 60, 82, 87, 81, -7, 57, 26, 36, 17, 43, 80, 40, 75, 94, 91, 64, 38, 72, 29, 84, 38, 35, 7, 54, 31, 95, 78, 27, 82, 1, 64, 94, 31, 29, -8, 98, 24, 61, 7, 73] 8 | 9 | print (sum([pow(elem, 2) for elem in numbers])) 10 | -------------------------------------------------------------------------------- /functions/high_order_funcs_4.py: -------------------------------------------------------------------------------- 1 | def map(function, items): 2 | result = [] 3 | for item in items: 4 | result.append(function(item)) 5 | return result 6 | 7 | 8 | def filter(function, items): 9 | result = [] 10 | for item in items: 11 | if function(item): 12 | result.append(item) 13 | return result 14 | 15 | numbers = [77, 293, 28, 242, 213, 285, 71, 286, 144, 276, 61, 298, 280, 214, 156, 227, 228, 51, -4, 202, 58, 99, 270, 219, 94, 253, 53, 235, 9, 158, 49, 183, 166, 205, 183, 266, 180, 6, 279, 200, 208, 231, 178, 201, 260, -35, 152, 115, 79, 284, 181, 92, 286, 98, 271, 259, 258, 196, -8, 43, 2, 128, 143, 43, 297, 229, 60, 254, -9, 5, 187, 220, -8, 111, 285, 5, 263, 187, 192, -9, 268, -9, 23, 71, 135, 7, -161, 65, 135, 29, 148, 242, 33, 35, 211, 5, 161, 46, 159, 23, 169, 23, 172, 184, -7, 228, 129, 274, 73, 197, 272, 54, 278, 26, 280, 13, 171, 2, 79, -2, 183, 10, 236, 276, 4, 29, -10, 41, 269, 94, 279, 129, 39, 92, -63, 263, 219, 57, 18, 236, 291, 234, 10, 250, 0, 64, 172, 216, 30, 15, 229, 205, 123, -105] 16 | 17 | print (sum([pow(elem, 2) for elem in numbers if abs(elem)//100 == 0 and abs(elem)//10 > 0 and elem%7==0])) 18 | -------------------------------------------------------------------------------- /functions/high_order_funcs_5.py: -------------------------------------------------------------------------------- 1 | def func_apply(func, arr): 2 | return [func(elem) for elem in arr] 3 | -------------------------------------------------------------------------------- /functions/inside_the_ball.py: -------------------------------------------------------------------------------- 1 | def isInSphere(arr1, arr2, arr3): 2 | for i in range(len(arr1)): 3 | if sum([pow(elem, 2) for elem in [arr1[i], arr2[i], arr3[i]]]) > 4: 4 | return False 5 | return True 6 | 7 | abscissas = [float(elem) for elem in input().split()] 8 | ordinates = [float(elem) for elem in input().split()] 9 | applicates = [float(elem) for elem in input().split()] 10 | 11 | print(isInSphere(abscissas, ordinates, applicates)) 12 | -------------------------------------------------------------------------------- /functions/interesting_nums.py: -------------------------------------------------------------------------------- 1 | begin = int(input()) 2 | end = int(input()) 3 | 4 | def predicate(num): 5 | for i in range(len(str(num))): 6 | if int(str(num)[i]) == 0: 7 | return False 8 | if num%int(str(num)[i]) != 0: 9 | return False 10 | return True 11 | 12 | print(*[i for i in range(begin, end+1) if predicate(i)]) 13 | -------------------------------------------------------------------------------- /functions/interesting_sort_1.py: -------------------------------------------------------------------------------- 1 | arr = [elem for elem in input().split()] 2 | 3 | func = lambda x: sum([int(elem) for elem in x]) 4 | 5 | print(*sorted(arr, key=func)) 6 | -------------------------------------------------------------------------------- /functions/interesting_sort_2.py: -------------------------------------------------------------------------------- 1 | arr = [int(elem) for elem in input().split()] 2 | arr = sorted(arr) 3 | func = lambda x: sum([int(elem) for elem in str(x)]) 4 | 5 | print(*sorted(arr, key=func)) 6 | -------------------------------------------------------------------------------- /functions/math_funcs.py: -------------------------------------------------------------------------------- 1 | from math import sin 2 | 3 | d = {'квадрат': lambda x: x ** 2, 4 | 'куб': lambda x: x ** 3, 5 | 'корень': lambda x: x ** 0.5, 6 | 'модуль': lambda x: abs(x), 7 | 'синус': lambda x: sin(x)} 8 | 9 | num = int(input()) 10 | operation = input() 11 | 12 | print(d[operation](num)) 13 | -------------------------------------------------------------------------------- /functions/opposite_color.py: -------------------------------------------------------------------------------- 1 | print(*list(map(lambda x: 255 - x, [int(i) for i in input().split()]))) 2 | -------------------------------------------------------------------------------- /functions/polynominal_value.py: -------------------------------------------------------------------------------- 1 | def evaluate(coefficients, x): 2 | summa = 0 3 | for i in range(len(coefficients)): 4 | summa += coefficients[i] * pow(x, len(coefficients) - i - 1) 5 | 6 | return summa 7 | 8 | coefficients = [int(elem) for elem in input().split()] 9 | x = int(input()) 10 | 11 | print(evaluate(coefficients, x)) 12 | -------------------------------------------------------------------------------- /functions/sort_as_you_want.py: -------------------------------------------------------------------------------- 1 | fieldNum = int(input()) 2 | 3 | athletes = [('Дима', 10, 130, 35), ('Тимур', 11, 135, 39), ('Руслан', 9, 140, 33), ('Рустам', 10, 128, 30), ('Амир', 16, 170, 70), ('Рома', 16, 188, 100), ('Матвей', 17, 168, 68), ('Петя', 15, 190, 90)] 4 | 5 | func = lambda x: x[fieldNum - 1] 6 | 7 | for elem in sorted(athletes, key=func): 8 | print(*elem) 9 | -------------------------------------------------------------------------------- /functions/unnecessary_args.py: -------------------------------------------------------------------------------- 1 | def matrix(n=1, m=-1, value=0): 2 | if m == -1: 3 | m = n 4 | 5 | arr = [] 6 | for i in range(n): 7 | arr.append([]) 8 | for j in range(m): 9 | arr[i].append(value) 10 | return arr 11 | -------------------------------------------------------------------------------- /functions/variable_nums_of_args_funcs_1.py: -------------------------------------------------------------------------------- 1 | def count_args(*args): 2 | return len(args) 3 | -------------------------------------------------------------------------------- /functions/variable_nums_of_args_funcs_2.py: -------------------------------------------------------------------------------- 1 | def sq_sum(*args): 2 | summa = 0 3 | for elem in args: 4 | summa += pow(float(elem), 2) 5 | if (summa%1 == 0): 6 | summa = int(summa) 7 | return summa 8 | -------------------------------------------------------------------------------- /functions/variable_nums_of_args_funcs_3.py: -------------------------------------------------------------------------------- 1 | def mean(*args): 2 | summa = 0.0 3 | counter = 0 4 | for elem in args: 5 | if type(elem) == type(0) or type(elem) == type(0.0): 6 | summa += elem 7 | counter += 1 8 | if counter == 0: 9 | return summa 10 | return summa / counter 11 | -------------------------------------------------------------------------------- /functions/variable_nums_of_args_funcs_4.py: -------------------------------------------------------------------------------- 1 | def greet(name, *args): 2 | result = 'Hello, ' + name + ' and ' 3 | 4 | for elem in args: 5 | result += elem 6 | result += ' and ' 7 | 8 | return result[:-5] + '!' 9 | -------------------------------------------------------------------------------- /functions/variable_nums_of_args_funcs_5.py: -------------------------------------------------------------------------------- 1 | def print_products(*args): 2 | counter = 1 3 | result = str(counter) + ') ' 4 | 5 | for elem in args: 6 | if type(elem) is str and len(elem) != 0: 7 | result += elem 8 | counter += 1 9 | result += '\n' + str(counter) + ') ' 10 | 11 | if counter == 1: 12 | print('Нет продуктов') 13 | return 14 | 15 | print(result[:-4]) 16 | -------------------------------------------------------------------------------- /functions/variable_nums_of_args_funcs_6.py: -------------------------------------------------------------------------------- 1 | def info_kwargs(**kwargs): 2 | for key, value in sorted(kwargs.items()): 3 | print(key + ": " + str(value)) 4 | -------------------------------------------------------------------------------- /functions_exam/exam_letter.py: -------------------------------------------------------------------------------- 1 | def generate_letter(mail, name, date, time, place, teacher = 'Тимур Гуев', number = 17): 2 | return 'To: ' + mail + \ 3 | '\nПриветствую, ' + name + \ 4 | '!\nВам назначен экзамен, который пройдет ' + date + ', в ' + time + \ 5 | '.\nПо адресу: ' + place + \ 6 | '.\nЭкзамен будет проводить ' + teacher + ' в кабинете ' + str(number) + '.\nЖелаем удачи на экзамене!' 7 | -------------------------------------------------------------------------------- /functions_exam/functions_exam_1.py: -------------------------------------------------------------------------------- 1 | def concat(*elems, sep = ' '): 2 | return sep.join([str(elem) for elem in elems]) 3 | -------------------------------------------------------------------------------- /functions_exam/functions_exam_2.py: -------------------------------------------------------------------------------- 1 | from functools import reduce 2 | 3 | def product_of_odds(data): 4 | return reduce(lambda a,b: a*b, [elem for elem in data if elem%2==1], 1) 5 | -------------------------------------------------------------------------------- /functions_exam/functions_exam_3.py: -------------------------------------------------------------------------------- 1 | words = 'the world is mine take a look what you have started'.split() 2 | 3 | print(*map(lambda x: '\"' + x + '\"',words)) 4 | -------------------------------------------------------------------------------- /functions_exam/functions_exam_4.py: -------------------------------------------------------------------------------- 1 | numbers = [18, 191, 9009, 5665, 78, 77, 45, 23, 19991, 908, 8976, 6565, 5665, 10, 1000, 908, 909, 232, 45654, 786] 2 | print(*list(filter(lambda x: str(x) != str(x)[::-1], numbers))) 3 | -------------------------------------------------------------------------------- /functions_exam/functions_exam_5.py: -------------------------------------------------------------------------------- 1 | numbers = [(10, -2, 3, 4), (-13, 56), (1, 9, 2), (-1, -9, -45, 32), (-1, 5, 1), (17, 0, 1), (0, 1), (3,), (39, 12), (11, -23), (10, -100, 21, 32), (3, -8), (1, 1)] 2 | 3 | sorted_numbers = sorted(numbers, key=lambda x: sum(x)/len(x), reverse=True) 4 | 5 | print(sorted_numbers) 6 | -------------------------------------------------------------------------------- /functions_exam/functions_exam_6.py: -------------------------------------------------------------------------------- 1 | def call(func, *args): 2 | return func(*args) 3 | -------------------------------------------------------------------------------- /functions_exam/functions_exam_7.py: -------------------------------------------------------------------------------- 1 | def compose(f, g): 2 | return lambda x: f(g(x)) 3 | -------------------------------------------------------------------------------- /functions_exam/functions_exam_8.py: -------------------------------------------------------------------------------- 1 | def arithmetic_operation(operation): 2 | if operation == '+': 3 | return lambda a,b: a + b 4 | if operation == '-': 5 | return lambda a,b: a - b 6 | if operation == '*': 7 | return lambda a,b: a * b 8 | if operation == '/': 9 | return lambda a,b: a / b 10 | -------------------------------------------------------------------------------- /functions_exam/in_one_string.py: -------------------------------------------------------------------------------- 1 | words = input().split() 2 | 3 | sorted_words = sorted(words, key=lambda a: a.lower()) 4 | 5 | print(*sorted_words) 6 | -------------------------------------------------------------------------------- /functions_exam/ip_sort.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | ip_addresses = [] 3 | 4 | def calculate_digit(ip): 5 | arr = [int(elem) for elem in ip.split('.')] 6 | return arr[0]*pow(256, 3) + arr[1]*pow(256, 2) + arr[2]*pow(256, 1) + arr[3]*pow(256, 0) 7 | 8 | for i in range(n): 9 | ip_addresses.append(input()) 10 | 11 | ip_addresses = sorted(ip_addresses, key = calculate_digit) 12 | for elem in ip_addresses: 13 | print(elem) 14 | -------------------------------------------------------------------------------- /functions_exam/pretty_print.py: -------------------------------------------------------------------------------- 1 | def pretty_print(data, side = '-', delimiter = '|'): 2 | mainStr = delimiter + ' ' + (' ' + delimiter + ' ').join(map(str, data)) + ' ' + delimiter 3 | print(' ' + side * (len(mainStr) - 2)) 4 | print (mainStr) 5 | print(' ' + side * (len(mainStr) - 2)) 6 | -------------------------------------------------------------------------------- /functions_exam/word_gematry.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | words = [] 3 | 4 | def calculate_gematry(word): 5 | summa = 0 6 | word = word.upper() 7 | for elem in word: 8 | summa += ord(elem) - ord('A') 9 | return summa 10 | 11 | for i in range(n): 12 | words.append(input()) 13 | 14 | sorted_words = sorted(words) 15 | sorted_words = sorted(sorted_words, key=calculate_gematry) 16 | 17 | for elem in sorted_words: 18 | print(elem) 19 | -------------------------------------------------------------------------------- /nested_lists/chess_board.py: -------------------------------------------------------------------------------- 1 | arr = [int(elem) for elem in input().split()] 2 | flag = False 3 | 4 | for i in range(arr[0]): 5 | result = [] 6 | for j in range(arr[1]): 7 | if flag: 8 | result.append('*') 9 | flag = not flag 10 | else: 11 | result.append('.') 12 | flag = not flag 13 | print(*result) 14 | if arr[1]%2 == 0: 15 | flag = not flag 16 | -------------------------------------------------------------------------------- /nested_lists/chunk_splitting.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | chunkSize = int(input()) 3 | 4 | arr = s.split() 5 | result = [] 6 | curIdx = 0 7 | 8 | for i in range(len(arr)): 9 | if i == 0: 10 | result.append([arr[i]]) 11 | elif len(result[curIdx]) < chunkSize: 12 | result[curIdx].append(arr[i]) 13 | else: 14 | result.append([arr[i]]) 15 | curIdx += 1 16 | 17 | print(result) 18 | -------------------------------------------------------------------------------- /nested_lists/collateral_diag.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | for i in range(n): 4 | arr = [] 5 | for j in range(n): 6 | if i == n - j - 1: 7 | arr.append(1) 8 | elif i > n - j - 1: 9 | arr.append(2) 10 | else: 11 | arr.append(0) 12 | print(*arr) 13 | -------------------------------------------------------------------------------- /nested_lists/cols_changing.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | n = int(input()) 4 | m = int(input()) 5 | 6 | arr = [] 7 | 8 | for i in range(n): 9 | arr.append(input().split()) 10 | 11 | idxs = input().split() 12 | 13 | arr = np.array(arr) 14 | arr[:,[int(idxs[0]),int(idxs[1])]] = arr[:,[int(idxs[1]),int(idxs[0])]] 15 | 16 | for i in range(n): 17 | print(*arr[i]) 18 | -------------------------------------------------------------------------------- /nested_lists/diag_filling.py: -------------------------------------------------------------------------------- 1 | arr = [int(elem) for elem in input().split()] 2 | elem = 1 3 | result = [] 4 | sumIdx = arr[0] + arr[1] - 1 5 | d = {} 6 | minimum = min(arr) 7 | 8 | for i in range(sumIdx): 9 | if i < minimum: 10 | d[i] = [i + 1, 0] 11 | elif i > sumIdx - minimum: 12 | d[i] = [sumIdx - i, 0] 13 | else: 14 | d[i] = [minimum, 0] 15 | 16 | for i in range(arr[0]): 17 | result.append([]) 18 | for j in range(arr[1]): 19 | if i+j == 0: 20 | d[i+j][1] += 1 21 | elif d[i+j][1] == 0: 22 | d[i+j][1] = d[i+j-1][1] + d[i+j-1][0] + 1 23 | else: 24 | d[i+j][1] += 1 25 | d[i+j][0] -= 1 26 | result[i].append(d[i+j][1]) 27 | 28 | for i in range(arr[0]): 29 | print(*result[i]) 30 | -------------------------------------------------------------------------------- /nested_lists/diags_swap.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | arr = [] 3 | 4 | for i in range(n): 5 | arr.append(input().split()) 6 | 7 | for i in range(n): 8 | for j in range(n): 9 | if i == j: 10 | arr[i][j], arr[n-1-j][i] = arr[n-1-j][i] , arr[i][j] 11 | break 12 | for i in range(n): 13 | print(*arr[i]) 14 | -------------------------------------------------------------------------------- /nested_lists/duplicate_packaging.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | 3 | arr = s.split() 4 | result = [] 5 | curIdx = 0 6 | 7 | for i in range(len(arr)): 8 | if i == 0: 9 | result.append([arr[i]]) 10 | elif arr[i] != arr[i-1]: 11 | result.append([arr[i]]) 12 | curIdx += 1 13 | else: 14 | result[curIdx].append(arr[i]) 15 | 16 | print(result) 17 | -------------------------------------------------------------------------------- /nested_lists/filling_1.py: -------------------------------------------------------------------------------- 1 | arr = [int(elem) for elem in input().split()] 2 | elem = 1 3 | for i in range(arr[0]): 4 | result =[] 5 | for j in range(arr[1]): 6 | result.append(elem) 7 | elem += 1 8 | print(*result) 9 | -------------------------------------------------------------------------------- /nested_lists/filling_2.py: -------------------------------------------------------------------------------- 1 | arr = [int(elem) for elem in input().split()] 2 | elem = 1 3 | for i in range(arr[0]): 4 | result =[] 5 | for j in range(arr[1]): 6 | result.append(elem) 7 | elem += arr[0] 8 | print(*result) 9 | elem = result[0] + 1 10 | -------------------------------------------------------------------------------- /nested_lists/filling_3.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | for i in range(n): 4 | result =[] 5 | for j in range(n): 6 | if i == j or i == n - j - 1: 7 | result.append(1) 8 | else: 9 | result.append(0) 10 | print(*result) 11 | -------------------------------------------------------------------------------- /nested_lists/filling_4.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | for i in range(n): 4 | result =[] 5 | for j in range(n): 6 | if (i >= j and i >= n - 1 - j) or (i <= j and i <= n - 1 - j): 7 | result.append(1) 8 | else: 9 | result.append(0) 10 | print(*result) 11 | -------------------------------------------------------------------------------- /nested_lists/filling_5.py: -------------------------------------------------------------------------------- 1 | arr = [int(elem) for elem in input().split()] 2 | elem = 1 3 | 4 | for i in range(arr[0]): 5 | result = [] 6 | for j in range(arr[1]): 7 | result.append(elem) 8 | if elem >= arr[1]: 9 | elem = 1 10 | else: 11 | elem += 1 12 | print(*result) 13 | if elem >= arr[1]: 14 | elem = 1 15 | else: 16 | elem = result[0] + 1 17 | -------------------------------------------------------------------------------- /nested_lists/horse_move.py: -------------------------------------------------------------------------------- 1 | position = [sym for sym in input()] 2 | 3 | d = {'a':1, 'b':2, 'c':3, 'd':4, 'e':5, 'f':6, 'g':7, 'h':8} 4 | position[0] = d[position[0]] 5 | position[1] = int(position[1]) 6 | 7 | for i in range(8): 8 | arr = [] 9 | for j in range(8): 10 | if j == position[0] - 1 and i == 8 - position[1]: 11 | arr.append('N') 12 | elif position[0]-2 > 0 and j == position[0] - 3 and (i == 8 - position[1] + 1 or i == 8 - position[1] - 1): 13 | arr.append('*') 14 | elif position[0]+2 < 9 and j == position[0] + 1 and (i == 8 - position[1] + 1 or i == 8 - position[1] - 1): 15 | arr.append('*') 16 | elif position[1] - 2 > 0 and i == 8-position[1]+2 and (j == position[0] - 2 or j == position[0]): 17 | arr.append('*') 18 | elif position[1] + 2 < 9 and i == 8-position[1]-2 and (j == position[0] - 2 or j == position[0]): 19 | arr.append('*') 20 | else: 21 | arr.append('.') 22 | print(*arr) 23 | -------------------------------------------------------------------------------- /nested_lists/magic_square.py: -------------------------------------------------------------------------------- 1 | def IsMagicSquare(arr, n, summa): 2 | sumDiag1 = 0 3 | sumDiag2 = 0 4 | sumCols = [0] * n 5 | for i in range(n): 6 | if len(arr[i]) != len(set(arr[i])): 7 | return False 8 | if sum(arr[i]) != summa: 9 | return False 10 | for j in range(n): 11 | if arr[i][j] < 1 or arr[i][j] > pow(n, 2): 12 | return False 13 | sumCols[j] += arr[i][j] 14 | if i == j: 15 | sumDiag1 += arr[i][j] 16 | sumDiag2 += arr[i][n - 1- j] 17 | 18 | if sumDiag1 != summa or sumDiag2 != summa: 19 | return False 20 | for i in range(n): 21 | if sumCols[i] != summa: 22 | return False 23 | return True 24 | 25 | n = int(input()) 26 | arr = [] 27 | 28 | for i in range(n): 29 | arr.append([int(elem) for elem in input().split()]) 30 | 31 | summa = sum(arr[0]) 32 | print('YES' if IsMagicSquare(arr, n, summa) else 'NO') 33 | -------------------------------------------------------------------------------- /nested_lists/matrix_addition.py: -------------------------------------------------------------------------------- 1 | size = [int(elem) for elem in input().split()] 2 | arr1 = [] 3 | arr2 = [] 4 | summa = [] 5 | 6 | for i in range(size[0]): 7 | arr1.append([int(elem) for elem in input().split()]) 8 | 9 | input() 10 | 11 | for i in range(size[0]): 12 | arr2.append([int(elem) for elem in input().split()]) 13 | 14 | for i in range(size[0]): 15 | summa.append([]) 16 | for j in range(size[1]): 17 | summa[i].append(arr1[i][j] + arr2[i][j]) 18 | 19 | for i in range(size[0]): 20 | print(*summa[i]) 21 | -------------------------------------------------------------------------------- /nested_lists/matrix_multiplication.py: -------------------------------------------------------------------------------- 1 | size1 = [int(elem) for elem in input().split()] 2 | arr1 = [] 3 | arr2 = [] 4 | prod = [] 5 | 6 | for i in range(size1[0]): 7 | arr1.append([int(elem) for elem in input().split()]) 8 | 9 | input() 10 | 11 | size2 = [int(elem) for elem in input().split()] 12 | 13 | for i in range(size2[0]): 14 | arr2.append([int(elem) for elem in input().split()]) 15 | 16 | for i in range(size1[0]): 17 | prod.append([]) 18 | newArr1 = arr1[i] 19 | for j in range(size2[1]): 20 | newArr2 = [row[j] for row in arr2] 21 | tmp = 0 22 | for k in range(len(newArr1)): 23 | tmp += newArr1[k] * newArr2[k] 24 | prod[i].append(tmp) 25 | 26 | for i in prod: 27 | print(*i) 28 | -------------------------------------------------------------------------------- /nested_lists/matrix_power.py: -------------------------------------------------------------------------------- 1 | def MatrixMultiply(arr1, arr2, n): 2 | prod = [] 3 | for i in range(n): 4 | prod.append([]) 5 | newArr1 = arr1[i] 6 | for j in range(n): 7 | newArr2 = [row[j] for row in arr2] 8 | tmp = 0 9 | for k in range(n): 10 | tmp += newArr1[k] * newArr2[k] 11 | prod[i].append(tmp) 12 | return prod 13 | 14 | def PowerMatrix(arr, power, n): 15 | if power == 0: 16 | pass 17 | elif power == 1: 18 | return arr 19 | elif power == 2: 20 | return MatrixMultiply(arr, arr, n) 21 | elif power >= 3: 22 | return MatrixMultiply(PowerMatrix(arr, power - 1, n), arr, n) 23 | 24 | n = int(input()) 25 | arr = [] 26 | prod = [] 27 | 28 | for i in range(n): 29 | arr.append([int(elem) for elem in input().split()]) 30 | 31 | power = int(input()) 32 | 33 | prod = PowerMatrix(arr, power, n) 34 | 35 | for i in prod: 36 | print(*i) 37 | -------------------------------------------------------------------------------- /nested_lists/matrix_trace.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | arr = [] 3 | sum = 0 4 | 5 | for i in range(n): 6 | arr.append([int(i) for i in input().split()]) 7 | sum += arr[i][i] 8 | 9 | print(sum) 10 | -------------------------------------------------------------------------------- /nested_lists/matrix_turn.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | arr = [] 3 | result = [] 4 | 5 | for i in range(n): 6 | arr.append(input().split()) 7 | 8 | for j in range(n): 9 | result.append([]) 10 | for i in reversed(range(n)): 11 | result[j].append(arr[i][j]) 12 | 13 | for i in range(n): 14 | print(*result[i]) 15 | -------------------------------------------------------------------------------- /nested_lists/max_in_region_1.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | arr = [] 3 | maximum = -100; 4 | 5 | for i in range(n): 6 | arr.append([int(i) for i in input().split()][:i+1]) 7 | if maximum < max(arr[i]): 8 | maximum = max(arr[i]) 9 | 10 | print(maximum) 11 | -------------------------------------------------------------------------------- /nested_lists/max_in_region_2.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | arr = [] 3 | maximum = -100; 4 | 5 | for i in range(n): 6 | arr.append([int(i) for i in input().split()]) 7 | 8 | for i in range(n): 9 | for j in range(n): 10 | if (i>=j and i <= n-1-j) or (i<=j and i>=n-1-j): 11 | if arr[i][j] > maximum: 12 | maximum = arr[i][j] 13 | 14 | print(maximum) 15 | -------------------------------------------------------------------------------- /nested_lists/max_in_table.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | m = int(input()) 3 | 4 | indexes = [0]*2 5 | maximum = -100 6 | 7 | for i in range(n): 8 | arr = [int(i) for i in input().split()] 9 | valueMax = max(arr) 10 | if valueMax > maximum: 11 | maximum = valueMax 12 | indexMax = max(range(len(arr)), key=arr.__getitem__) 13 | indexes = [i, indexMax] 14 | 15 | print(*indexes) 16 | -------------------------------------------------------------------------------- /nested_lists/more_than_average.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | arr = [] 3 | 4 | for i in range(n): 5 | arr.append([int(i) for i in input().split()]) 6 | average = sum(arr[i]) / len(arr[i]) 7 | print(len([elem for elem in arr[i] if elem > average])) 8 | -------------------------------------------------------------------------------- /nested_lists/multiplication_table.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | m = int(input()) 3 | 4 | arr = [] 5 | 6 | for i in range(n): 7 | arr.append([]) 8 | for j in range(m): 9 | arr[i].append(i*j) 10 | 11 | for i in range(n): 12 | print(' '.join([str(item).ljust(3) for item in arr[i]])) 13 | -------------------------------------------------------------------------------- /nested_lists/nested_lists_1.py: -------------------------------------------------------------------------------- 1 | list1 = [10, 20, [300, 400, [5000, 6000], 500], 30, 40] 2 | 3 | list1[2][2].append(7000) 4 | 5 | print(list1) 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /nested_lists/nested_lists_2.py: -------------------------------------------------------------------------------- 1 | list1 = ['a', 'b', ['c', ['d', 'e', ['f', 'g'], 'k'], 'l'], 'm', 'n'] 2 | sub_list = ['h', 'i', 'j'] 3 | 4 | list1[2][1][2].extend(sub_list) 5 | 6 | print(list1) 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /nested_lists/nested_lists_3.py: -------------------------------------------------------------------------------- 1 | list1 = [[1, 7, 8], [9, 7, 102], [6, 106, 105], [100, 99, 98, 103], [1, 2, 3]] 2 | maximum = -1 3 | 4 | for l in list1: 5 | if max(l) > maximum: 6 | maximum = max(l) 7 | 8 | print(maximum) 9 | -------------------------------------------------------------------------------- /nested_lists/nested_lists_4.py: -------------------------------------------------------------------------------- 1 | list1 = [[1, 7, 8], [9, 7, 102], [102, 106, 105], [100, 99, 98, 103], [1, 2, 3]] 2 | 3 | for l in list1: 4 | l.reverse() 5 | 6 | 7 | print(list1) 8 | -------------------------------------------------------------------------------- /nested_lists/nested_lists_5.py: -------------------------------------------------------------------------------- 1 | list1 = [[1, 7, 8], [9, 7, 102], [102, 106, 105], [100, 99, 98, 103], [1, 2, 3]] 2 | total = 0 3 | counter = 0 4 | 5 | for l in list1: 6 | for elem in l: 7 | total += elem 8 | counter += 1 9 | 10 | print(total / counter) 11 | -------------------------------------------------------------------------------- /nested_lists/pascal_triangle_1.py: -------------------------------------------------------------------------------- 1 | def factorial(n): 2 | if n > 1: 3 | return n * factorial(n - 1) 4 | return 1 5 | 6 | def getPascalStr(n): 7 | result = [] 8 | for i in range(n + 1): 9 | result.append(int(factorial(n) / (factorial(i) * factorial(n - i)))) 10 | return result 11 | 12 | n = int(input()) 13 | 14 | print(getPascalStr(n)) 15 | -------------------------------------------------------------------------------- /nested_lists/pascal_triangle_2.py: -------------------------------------------------------------------------------- 1 | def factorial(n): 2 | if n > 1: 3 | return n * factorial(n - 1) 4 | return 1 5 | 6 | def getPascalStr(n): 7 | result = [] 8 | for i in range(n + 1): 9 | result.append(int(factorial(n) / (factorial(i) * factorial(n - i)))) 10 | return result 11 | 12 | n = int(input()) 13 | 14 | for i in range(n): 15 | print(*getPascalStr(i)) 16 | -------------------------------------------------------------------------------- /nested_lists/print_matrix_1.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | m = int(input()) 3 | 4 | arr = [] 5 | counter = 0 6 | 7 | for i in range(n*m): 8 | if i%m == 0: 9 | arr.append([input()]) 10 | elif i%m == m-1: 11 | arr[counter].append(input()) 12 | counter += 1 13 | else: 14 | arr[counter].append(input()) 15 | 16 | for i in range(n): 17 | print(*arr[i]) 18 | -------------------------------------------------------------------------------- /nested_lists/print_matrix_2.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | m = int(input()) 3 | 4 | arr = [] 5 | counter = 0 6 | 7 | for i in range(n*m): 8 | if i%m == 0: 9 | arr.append([input()]) 10 | elif i%m == m-1: 11 | arr[counter].append(input()) 12 | counter += 1 13 | else: 14 | arr[counter].append(input()) 15 | 16 | for i in range(n): 17 | print(*arr[i]) 18 | 19 | print() 20 | 21 | for i in range(m): 22 | result = [] 23 | for j in range(n): 24 | result.append(arr[j][i]) 25 | print(*result) 26 | -------------------------------------------------------------------------------- /nested_lists/reflection.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | arr = [] 3 | 4 | for i in range(n): 5 | arr.append(input().split()) 6 | 7 | for i in range(n//2): 8 | for j in range(n): 9 | arr[i][j], arr[n-1-i][j] = arr[n-1-i][j] , arr[i][j] 10 | for i in range(n): 11 | print(*arr[i]) 12 | -------------------------------------------------------------------------------- /nested_lists/snake_filling.py: -------------------------------------------------------------------------------- 1 | arr = [int(elem) for elem in input().split()] 2 | elem = 1 3 | isEven = False 4 | 5 | for i in range(arr[0]): 6 | result = [] 7 | for j in range(arr[1]): 8 | result.append(str(elem).ljust(3)) 9 | if isEven: 10 | elem -= 1 11 | else: 12 | elem += 1 13 | print(*result) 14 | if isEven: 15 | elem += arr[1] + 1 16 | else: 17 | elem += arr[1] - 1 18 | isEven = not isEven 19 | -------------------------------------------------------------------------------- /nested_lists/spiral_filling.py: -------------------------------------------------------------------------------- 1 | arr = [int(elem) for elem in input().split()] 2 | n = arr[0] 3 | m = arr[1] 4 | 5 | a = [[0]*arr[1] for i in range(arr[0])] 6 | 7 | Ibeg = 0 8 | Ifin = 0 9 | Jbeg = 0 10 | Jfin = 0 11 | 12 | k = 1 13 | i = 0 14 | j = 0 15 | 16 | while (k <= n * m): 17 | a[i][j] = k; 18 | if i == Ibeg and j < m - Jfin - 1: 19 | j += 1 20 | elif j == m - Jfin - 1 and i < n - Ifin - 1: 21 | i += 1 22 | elif i == n - Ifin - 1 and j > Jbeg: 23 | j -= 1 24 | else: 25 | i -= 1 26 | 27 | if (i == Ibeg + 1) and (j == Jbeg) and (Jbeg != m - Jfin - 1): 28 | Ibeg += 1 29 | Ifin += 1 30 | Jbeg += 1 31 | Jfin += 1 32 | 33 | k += 1 34 | 35 | for i in range(n): 36 | result = [] 37 | for j in range(m): 38 | result.append(str(a[i][j]).ljust(3)) 39 | print(*result) 40 | -------------------------------------------------------------------------------- /nested_lists/sublists_of_lists.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | 3 | arr = s.split() 4 | result = [] 5 | result.append([]) 6 | 7 | for i in range(len(arr)): 8 | result.append([arr[i]]) 9 | for j in range(i, len(arr)): 10 | if (i != j): 11 | result.append(arr[i:j+1]) 12 | 13 | result.sort(key=len) 14 | print(result) 15 | -------------------------------------------------------------------------------- /nested_lists/sum_of_quaters.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | arr = [] 3 | maximum = -100; 4 | 5 | for i in range(n): 6 | arr.append([int(i) for i in input().split()]) 7 | 8 | sumArr = [0]*4 9 | 10 | for i in range(n): 11 | for j in range(n): 12 | if (i n-1-j): 15 | sumArr[1] += arr[i][j] 16 | elif (i>j and i > n-1-j): 17 | sumArr[2] += arr[i][j] 18 | elif (i>j and i < n-1-j): 19 | sumArr[3] += arr[i][j] 20 | 21 | print('Верхняя четверть: ' + str(sumArr[0])) 22 | print('Правая четверть: ' + str(sumArr[1])) 23 | print('Нижняя четверть: ' + str(sumArr[2])) 24 | print('Левая четверть: ' + str(sumArr[3])) 25 | -------------------------------------------------------------------------------- /nested_lists/symmetric_matrix.py: -------------------------------------------------------------------------------- 1 | def IsSymmetric(arr): 2 | for i in range(n): 3 | for j in range(n): 4 | if i !=j: 5 | if arr[i][j] != arr[j][i]: 6 | return 'NO' 7 | return 'YES' 8 | 9 | n = int(input()) 10 | arr = [] 11 | 12 | for i in range(n): 13 | arr.append(input().split()) 14 | 15 | print(IsSymmetric(arr)); 16 | -------------------------------------------------------------------------------- /nested_lists/template_list_1.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | for i in range(n): 4 | print([i for i in range(1, n+1)]) 5 | -------------------------------------------------------------------------------- /nested_lists/template_list_2.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | for i in range(n): 4 | print([i for i in range(1, i+2)]) 5 | -------------------------------------------------------------------------------- /nested_lists_exam/diags_parallel_to_main.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | arr = [] 3 | 4 | for i in range(n): 5 | arr.append([]) 6 | for j in range(n): 7 | arr[i].append(abs(i-j)) 8 | 9 | for i in range(n): 10 | print(*arr[i]) 11 | -------------------------------------------------------------------------------- /nested_lists_exam/every_nth_elem.py: -------------------------------------------------------------------------------- 1 | arr = input().split() 2 | n = int(input()) 3 | result = [] 4 | 5 | for i in range(n): 6 | result.append([]) 7 | 8 | for i in range(len(arr)): 9 | result[i%n].append(arr[i]) 10 | 11 | print(result) 12 | -------------------------------------------------------------------------------- /nested_lists_exam/latin_square.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | def is_every_digit_in_arr(arr, n): 4 | for i in range(1, n + 1): 5 | if i not in arr: 6 | return False 7 | return True 8 | 9 | def is_latin_square(arr, n): 10 | result = [] 11 | a_t = np.matrix(arr).transpose().tolist() 12 | for i in range(n): 13 | arr[i] = sorted(arr[i]) 14 | if (result != [] and result != arr[i]) or not is_every_digit_in_arr(arr[i], n): 15 | return 'NO' 16 | result = arr[i] 17 | result = [] 18 | for i in range(n): 19 | a_t[i] = sorted(a_t[i]) 20 | if (result != [] and result != a_t[i]) or not is_every_digit_in_arr(arr[i], n): 21 | return 'NO' 22 | result = a_t[i] 23 | 24 | return 'YES' 25 | 26 | n = int(input()) 27 | arr = [] 28 | 29 | for i in range(n): 30 | arr.append([int(elem) for elem in input().split()]) 31 | 32 | print(is_latin_square(arr, n)) 33 | -------------------------------------------------------------------------------- /nested_lists_exam/matrix_transporting.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | n = int(input()) 4 | arr = [] 5 | 6 | for i in range(n): 7 | arr.append([int(elem) for elem in input().split()]) 8 | 9 | a_t = np.matrix(arr).transpose().tolist() 10 | 11 | for i in range(n): 12 | print(*a_t[i]) 13 | -------------------------------------------------------------------------------- /nested_lists_exam/max_in_region_2.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | arr = [] 3 | maximum = 0 4 | 5 | for i in range(n): 6 | arr.append([int(elem) for elem in input().split()]) 7 | 8 | for i in range(n): 9 | for j in range(n): 10 | if i >= n - 1 - j and arr[i][j] > maximum: 11 | maximum = arr[i][j] 12 | 13 | print(maximum) 14 | -------------------------------------------------------------------------------- /nested_lists_exam/queen_moves.py: -------------------------------------------------------------------------------- 1 | slovarx = {'a':0, 'b':1, 'c':2, 'd':3, 'e':4, 'f':5, 'g':6, 'h':7} 2 | slovary = {'8':0, '7':1, '6':2, '5':3, '4':4, '3':5, '2':6, '1':7, '0':8} 3 | 4 | position = input() 5 | arr = [] 6 | 7 | for i in range(8): 8 | arr.append([]) 9 | for j in range(8): 10 | if j == slovarx[position[0]] and i == slovary[position[1]]: 11 | arr[i].append('Q') 12 | elif j == slovarx[position[0]] or i == slovary[position[1]]: 13 | arr[i].append('*') 14 | elif abs(slovarx[position[0]] - j) == abs(slovary[position[1]] - i): 15 | arr[i].append('*') 16 | else: 17 | arr[i].append('.') 18 | 19 | for i in range(8): 20 | print(*arr[i]) 21 | -------------------------------------------------------------------------------- /nested_lists_exam/snowflake.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | arr = [] 3 | 4 | for i in range(n): 5 | arr.append([]) 6 | for j in range(n): 7 | if i == n//2 or j == n//2 or i == j or i == n-j-1: 8 | arr[i].append('*') 9 | else: 10 | arr[i].append('.') 11 | 12 | for i in range(n): 13 | print(*arr[i]) 14 | -------------------------------------------------------------------------------- /nested_lists_exam/symmetric_matrix.py: -------------------------------------------------------------------------------- 1 | def is_symmetric(arr, n): 2 | for i in range(n): 3 | for j in range(n): 4 | if arr[i][j] != arr[n-j-1][n-i-1]: 5 | return 'NO' 6 | return 'YES' 7 | 8 | n = int(input()) 9 | arr = [] 10 | 11 | for i in range(n): 12 | arr.append([int(elem) for elem in input().split()]) 13 | 14 | print(is_symmetric(arr, n)) 15 | -------------------------------------------------------------------------------- /random_string/monte_carlo_1.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | n = 1000 4 | k = 0 5 | s0 = 16 6 | for _ in range(n): 7 | x = random.uniform(-2, 2) 8 | y = random.uniform(-2, 2) 9 | 10 | if x**3 + y**4 + 2 >= 0 and 3*x + y**2 <= 2: 11 | k += 1 12 | 13 | print((k/n)*s0) 14 | -------------------------------------------------------------------------------- /random_string/monte_carlo_2.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | n = 10**6 # количество испытаний 4 | k = 0.0 5 | for i in range(n): 6 | x = random.random() 7 | y = random.random() 8 | k += (x * x + y * y < 1.0) 9 | print(4 * k / n) 10 | -------------------------------------------------------------------------------- /random_string/password_generator_1.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | def generate_password(length): 4 | s = "qwertyuipasdfghjkzxcvbnmQWERTYUPASDFGHJKLZXCVBNM23456789" 5 | print(''.join(random.sample([elem for elem in s], length))) 6 | 7 | def generate_passwords(count, length): 8 | for i in range(count): 9 | generate_password(length) 10 | 11 | n, m = int(input()), int(input()) 12 | 13 | generate_passwords(n, m) 14 | -------------------------------------------------------------------------------- /random_string/password_generator_2.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | def generate_password(length): 4 | result = [] 5 | digits = '23456789'; 6 | small_letters = 'qwertyuipasdfghjkzxcvbnm'; 7 | huge_letters = 'QWERTYUPASDFGHJKLZXCVBNM'; 8 | s = "qwertyuipasdfghjkzxcvbnmQWERTYUPASDFGHJKLZXCVBNM23456789" 9 | result += random.sample([elem for elem in digits], 1) 10 | result += random.sample([elem for elem in small_letters], 1) 11 | result += random.sample([elem for elem in huge_letters], 1) 12 | result += random.sample([elem for elem in s], length - 3) 13 | print(''.join(result)) 14 | 15 | def generate_passwords(count, length): 16 | for i in range(count): 17 | generate_password(length) 18 | 19 | n, m = int(input()), int(input()) 20 | 21 | generate_passwords(n, m) 22 | -------------------------------------------------------------------------------- /random_string/random_string_1.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | n = int(input()) 4 | 5 | for i in range(n): 6 | print('Орел' if random.randint(0,1) == 0 else 'Решка') 7 | -------------------------------------------------------------------------------- /random_string/random_string_10.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | arr = [str(i).ljust(3) for i in range(1,76)] 4 | 5 | result = random.sample(arr, 25) 6 | result[12] = '0'.ljust(3) 7 | 8 | for i in range(5): 9 | print(' '.join(result[i*5:i*5+5])) 10 | -------------------------------------------------------------------------------- /random_string/random_string_2.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | n = int(input()) 4 | 5 | for i in range(n): 6 | print(random.randint(1,6)) 7 | -------------------------------------------------------------------------------- /random_string/random_string_3.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | length = int(input()) 4 | result = "" 5 | 6 | for i in range(length): 7 | result += chr(random.randint(65,90)) 8 | 9 | print(result) 10 | -------------------------------------------------------------------------------- /random_string/random_string_4.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | result = set() 4 | 5 | while len(result) < 7: 6 | result.add(random.randint(1,49)) 7 | 8 | print(*sorted(result)) 9 | -------------------------------------------------------------------------------- /random_string/random_string_5.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | def generate_ip(): 4 | result = "" 5 | 6 | for i in range(4): 7 | result += str(random.randint(0,255)) + '.' 8 | 9 | return result[:-1]; 10 | -------------------------------------------------------------------------------- /random_string/random_string_6.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | def generate_index(): 4 | result = '' 5 | 6 | result += chr(random.randint(65, 90)) 7 | result += chr(random.randint(65, 90)) 8 | result += str(random.randint(0, 99)) 9 | result += '_' 10 | result += str(random.randint(0, 99)) 11 | result += chr(random.randint(65, 90)) 12 | result += chr(random.randint(65, 90)) 13 | 14 | return result 15 | -------------------------------------------------------------------------------- /random_string/random_string_7.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | matrix = [[1, 2, 3, 4], 4 | [5, 6, 7, 8], 5 | [9, 10, 11, 12], 6 | [13, 14, 15, 16]] 7 | 8 | for elem in matrix: 9 | random.shuffle(elem) 10 | -------------------------------------------------------------------------------- /random_string/random_string_8.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | arr = [i for i in range(1,10)] 4 | 5 | for i in range(100): 6 | print(''.join([str(i) for i in random.sample(arr, 7)])) 7 | -------------------------------------------------------------------------------- /random_string/random_string_9.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | inputStr = [i for i in input()] 4 | 5 | random.shuffle(inputStr) 6 | 7 | print(''.join(inputStr)) 8 | -------------------------------------------------------------------------------- /random_string/secret_friend.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | n = int(input()) 4 | 5 | arr = [input() for i in range(n)] 6 | size = len(arr) 7 | arr += arr 8 | 9 | 10 | for i in range(size): 11 | if (arr[i] == arr[size*2 - i - 1]): 12 | arr[i], arr[i+1] = arr[i+1], arr[i] 13 | print(arr[i] + ' - ' + arr[size*2 - i - 1]) 14 | -------------------------------------------------------------------------------- /sets/all_10_digits.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | l = input() 3 | 4 | print('YES' if len(set(l + s)) == 10 else 'NO') 5 | -------------------------------------------------------------------------------- /sets/all_digits.py: -------------------------------------------------------------------------------- 1 | set1 = set([int(elem) for elem in input()]) 2 | set2 = set([int(elem) for elem in input()]) 3 | 4 | print('YES' if set1.issuperset(set2) else 'NO') 5 | -------------------------------------------------------------------------------- /sets/amount_of_words_in_text.py: -------------------------------------------------------------------------------- 1 | s = input().lower() 2 | 3 | for elem in '.,;:-?!': 4 | s = s.replace(elem, '') 5 | 6 | print(len(set(s.split()))) 7 | -------------------------------------------------------------------------------- /sets/biology_lesson.py: -------------------------------------------------------------------------------- 1 | set1 = set([int(elem) for elem in input().split()]) 2 | set2 = set([int(elem) for elem in input().split()]) 3 | set3 = set([int(elem) for elem in input().split()]) 4 | superSet = {0,1,2,3,4,5,6,7,8,9,10} 5 | 6 | print(*sorted(superSet - set1 - set2 - set3)) 7 | -------------------------------------------------------------------------------- /sets/books_to_read.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | m = int(input()) 3 | k = int(input()) 4 | x = int(input()) 5 | y = int(input()) 6 | z = int(input()) 7 | t = int(input()) 8 | a = int(input()) 9 | 10 | onlyOneBook = 2*(x + y + z) - 3*(n+m+k) + 3*t 11 | twoBooks = 2*(n+m+k) - x - y - z - 3*t 12 | noBooks = a + n + m + k - x - y - z - t 13 | 14 | print(onlyOneBook) 15 | print(twoBooks) 16 | print(noBooks) 17 | -------------------------------------------------------------------------------- /sets/common_digits.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | for i in range(n): 4 | if i == 0: 5 | set1 = set([int(elem) for elem in input()]) 6 | else: 7 | set1.intersection_update(set([int(elem) for elem in input()])) 8 | 9 | 10 | print(*sorted(set1)) 11 | -------------------------------------------------------------------------------- /sets/common_numbers.py: -------------------------------------------------------------------------------- 1 | set1 = set([int(elem) for elem in input().split()]) 2 | set2 = set([int(elem) for elem in input().split()]) 3 | 4 | print(*sorted(set1.intersection(set2))) 5 | -------------------------------------------------------------------------------- /sets/dif_symbols_amount.py: -------------------------------------------------------------------------------- 1 | print(len(set(input()))) 2 | -------------------------------------------------------------------------------- /sets/first_string_sums.py: -------------------------------------------------------------------------------- 1 | set1 = set([int(elem) for elem in input().split()]) 2 | set2 = set([int(elem) for elem in input().split()]) 3 | 4 | print(*sorted(set1.difference(set2))) 5 | -------------------------------------------------------------------------------- /sets/informatics_lesson.py: -------------------------------------------------------------------------------- 1 | set1 = set([int(elem) for elem in input().split()]) 2 | set2 = set([int(elem) for elem in input().split()]) 3 | set3 = set([int(elem) for elem in input().split()]) 4 | 5 | set1.intersection_update(set2) 6 | print(*sorted(set1 - set3, reverse = True)) 7 | -------------------------------------------------------------------------------- /sets/is_repeated_num.py: -------------------------------------------------------------------------------- 1 | s = [int(i) for i in input().split()] 2 | arr = set() 3 | 4 | for elem in s: 5 | if elem in arr: 6 | print('YES') 7 | else: 8 | print('NO') 9 | arr.add(elem) 10 | -------------------------------------------------------------------------------- /sets/maths_lesson.py: -------------------------------------------------------------------------------- 1 | set1 = set([int(elem) for elem in input().split()]) 2 | set2 = set([int(elem) for elem in input().split()]) 3 | set3 = set([int(elem) for elem in input().split()]) 4 | 5 | print(*sorted(set1.union(set2).union(set3) - set1.intersection(set2).intersection(set3))) 6 | -------------------------------------------------------------------------------- /sets/non_repeated_digits.py: -------------------------------------------------------------------------------- 1 | s = input() 2 | print('YES' if len(set(s)) == len(s) else "NO") 3 | -------------------------------------------------------------------------------- /sets/physics_lesson.py: -------------------------------------------------------------------------------- 1 | set1 = set([int(elem) for elem in input().split()]) 2 | set2 = set([int(elem) for elem in input().split()]) 3 | set3 = set([int(elem) for elem in input().split()]) 4 | 5 | print(*sorted(set3 - set1.union(set2), reverse=True)) 6 | -------------------------------------------------------------------------------- /sets/repeated_amount.py: -------------------------------------------------------------------------------- 1 | set1 = set([int(elem) for elem in input().split()]) 2 | set2 = set([int(elem) for elem in input().split()]) 3 | 4 | print(len(set1.intersection(set2))) 5 | -------------------------------------------------------------------------------- /sets/same_digits.py: -------------------------------------------------------------------------------- 1 | set1 = set([int(elem) for elem in input()]) 2 | set2 = set([int(elem) for elem in input()]) 3 | 4 | print('NO' if set1.isdisjoint(set2) else 'YES') 5 | -------------------------------------------------------------------------------- /sets/same_sets.py: -------------------------------------------------------------------------------- 1 | print('YES' if set(input()) == set(input()) else 'NO') 2 | -------------------------------------------------------------------------------- /sets/sets_1.py: -------------------------------------------------------------------------------- 1 | numbers = {1.414, 12.5, 3.1415, 2.718, 9.8, 1.414, 1.1618, 1.324, 2.718, 1.324} 2 | 3 | print(min(numbers) + max(numbers)) 4 | -------------------------------------------------------------------------------- /sets/sets_2.py: -------------------------------------------------------------------------------- 1 | numbers = {20, 6, 8, 18, 18, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 12, 8, 8, 10, 4, 2, 2, 2, 16, 20} 2 | average = sum(numbers)/ len(numbers) 3 | 4 | print(average) 5 | -------------------------------------------------------------------------------- /sets/sets_3.py: -------------------------------------------------------------------------------- 1 | numbers = {9089, -67, -32, 1, 78, 23, -65, 99, 9089, 34, -32, 0, -67, 1, 11, 111, 111, 1, 23} 2 | sum = 0 3 | 4 | for num in numbers: 5 | sum += pow(num, 2) 6 | 7 | print(sum) 8 | -------------------------------------------------------------------------------- /sets/sets_4.py: -------------------------------------------------------------------------------- 1 | fruits = {'apple', 'banana', 'cherry', 'avocado', 'pineapple', 'apricot', 'banana', 'avocado', 'grapefruit'} 2 | 3 | for elem in sorted(fruits, reverse=True): 4 | print(elem) 5 | -------------------------------------------------------------------------------- /sets/sets_generator_1.py: -------------------------------------------------------------------------------- 1 | items = [10, '30', 30, 10, '56', 34, '12', 90, 89, 34, 45, '67', 12, 10, 90, 23, '45', 56, '56', 1, 5, '6', 5] 2 | 3 | print(*sorted({int(elem) for elem in items})) 4 | -------------------------------------------------------------------------------- /sets/sets_generator_2.py: -------------------------------------------------------------------------------- 1 | words = ['Plum', 'Grapefruit', 'apple', 'orange', 'pomegranate', 'Cranberry', 'lime', 'Lemon', 'grapes', 'persimmon', 'tangerine', 'Watermelon', 'currant', 'Almond'] 2 | 3 | print(*sorted({elem[0].lower() for elem in words})) 4 | -------------------------------------------------------------------------------- /sets/sets_generator_3.py: -------------------------------------------------------------------------------- 1 | sentence = '''My very photogenic mother died in a freak accident (picnic, lightning) when I was three, and, save for a pocket of warmth in the darkest past, nothing of her subsists within the hollows and dells of memory, over which, if you can still stand my style (I am writing under observation), the sun of my infancy had set: surely, you all know those redolent remnants of day suspended, with the midges, about some hedge in bloom or suddenly entered and traversed by the rambler, at the bottom of a hill, in the summer dusk; a furry warmth, golden midges.''' 2 | 3 | 4 | print(*sorted({elem.strip(':,.!?();').lower() for elem in sentence.split()})) 5 | -------------------------------------------------------------------------------- /sets/sets_generator_4.py: -------------------------------------------------------------------------------- 1 | sentence = '''My very photogenic mother died in a freak accident (picnic, lightning) when I was three, and, save for a pocket of warmth in the darkest past, nothing of her subsists within the hollows and dells of memory, over which, if you can still stand my style (I am writing under observation), the sun of my infancy had set: surely, you all know those redolent remnants of day suspended, with the midges, about some hedge in bloom or suddenly entered and traversed by the rambler, at the bottom of a hill, in the summer dusk; a furry warmth, golden midges.''' 2 | 3 | print(*sorted({elem.strip(':,.!?();').lower() for elem in sentence.split() if len(elem.strip(':,.!?();').lower()) < 4})) 4 | -------------------------------------------------------------------------------- /sets/sets_generator_5.py: -------------------------------------------------------------------------------- 1 | files = ['python.png', 'qwerty.py', 'stepik.png', 'beegeek.org', 'windows.pnp', 'pen.txt', 'phone.py', 'book.txT', 'board.pNg', 'keyBoard.jpg', 'Python.PNg', 'apple.jpeg', 'png.png', 'input.tXt', 'split.pop', 'solution.Py', 'stepik.org', 'kotlin.ko', 'github.git'] 2 | 3 | 4 | print(*sorted({elem.lower() for elem in files if elem.lower().endswith('.png')})) 5 | -------------------------------------------------------------------------------- /sets/three_words.py: -------------------------------------------------------------------------------- 1 | arr = input().split() 2 | 3 | print('YES' if set(arr[0]) == set(arr[1]) and set(arr[0]) == set(arr[2]) else 'NO') 4 | -------------------------------------------------------------------------------- /sets/timur_team.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | m = int(input()) 3 | k = int(input()) 4 | x = int(input()) 5 | y = int(input()) 6 | z = int(input()) 7 | 8 | allPupils = z + m + k -y + n - x 9 | 10 | print(allPupils) 11 | -------------------------------------------------------------------------------- /sets/unique_symbols_1.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | for i in range(n): 4 | word = input().lower() 5 | print(len(set(word))) 6 | -------------------------------------------------------------------------------- /sets/unique_symbols_2.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | word = '' 3 | 4 | for i in range(n): 5 | word += input().lower() 6 | print(len(set(word))) 7 | -------------------------------------------------------------------------------- /sets_exam/beegeek_school_1.py: -------------------------------------------------------------------------------- 1 | arr1 = {int(elem) for elem in input().split()} 2 | arr2 = {int(elem) for elem in input().split()} 3 | 4 | if arr1 == arr2: 5 | print('YES') 6 | else: 7 | print('NO') 8 | -------------------------------------------------------------------------------- /sets_exam/beegeek_school_2.py: -------------------------------------------------------------------------------- 1 | m = int(input()) 2 | n = int(input()) 3 | 4 | maths = set() 5 | informatics = set() 6 | 7 | for i in range(m): 8 | maths.add(input()) 9 | 10 | for i in range(n): 11 | informatics.add(input()) 12 | 13 | print(len(maths.difference(informatics))) 14 | -------------------------------------------------------------------------------- /sets_exam/beegeek_school_3.py: -------------------------------------------------------------------------------- 1 | m = int(input()) 2 | n = int(input()) 3 | 4 | maths = set() 5 | informatics = set() 6 | 7 | for i in range(m): 8 | maths.add(input()) 9 | 10 | for i in range(n): 11 | informatics.add(input()) 12 | 13 | if len(maths.symmetric_difference(informatics)) == 0: 14 | print('NO') 15 | else: 16 | print(len(maths.symmetric_difference(informatics))) 17 | -------------------------------------------------------------------------------- /sets_exam/beegeek_school_4.py: -------------------------------------------------------------------------------- 1 | arr1 = {elem for elem in input().split()} 2 | arr2 = {elem for elem in input().split()} 3 | 4 | print(*sorted(arr1.union(arr2))) 5 | -------------------------------------------------------------------------------- /sets_exam/beegeek_school_5.py: -------------------------------------------------------------------------------- 1 | m = int(input()) 2 | n = int(input()) 3 | 4 | maths = set() 5 | 6 | for i in range(m + n): 7 | maths.add(input()) 8 | 9 | if len(maths) - (n + m - len(maths)) == 0: 10 | print('NO') 11 | else: 12 | print(len(maths) - (n + m - len(maths))) 13 | -------------------------------------------------------------------------------- /sets_exam/beegeek_school_6.py: -------------------------------------------------------------------------------- 1 | m = int(input()) 2 | resultArr = set() 3 | currArr = set() 4 | 5 | for i in range(m): 6 | n = int(input()) 7 | for j in range(n): 8 | if i == 0: 9 | resultArr.add(input()) 10 | else: 11 | currArr.add(input()) 12 | if i != 0: 13 | resultArr = resultArr.intersection(currArr) 14 | currArr = set() 15 | 16 | for elem in sorted(resultArr): 17 | print(elem) 18 | -------------------------------------------------------------------------------- /sets_exam/books_to_read.py: -------------------------------------------------------------------------------- 1 | m = int(input()) 2 | n = int(input()) 3 | library = [] 4 | toRead = [] 5 | 6 | for i in range(m): 7 | library.append(input()) 8 | 9 | for i in range(n): 10 | toRead.append(input()) 11 | 12 | for elem in toRead: 13 | if elem in library: 14 | print('YES') 15 | else: 16 | print('NO') 17 | -------------------------------------------------------------------------------- /sets_exam/hometask.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | m = int(input()) 3 | k = int(input()) 4 | p = int(input()) 5 | 6 | print(n+p-m-k) 7 | -------------------------------------------------------------------------------- /sets_exam/sets_exam_1.py: -------------------------------------------------------------------------------- 1 | set1 = {'a', 't', 'f', 'p'} 2 | set2 = {'a', 't', 'f'} 3 | 4 | print(set1 - set2) 5 | -------------------------------------------------------------------------------- /sets_exam/strange_infatuation.py: -------------------------------------------------------------------------------- 1 | arr1 = {int(elem) for elem in input().split()} 2 | arr2 = {int(elem) for elem in input().split()} 3 | 4 | if len(arr1.intersection(arr2)) == 0: 5 | print('BAD DAY') 6 | else: 7 | print(*sorted(arr1.intersection(arr2), reverse=True)) 8 | -------------------------------------------------------------------------------- /sets_exam/sunrise.py: -------------------------------------------------------------------------------- 1 | arr = [int(elem) for elem in input().split()] 2 | 3 | print(len(arr) - len(set(arr))) 4 | -------------------------------------------------------------------------------- /sets_exam/towns.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | arr = set() 3 | 4 | def is_repeat(arr, n): 5 | for i in range(n + 1): 6 | s = input() 7 | length = len(arr) 8 | arr.add(s) 9 | if length == len(arr): 10 | return 'REPEAT' 11 | return 'OK' 12 | 13 | print(is_repeat(arr, n)) 14 | -------------------------------------------------------------------------------- /tuples/competitive_selection.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | coords = [input().split() for i in range(n)] 4 | 5 | for elem in coords: 6 | print(*elem) 7 | 8 | print() 9 | 10 | coords = [i for i in coords if int(i[1]) > 3] 11 | 12 | for elem in coords: 13 | print(*elem) 14 | -------------------------------------------------------------------------------- /tuples/parabola_vertex.py: -------------------------------------------------------------------------------- 1 | coords = [int(input()) for i in range(3)] 2 | 3 | result = ( -1 * coords[1]/(2 * coords[0]), (4 * coords[0] * coords[2] - pow(coords[1],2)) / (4 * coords[0])) 4 | 5 | print(result) 6 | -------------------------------------------------------------------------------- /tuples/tribonacci.py: -------------------------------------------------------------------------------- 1 | def printFirstTribonacciNums(n): 2 | arr = [] 3 | for i in range(n): 4 | if i < 3: 5 | arr.append(1) 6 | else: 7 | arr.append(arr[i - 1] + arr[i - 2] + arr[i - 3]) 8 | print(*arr) 9 | 10 | n = int(input()) 11 | 12 | printFirstTribonacciNums(n) 13 | -------------------------------------------------------------------------------- /tuples/tuples_1.py: -------------------------------------------------------------------------------- 1 | countries = ('Russia', 'Argentina', 'Spain', 'Slovakia', 'Canada', 'Slovenia', 'Italy') 2 | last = countries[-1] 3 | print(last) 4 | -------------------------------------------------------------------------------- /tuples/tuples_10.py: -------------------------------------------------------------------------------- 1 | numbers1 = (1, 2, 3) 2 | numbers2 = (6,) 3 | numbers3 = (7, 8, 9, 10, 11, 12, 13) 4 | 5 | print(numbers1 * 2 + numbers2 * 9 + numbers3) 6 | -------------------------------------------------------------------------------- /tuples/tuples_11.py: -------------------------------------------------------------------------------- 1 | city_name = input() 2 | city_year = int(input()) 3 | city = tuple([city_name, city_year]) 4 | print(city) 5 | -------------------------------------------------------------------------------- /tuples/tuples_12.py: -------------------------------------------------------------------------------- 1 | tuples = [(), (), ('',), ('a', 'b'), (), ('a', 'b', 'c'), (1,), (), (), ('d',), ('', ''), ()] 2 | non_empty_tuples = [i for i in tuples if i != tuple()] 3 | 4 | print(non_empty_tuples) 5 | -------------------------------------------------------------------------------- /tuples/tuples_13.py: -------------------------------------------------------------------------------- 1 | tuples = [(10, 20, 40), (40, 50, 60), (70, 80, 90), (10, 90), (1, 2, 3, 4), (5, 6, 10, 2, 1, 77)] 2 | new_tuples = [i[:-1] + (100,) for i in tuples] 3 | print(new_tuples) 4 | -------------------------------------------------------------------------------- /tuples/tuples_14.py: -------------------------------------------------------------------------------- 1 | numbers = (2, 3, 5, 7, -11, 13, 17, 19, 23, 29, 31, -6, 41, 43, 47, 53, 59, 61, -96, 71, 1000, -1) 2 | 3 | multiply = 1 4 | 5 | for elem in numbers: 6 | multiply *= elem 7 | 8 | 9 | print(multiply) 10 | -------------------------------------------------------------------------------- /tuples/tuples_15.py: -------------------------------------------------------------------------------- 1 | data = 'Python для продвинутых!' 2 | 3 | print(tuple(data)) 4 | -------------------------------------------------------------------------------- /tuples/tuples_16.py: -------------------------------------------------------------------------------- 1 | poet_data = ('Пушкин', 1799, 'Санкт-Петербург') 2 | 3 | poet_data = poet_data[:-1] + ('Москва',) 4 | 5 | print(poet_data) 6 | -------------------------------------------------------------------------------- /tuples/tuples_17.py: -------------------------------------------------------------------------------- 1 | numbers = ((10, 10, 10, 12), (30, 45, 56, 45), (81, 80, 39, 32), (1, 2, 3, 4), (90, 10)) 2 | 3 | result = [] 4 | 5 | for tuples in numbers: 6 | average = 0 7 | for elem in tuples: 8 | average += elem 9 | result.append(average / len(tuples)) 10 | 11 | print(result) 12 | -------------------------------------------------------------------------------- /tuples/tuples_2.py: -------------------------------------------------------------------------------- 1 | primes = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71) 2 | print(primes[:6]) 3 | -------------------------------------------------------------------------------- /tuples/tuples_3.py: -------------------------------------------------------------------------------- 1 | countries = ('Russia', 'Argentina', 'Slovakia', 'Canada', 'Slovenia', 'Italy', 'Spain', 'Ukraine', 'Chile', 'Cameroon') 2 | print(countries[2:]) 3 | -------------------------------------------------------------------------------- /tuples/tuples_4.py: -------------------------------------------------------------------------------- 1 | countries = ('Russia', 'Argentina', 'Slovakia', 'Canada', 'Slovenia', 'Italy', 'Spain', 'Ukraine', 'Chile', 'Cameroon') 2 | print(countries[:-3]) 3 | -------------------------------------------------------------------------------- /tuples/tuples_5.py: -------------------------------------------------------------------------------- 1 | countries = ('Russia', 'Argentina', 'Slovakia', 'Canada', 'Slovenia', 'Italy', 'Spain', 'Ukraine', 'Chile', 'Cameroon') 2 | print(countries[3:-2]) 3 | -------------------------------------------------------------------------------- /tuples/tuples_6.py: -------------------------------------------------------------------------------- 1 | countries = ('Romania', 'Poland', 'Estonia', 'Bulgaria', 'Slovakia', 'Slovenia', 'Hungary') 2 | number = len(countries) 3 | print(number) 4 | -------------------------------------------------------------------------------- /tuples/tuples_7.py: -------------------------------------------------------------------------------- 1 | numbers = (12.5, 3.1415, 2.718, 9.8, 1.414, 1.1618, 1.324) 2 | 3 | print(max(numbers) + min(numbers)) 4 | -------------------------------------------------------------------------------- /tuples/tuples_8.py: -------------------------------------------------------------------------------- 1 | countries = ('Russia', 'Argentina', 'Spain', 'Slovakia', 'Canada', 'Slovenia', 'Italy') 2 | index = countries.index('Slovenia') 3 | print(index) 4 | -------------------------------------------------------------------------------- /tuples/tuples_9.py: -------------------------------------------------------------------------------- 1 | countries = ('Russia', 'Argentina', 'Spain', 'Slovakia', 'Canada', 'Slovenia', 'Italy', 'Spain', 'Ukraine', 'Chile', 'Spain', 'Cameroon') 2 | number = countries.count('Spain') 3 | print(number) 4 | -------------------------------------------------------------------------------- /work_with_files/christmas_present.py: -------------------------------------------------------------------------------- 1 | arr = [] 2 | 3 | with open('class_scores.txt') as file: 4 | for line in file.readlines(): 5 | arr.append(line.strip().split()[0] + ' ' + str(int(line.strip().split()[1]) + 5 if int(line.strip().split()[1]) < 95 else 100) + '\n') 6 | 7 | with open('new_scores.txt', 'w') as file: 8 | file.writelines(arr) 9 | -------------------------------------------------------------------------------- /work_with_files/csv_files.py: -------------------------------------------------------------------------------- 1 | def read_csv(): 2 | needWriteKeys = True 3 | keys = [] 4 | result = [] 5 | 6 | with open('data.csv') as file: 7 | for line in file.readlines(): 8 | if needWriteKeys: 9 | keys = line.strip().split(',') 10 | needWriteKeys = False 11 | else: 12 | result.append(dict(zip(keys, line.strip().split(',')))) 13 | return result 14 | -------------------------------------------------------------------------------- /work_with_files/file_concat.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | result = '' 3 | 4 | for i in range(n): 5 | with open(input()) as file: 6 | result += file.read() 7 | 8 | with open('output.txt', 'w') as file: 9 | file.write(result) 10 | -------------------------------------------------------------------------------- /work_with_files/file_content.py: -------------------------------------------------------------------------------- 1 | file = open(input()) 2 | 3 | print(file.read()) 4 | -------------------------------------------------------------------------------- /work_with_files/file_statistics.py: -------------------------------------------------------------------------------- 1 | linesNum = 0 2 | wordsNum = 0 3 | lettersNum = 0 4 | 5 | with open('file.txt') as file: 6 | for line in file.readlines(): 7 | linesNum += 1 8 | wordsNum += len(line.split()) 9 | lettersNum += len([elem for elem in line if elem.isalpha()]) 10 | 11 | print("Input file contains:\n" + str(lettersNum) + " letters\n" + str(wordsNum) + " words\n" + str(linesNum) + " lines") 12 | -------------------------------------------------------------------------------- /work_with_files/flip_line.py: -------------------------------------------------------------------------------- 1 | with open('text.txt') as file: 2 | line = file.read() 3 | print(line[::-1]) 4 | -------------------------------------------------------------------------------- /work_with_files/input_string.py: -------------------------------------------------------------------------------- 1 | with open('output.txt', 'w') as output: 2 | output.write(input()) 3 | -------------------------------------------------------------------------------- /work_with_files/jacque_fresco_task.py: -------------------------------------------------------------------------------- 1 | colours = dict() 2 | totalAmount = 0 3 | readColoursOn = False 4 | readGoatsOn = False 5 | result = [] 6 | 7 | with open('goats.txt') as file: 8 | for line in file.readlines(): 9 | if line == 'COLOURS\n': 10 | readColoursOn = True 11 | continue 12 | if line == 'GOATS\n': 13 | readColoursOn = False 14 | readGoatsOn = True 15 | continue 16 | 17 | if readColoursOn: 18 | colours[line.strip()] = 0 19 | if readGoatsOn: 20 | colours[line.strip()] += 1 21 | totalAmount += 1 22 | 23 | for (key, value)in colours.items(): 24 | if value/totalAmount > 0.07: 25 | result.append(key + '\n') 26 | 27 | with open('answer.txt', 'w') as file: 28 | file.writelines(sorted(result)) 29 | -------------------------------------------------------------------------------- /work_with_files/line_numbering.py: -------------------------------------------------------------------------------- 1 | arr = [] 2 | counter = 1 3 | 4 | with open('input.txt') as file: 5 | for line in file.readlines(): 6 | arr.append(str(counter) + ') ' + line) 7 | counter += 1 8 | 9 | with open('output.txt', 'w') as file: 10 | file.writelines(arr) 11 | -------------------------------------------------------------------------------- /work_with_files/log_file.py: -------------------------------------------------------------------------------- 1 | import io 2 | 3 | result = [] 4 | 5 | with io.open('logfile.txt', encoding='utf-8') as file: 6 | for line in file: 7 | name = line.strip().split(', ')[0] 8 | startTime = line.strip().split(', ')[1] 9 | endTime = line.strip().split(', ')[2] 10 | if int(endTime.split(':')[0]) - int(startTime.split(':')[0]) > 1: 11 | result.append(name + '\n') 12 | if int(endTime.split(':')[0]) - int(startTime.split(':')[0]) == 1 and int(endTime.split(':')[1]) - int(startTime.split(':')[1]) >= 0: 13 | result.append(name + '\n') 14 | 15 | with open('output.txt', 'w') as file: 16 | file.writelines(result) 17 | -------------------------------------------------------------------------------- /work_with_files/long_lines.py: -------------------------------------------------------------------------------- 1 | max_len = 0 2 | max_str = [] 3 | 4 | with open('lines.txt') as file: 5 | for line in file.readlines(): 6 | if len(line) > max_len: 7 | max_len = len(line) 8 | max_str = [line] 9 | elif len(line) == max_len: 10 | max_str.append(line) 11 | 12 | for elem in max_str: 13 | print(elem, end = '') 14 | -------------------------------------------------------------------------------- /work_with_files/penultimate_line.py: -------------------------------------------------------------------------------- 1 | file = open(input()) 2 | 3 | print(file.readlines()[-2]) 4 | 5 | file.close() 6 | -------------------------------------------------------------------------------- /work_with_files/random_name_surname.py: -------------------------------------------------------------------------------- 1 | from random import randint 2 | 3 | firstNames = [] 4 | lastNames = [] 5 | 6 | with open('first_names.txt') as file: 7 | for line in file.readlines(): 8 | firstNames.append(line) 9 | 10 | with open('last_names.txt') as file: 11 | for line in file.readlines(): 12 | lastNames.append(line) 13 | 14 | for i in range(3): 15 | print(firstNames[randint(0, len(firstNames))].strip() + ' ' + lastNames[randint(0, len(lastNames))].strip()) 16 | -------------------------------------------------------------------------------- /work_with_files/random_nums.py: -------------------------------------------------------------------------------- 1 | from random import randint 2 | 3 | with open('random.txt', 'w') as file: 4 | arr = [str(randint(111, 777)) + '\n' for i in range(25)] 5 | file.writelines(arr) 6 | -------------------------------------------------------------------------------- /work_with_files/random_string.py: -------------------------------------------------------------------------------- 1 | file = open('lines.txt') 2 | 3 | print(file.readlines()[0]) 4 | 5 | file.close() 6 | -------------------------------------------------------------------------------- /work_with_files/reverse_order.py: -------------------------------------------------------------------------------- 1 | with open('data.txt') as file: 2 | arr = file.readlines() 3 | for i in range(len(arr)): 4 | print(arr[len(arr) - 1 - i].strip()) 5 | -------------------------------------------------------------------------------- /work_with_files/sum_of_nums_in_file.py: -------------------------------------------------------------------------------- 1 | summa = 0 2 | 3 | with open('nums.txt') as file: 4 | for line in file.readlines(): 5 | result_line = '' 6 | for elem in line: 7 | result_line += elem if elem.isdigit() else ' ' 8 | summa += sum([int(elem) for elem in result_line.split()]) 9 | print(summa) 10 | -------------------------------------------------------------------------------- /work_with_files/sum_of_nums_in_lines.py: -------------------------------------------------------------------------------- 1 | with open('numbers.txt') as file: 2 | for line in file.readlines(): 3 | print(sum([int(elem) for elem in line.split()])) 4 | -------------------------------------------------------------------------------- /work_with_files/sum_of_two_1.py: -------------------------------------------------------------------------------- 1 | file = open('numbers.txt') 2 | summa = 0 3 | 4 | for line in file.readlines(): 5 | summa += int(line) 6 | 7 | print(summa) 8 | file.close() 9 | -------------------------------------------------------------------------------- /work_with_files/sum_of_two_2.py: -------------------------------------------------------------------------------- 1 | file = open('nums.txt') 2 | summa = 0 3 | 4 | for line in file.read().split(): 5 | summa += int(line) 6 | 7 | print(summa) 8 | file.close() 9 | -------------------------------------------------------------------------------- /work_with_files/total_cost.py: -------------------------------------------------------------------------------- 1 | file = open('prices.txt') 2 | summa = 0 3 | 4 | for line in file.readlines(): 5 | arr = line.split() 6 | summa += int(arr[1]) * int(arr[2]) 7 | 8 | print(summa) 9 | file.close() 10 | -------------------------------------------------------------------------------- /work_with_files/unusual_countries.py: -------------------------------------------------------------------------------- 1 | from random import randint 2 | 3 | with open('population.txt') as file: 4 | for line in file.readlines(): 5 | if (line.startswith('G') and int(line.strip().split()[-1]) > 500000): 6 | print(line.strip().split()[0]) 7 | -------------------------------------------------------------------------------- /work_with_files_exam/forbidden_words.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | with open('forbidden_words.txt') as file: 4 | forbidden_words = file.read().strip().split() 5 | 6 | with open(input()) as file: 7 | for line in file.readlines(): 8 | for elem in forbidden_words: 9 | if elem in line.strip().lower(): 10 | insensitive_hippo = re.compile(re.escape(elem), re.IGNORECASE) 11 | line = insensitive_hippo.sub('*'*len(elem), line).strip() 12 | print(line.strip()) 13 | -------------------------------------------------------------------------------- /work_with_files_exam/good_students.py: -------------------------------------------------------------------------------- 1 | counter = 0 2 | 3 | with open('grades.txt') as file: 4 | for line in file.readlines(): 5 | arr = [int(elem) for elem in line.strip().split()[1:]] 6 | if (arr[0] >= 65 and arr[1] >= 65 and arr[2] >= 65): 7 | counter += 1 8 | 9 | print(counter) 10 | -------------------------------------------------------------------------------- /work_with_files_exam/missed_comments.py: -------------------------------------------------------------------------------- 1 | functions = [] 2 | 3 | with open(input()) as file: 4 | arr = file.readlines() 5 | for i in range(len(arr)): 6 | if 'def ' in arr[i]: 7 | if i == 0: 8 | functions.append(arr[i].strip().split()[1].split('(')[0]) 9 | continue 10 | if not arr[i-1].startswith('#'): 11 | functions.append(arr[i].strip().split()[1].split('(')[0]) 12 | 13 | if len(functions) > 0: 14 | for elem in functions: 15 | print(elem) 16 | else: 17 | print('Best Programming Team') 18 | -------------------------------------------------------------------------------- /work_with_files_exam/num_of_lines_in_file.py: -------------------------------------------------------------------------------- 1 | with open(input()) as file: 2 | print(len(file.readlines())) 3 | -------------------------------------------------------------------------------- /work_with_files_exam/sum_cost.py: -------------------------------------------------------------------------------- 1 | summa = 0 2 | 3 | with open('ledger.txt') as file: 4 | for line in file.readlines(): 5 | summa += int(line.strip()[1:]) 6 | 7 | print('$' + str(summa)) 8 | -------------------------------------------------------------------------------- /work_with_files_exam/tail_of_a_file.py: -------------------------------------------------------------------------------- 1 | with open(input()) as file: 2 | if len(file.readlines()) < 10: 3 | file.seek(0) 4 | for line in file.readlines(): 5 | print(line.strip()) 6 | else: 7 | file.seek(0) 8 | for line in file.readlines()[-10:]: 9 | print(line.strip()) 10 | -------------------------------------------------------------------------------- /work_with_files_exam/the_longest_word_in_file.py: -------------------------------------------------------------------------------- 1 | with open('words.txt') as file: 2 | line = file.read().strip().split() 3 | 4 | for elem in list(filter(lambda x: len(x) == len(sorted(line, key = lambda x: len(x), reverse=True)[0]), line)): 5 | print(elem) 6 | -------------------------------------------------------------------------------- /work_with_files_exam/transliteration.py: -------------------------------------------------------------------------------- 1 | import io 2 | 3 | translate_dict = { 4 | 'а': 'a', 'к': 'k', 'х': 'h', 'б': 'b', 'л': 'l', 'ц': 'c', 'в': 'v', 'м': 'm', 'ч': 'ch', 5 | 'г': 'g', 'н': 'n', 'ш': 'sh', 'д': 'd', 'о': 'o', 'щ': 'shh', 'е': 'e', 'п': 'p', 'ъ': '*', 6 | 'ё': 'jo', 'р': 'r', 'ы': 'y', 'ж': 'zh', 'с': 's', 'ь': "'", 'з': 'z', 'т': 't', 'э': 'je', 7 | 'и': 'i', 'у': 'u', 'ю': 'ju', 'й': 'j', 'ф': 'f', 'я': 'ya', 8 | 'А': 'A', 'К': 'K', 'Х': 'H', 'Б': 'B', 'Л': 'L', 'Ц': 'C', 'В': 'V', 'М': 'M', 'Ч': 'Ch', 9 | 'Г': 'G', 'Н': 'N', 'Ш': 'Sh', 'Д': 'D', 'О': 'O', 'Щ': 'Shh', 'Е': 'E', 'П': 'P', 'Ъ': '*', 10 | 'Ё': 'Jo', 'Р': 'R', 'Ы': 'Y', 'Ж': 'Zh', 'С': 'S', 'Ь': "'", 'З': 'Z', 'Т': 'T', 'Э': 'Je', 11 | 'И': 'I', 'У': 'U', 'Ю': 'Ju', 'Й': 'J', 'Ф': 'F', 'Я': 'Ya', 12 | } 13 | 14 | result = [] 15 | counter = 0 16 | 17 | with io.open('cyrillic.txt', encoding='utf-8') as file: 18 | for line in file.readlines(): 19 | result.append('') 20 | for elem in line.strip(): 21 | if elem in translate_dict: 22 | result[counter] += translate_dict[elem] 23 | else: 24 | result[counter] += elem 25 | result[counter] += '\n' 26 | counter += 1 27 | 28 | with open('transliteration.txt', 'w') as file: 29 | file.writelines(result) 30 | --------------------------------------------------------------------------------