Advertisement
JmihPodvalbniy

Untitled

May 8th, 2024
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.17 KB | Software | 0 0
  1. 1)
  2. from collections import defaultdict
  3.  
  4. num_records = int(input("Введите число строк: "))
  5. votes = [input("Введите имена в алф. порядке и число голосов: ").split() for _ in range(num_records)]
  6.  
  7. results = defaultdict(int)
  8. for candidate, count in votes:
  9.   results[candidate] += int(count)
  10.  
  11. for candidate in sorted(results):
  12.   print(f"{candidate} {results[candidate]}")
  13. 2)
  14. strings = int(input("Ввести кол-во строк: "))
  15. text = " ".join(input("Ввести строки(у): ") for i in range(strings)).lower()
  16.  
  17. word_counts = {}
  18. for word in text.split():
  19.   if word in word_counts:
  20.     word_counts[word] += 1
  21.   else:
  22.     word_counts[word] = 1
  23.  
  24.  
  25. most_frequent_word = min(word_counts, key = lambda word: (-word_counts[word], word))
  26.  
  27. print(most_frequent_word)
  28. 3)
  29. n = int(input("Ввести число файлов: "))
  30. file_permissions = {}
  31.  
  32. for i in range(n):
  33.     file, *permissions = input("Ввести файл и операцию через пробел: ").split()
  34.     file_permissions[file] = set(permissions)
  35.  
  36. m = int(input("Ввести кол-во запросов: "))
  37.  
  38. for i in range(m):
  39.     operation, file = input("Ввести операцию и файл через пробел: ").split()
  40.     if operation in file_permissions.get(file, set()):
  41.         print("OK")
  42.     else:
  43.         print("Access denied")
  44.  
  45. sort_words()
  46. 4)
  47. def sort_words():
  48.     num_lines = int(input("Ввести кол-во строк: "))
  49.     text = []
  50.     for _ in range(num_lines):
  51.         text.extend(input("Ввести строки: ").lower().split())
  52.  
  53.     word_counts = {}
  54.     for word in text:
  55.         word_counts[word] = word_counts.get(word, 0) + 1
  56.  
  57.     sorted_words = sorted(word_counts.items(), key=lambda item: (-item[1], item[0]))
  58.  
  59.     for word, _ in sorted_words:
  60.         print(word)
  61.  
  62. sort_words()
  63. 5)
  64. def find_cities():
  65.     num_countries = int(input("Ввести кол-во стран: "))
  66.     country_cities = {}
  67.     for _ in range(num_countries):
  68.         country, *cities = input("Ввести в ключ: города, в значение: страну ").split()
  69.         for city in cities:
  70.             country_cities[city] = country
  71.  
  72.     num_queries = int(input("Ввести кол-во запросов: "))
  73.     for _ in range(num_queries):
  74.         city = input("Ввести город: ")
  75.         print(country_cities.get(city, "Страны нет в перечне"))
  76.  
  77. find_cities()
  78. 6)
  79. def create_latin_english_dict():
  80.     num_english_words = int(input("Ввести кол-во слов в словаре: "))
  81.     english_to_latin = {}
  82.     for _ in range(num_english_words):
  83.         english_word, latin_translations = input("Ввести слова из словаря: ").split(" - ")
  84.         latin_translations = latin_translations.split(", ")
  85.         for latin_word in latin_translations:
  86.             english_to_latin.setdefault(latin_word, []).append(english_word)
  87.  
  88.     latin_to_english = {}
  89.     for latin_word, english_words in english_to_latin.items():
  90.         latin_to_english[latin_word] = sorted(set(english_words))
  91.  
  92.     for latin_word in sorted(latin_to_english.keys()):
  93.         english_translations = ", ".join(latin_to_english[latin_word])
  94.         print(f"{latin_word} - {english_translations}")
  95.  
  96. create_latin_english_dict()
  97. 7)
  98. ---------------
  99. 8)
  100. ---------------
  101. 9)
  102. def determine_heights():
  103.     num_people = int(input("Ввести число элементов в генеалогическом древе: "))
  104.     parent_child = {}
  105.     for _ in range(num_people - 1):
  106.         child, parent = input("Ввести имя ребёнка и имя родителя через пробел: ").split()
  107.         parent_child[child] = parent
  108.  
  109.     heights = {}
  110.     def calculate_height(person):
  111.         if person not in heights:
  112.             if person not in parent_child:
  113.                 heights[person] = 0
  114.             else:
  115.                 heights[person] = calculate_height(parent_child[person]) + 1
  116.         return heights[person]
  117.  
  118.     for person in sorted(parent_child.keys()):
  119.         print(person, calculate_height(person))
  120.  
  121. determine_heights()
  122.  
  123. 10)
  124. ------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement