Advertisement
GeorgiLukanov87

Python-Advanced-Exercises Tuples and Sets - Exercise

Aug 18th, 2022
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.44 KB | None | 0 0
  1. # Python-Advanced-Exercises Tuples and Sets - Exercise
  2. # https://judge.softuni.org/Contests/Practice/Index/1833#0
  3.  
  4. # 01. Unique Usernames
  5. # 02. Sets of Elements
  6. # 03. Periodic Table
  7. # 04. Count Symbols
  8. # 05. Longest Intersection
  9. # 06. Battle of Names
  10.  
  11. ====================================================================================================
  12. # 01. Unique Usernames
  13.  
  14. n = int(input())
  15.  
  16. name_collection = set()
  17. for _ in range(n):
  18.     name = input()
  19.     name_collection.add(name)
  20.  
  21. print(*name_collection, sep='\n')
  22.  
  23.  
  24. ====================================================================================================
  25. # 02. Sets of Elements
  26.  
  27. data = input().split(' ')
  28. n = int(data[0])
  29. m = int(data[1])
  30.  
  31. n_set = set()
  32. m_set = set()
  33.  
  34. for _ in range(n):
  35.     number = int(input())
  36.     n_set.add(number)
  37.  
  38. for _ in range(m):
  39.     number = int(input())
  40.     m_set.add(number)
  41.  
  42. result = n_set.intersection(m_set)
  43. print(*result, sep='\n')
  44.  
  45.  
  46. ====================================================================================================
  47. # 03. Periodic Table
  48.  
  49. n = int(input())
  50. collection = set()
  51. for _ in range(n):
  52.     element = input().split(' ')
  53.     for sub_el in element:
  54.         collection.add(sub_el)
  55.  
  56. print(*collection, sep='\n')
  57.  
  58.  
  59. ====================================================================================================
  60. # 04. Count Symbols
  61.  
  62. text = input()
  63.  
  64. letter_dict = {}
  65.  
  66. for letter in text:
  67.     if letter not in letter_dict:
  68.         letter_dict[letter] = 0
  69.     letter_dict[letter] += 1
  70.  
  71. for data in sorted(letter_dict.items()):
  72.     print(f'{data[0]}: {data[1]} time/s')
  73.    
  74.    
  75. ====================================================================================================
  76. # 05. Longest Intersection
  77.  
  78. n = int(input())
  79. intersections = []
  80. for _ in range(n):
  81.     data = input().split('-')
  82.     data1 = data[0].split(',')
  83.     data2 = data[1].split(',')
  84.  
  85.     first_start = int(data1[0])
  86.     first_end = int(data1[1])
  87.  
  88.     second_start = int(data2[0])
  89.     second_end = int(data2[1])
  90.     set1 = set()
  91.     set2 = set()
  92.  
  93.     for num in range(first_start, first_end + 1):
  94.         set1.add(num)
  95.  
  96.     for num in range(second_start, second_end + 1):
  97.         set2.add(num)
  98.  
  99.     result = set1.intersection(set2)
  100.     intersections.append(result)
  101.  
  102. max_len = 0
  103. position = -1
  104. for index, el in enumerate(intersections):
  105.     if len(el) > max_len:
  106.         max_len = len(el)
  107.         position = index
  108.  
  109. longest_intersection = list(sorted(intersections[position]))
  110.  
  111. print(f'Longest intersection is [{", ".join([str(x) for x in longest_intersection])}] with length {max_len}')
  112.  
  113.  
  114. ====================================================================================================
  115. # 06. Battle of Names
  116.  
  117.  
  118. n = int(input())
  119.  
  120. odd_set = set()
  121. even_set = set()
  122.  
  123. for row in range(1, n + 1):
  124.     name = input()
  125.     ascii_sum = 0
  126.     for ch in name:
  127.         ascii_sum += ord(ch)
  128.     ascii_sum = ascii_sum / row
  129.     if not int(ascii_sum) % 2 == 0:
  130.         odd_set.add(int(ascii_sum))
  131.     else:
  132.         even_set.add(int(ascii_sum))
  133.  
  134. odd_sum = sum(odd_set)
  135. even_sum = sum(even_set)
  136.  
  137. if odd_sum == even_sum:
  138.     result = odd_set | even_set
  139.  
  140. elif odd_sum > even_sum:
  141.     result = odd_set - even_set
  142.  
  143. elif even_sum > odd_sum:
  144.     result = even_set ^ odd_set
  145.  
  146. print(*result, sep=', ')
  147.  
  148.  
  149. ====================================================================================================
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement