GeorgiLukanov87

All Dictionaries - Exercise Python Fundamentals 100/100

Jul 6th, 2022 (edited)
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.69 KB | None | 0 0
  1. # Dictionaries - Exercise Python Fundamentals 100/100
  2. # https://judge.softuni.org/Contests/Compete/Index/1737#0
  3.  
  4. *************************************************************************************************
  5.  
  6. # 01. Count Chars in a String
  7.  
  8. data = input()
  9. new_data = data.replace(" ", "")
  10. data = new_data
  11. my_dict = {}
  12.  
  13. for word in data:
  14.     for letter in word:
  15.         if letter not in my_dict:
  16.             my_dict[letter] = 1
  17.         else:
  18.             my_dict[letter] += 1
  19.  
  20. for el in my_dict:
  21.     print(f'{el} -> {my_dict[el]}')
  22.  
  23. *************************************************************************************************
  24.  
  25. # 02. A Miner Task
  26.  
  27. data = input()
  28.  
  29. miner = {}
  30.  
  31. while not data == 'stop':
  32.     resource = data
  33.     count = int(input())
  34.     if resource not in miner:
  35.         miner[resource] = 0
  36.     miner[resource] += count
  37.  
  38.     data = input()
  39.  
  40. for el in miner:
  41.     print(f'{el} -> {miner[el]}')
  42.  
  43.  
  44. *************************************************************************************************
  45.  
  46. # 03. Capitals
  47.  
  48. # 1. zip
  49.  
  50. country = input().split(', ')
  51. capitals = input().split(', ')
  52.  
  53. my_dict = {}
  54.  
  55. my_dict = dict(zip(country, capitals))
  56.  
  57. for el in my_dict:
  58.     print(f"{el} -> {my_dict[el]}")
  59.  
  60. #2. without zip
  61.  
  62. country = input().split(', ')
  63. capitals = input().split(', ')
  64.  
  65. for index, el in enumerate(country):
  66.     my_dict = {}
  67.     my_dict[el] = capitals[index]
  68.     print(f"{el} -> {my_dict[el]}")
  69.  
  70. *************************************************************************************************
  71.  
  72. # 04. Phonebook
  73.  
  74. data = input()
  75. contacts = {}
  76.  
  77. while not len(data) == 1:
  78.     data = data.split("-")
  79.     key = data[0]
  80.     value = data[1]
  81.     if key not in contacts:
  82.         contacts[key] = value
  83.     else:
  84.         contacts[key] = value
  85.  
  86.     data = input()
  87.  
  88. n = int(data)
  89.  
  90. for name in range(n):
  91.     searched_name = input()
  92.     if searched_name in contacts:
  93.         print(f'{searched_name} -> {contacts[searched_name]}')
  94.     else:
  95.         print(f"Contact {searched_name} does not exist.")
  96.  
  97. *************************************************************************************************
  98.  
  99. # 05. Legendary Farming
  100.  
  101. materials = input()
  102.  
  103. legendary = {'shards': 'Shadowmourne', 'fragments': 'Valanyr', 'motes': 'Dragonwrath'}
  104. key_materials = {'shards': 0, 'fragments': 0, 'motes': 0}
  105. junk = {}
  106. obtained = False
  107. final_print = ""
  108.  
  109. while not obtained:
  110.     details = materials.split(' ')
  111.     for index in range(0, len(details), 2):
  112.         amount = int(details[index])
  113.         material = details[index + 1].lower()
  114.         if material in legendary.keys():
  115.             key_materials[material] += amount
  116.             if key_materials[material] >= 250:
  117.                 key_materials[material] -= 250
  118.                 final_print = legendary[material]
  119.                 obtained = True
  120.                 print(f'{final_print} obtained!')
  121.                 break
  122.         else:
  123.             if material not in junk.keys():
  124.                 junk[material] = 0
  125.             junk[material] += amount
  126.  
  127.     if not obtained:
  128.         materials = input()
  129.  
  130. for key, value in key_materials.items():
  131.     print(f'{key}: {value}')
  132. for key, value in junk.items():
  133.     print(f'{key}: {value}')
  134.  
  135. *************************************************************************************************
  136.  
  137. # 06. Orders
  138.  
  139. data = input()
  140. products = {}
  141. while not data == 'buy':
  142.     data = data.split()
  143.     key = data[0]
  144.     amount = data[1]
  145.     value = data[2]
  146.  
  147.     if key not in products:
  148.         products[key] = []
  149.     else:
  150.         if products[key][0] != amount:
  151.             products[key][0] = amount
  152.         products[key][1] = float(value) + float(products[key][1])
  153.     if amount not in products[key]:
  154.         products[key].append(amount)
  155.     if value not in products[key]:
  156.         products[key].append(value)
  157.  
  158.     data = input()
  159.  
  160. for el in products:
  161.     print(f'{el} -> {float(products[el][0]) * float(products[el][1]):.2f}')
  162.  
  163. *************************************************************************************************
  164.  
  165. # 07. SoftUni Parking
  166.  
  167. n = int(input())
  168. registry = {}
  169. for client in range(n):
  170.     data = input()
  171.     data = data.split()
  172.     action = data[0]
  173.  
  174.     if action == 'register':
  175.         name = data[1]
  176.         code = data[2]
  177.         if name not in registry:
  178.             registry[name] = code
  179.             print(f"{name} registered {code} successfully")
  180.         else:
  181.             print(f"ERROR: already registered with plate number {code}")
  182.  
  183.     elif action == 'unregister':
  184.         name = data[1]
  185.         if name not in registry:
  186.             print(f"ERROR: user {name} not found")
  187.         else:
  188.             print(f"{name} unregistered successfully")
  189.             del registry[name]
  190.  
  191. for el in registry:
  192.     print(f'{el} => {registry[el]}')
  193.  
  194. *************************************************************************************************
  195.  
  196. # 08. Courses
  197.  
  198. data = input()
  199.  
  200. courses_dict = {}
  201.  
  202. while not data == 'end':
  203.     data = data.split(' : ')
  204.     course = data[0]
  205.     name = data[1]
  206.     if course not in courses_dict:
  207.         courses_dict[course] = [name]
  208.     else:
  209.         courses_dict[course].append(name)
  210.  
  211.     data = input()
  212.  
  213. for el in courses_dict:
  214.     print(f'{el}: {len(courses_dict[el])}')
  215.     for name in courses_dict[el]:
  216.         print(f'-- {name}')
  217.  
  218. *************************************************************************************************
  219.  
  220. # 09. Student Academy
  221.  
  222. n = int(input())
  223.  
  224. students = {}
  225.  
  226. for _ in range(n):
  227.     name = input()
  228.     grade = float(input())
  229.  
  230.     if name not in students:
  231.         students[name] = grade
  232.     else:
  233.         students[name] += grade
  234.         students[name] /= 2
  235.  
  236. for name in students:
  237.     if students[name] >= 4.50:
  238.         print(f'{name} -> {students[name]:.2f}')
  239.  
  240. *************************************************************************************************
  241.  
  242. # 10. Company Users
  243.  
  244. data = input()
  245. company = {}
  246.  
  247. while not data == 'End':
  248.     data = data.split(" -> ")
  249.     name = data[0]
  250.     id = data[1]
  251.  
  252.     if name not in company:
  253.         company[name] = [id]
  254.     else:
  255.         if id not in company[name]:
  256.             company[name].append(id)
  257.  
  258.     data = input()
  259.  
  260. for el in company:
  261.     print(f'{el}')
  262.     for name in company[el]:
  263.         print(f'-- {name}')
  264.  
  265. *************************************************************************************************
  266.  
  267. # 11. Force Book *
  268.  
  269. def force_user_exists(force_book_dict: dict, user: str):
  270.     for user_list in force_book_dict.values():
  271.         if user in user_list:
  272.             return True
  273.     return False
  274.  
  275.  
  276. def remove_user_from_side(force_book_dict: dict, user: str):
  277.     for (side, users) in force_book_dict.items():
  278.         if user in users:
  279.             force_book_dict[side].remove(user)
  280.  
  281.  
  282. def initial_join_force_side(force_book_dict: dict, side: str, user: str):
  283.     if side not in force_book_dict.keys() and not force_user_exists(force_book_dict, user):
  284.         force_book_dict[side] = []
  285.         force_book_dict[side].append(user)
  286.        
  287.     elif not force_user_exists(force_book_dict, user):
  288.         force_book_dict[side].append(user)
  289.  
  290.  
  291. def join_force_side(force_book_dict: dict, side: str, user: str):
  292.     if side not in force_book_dict.keys() and not force_user_exists(force_book_dict, user):
  293.         force_book_dict[side] = []
  294.         force_book_dict[side].append(user)
  295.        
  296.     elif not force_user_exists(force_book_dict, user):
  297.         if side not in force_book_dict.keys():
  298.             force_book_dict[side] = []
  299.         force_book_dict[side].append(user)
  300.        
  301.     elif force_user_exists(force_book_dict, user):
  302.         remove_user_from_side(force_book_dict, user)
  303.  
  304.         if side not in force_book_dict.keys():
  305.             force_book_dict[side] = []
  306.  
  307.         force_book_dict[side].append(user)
  308.  
  309.     print(f'{user} joins the {side} side!')
  310.  
  311.  
  312. force_book = {}
  313.  
  314. command = input()
  315. while command != 'Lumpawaroo':
  316.     if '|' in command:
  317.         details = command.split(' | ')
  318.         force_side = details[0]
  319.         force_user = details[1]
  320.  
  321.         initial_join_force_side(force_book, force_side, force_user)
  322.        
  323.     elif '->' in command:
  324.         details = command.split(' -> ')
  325.         force_user = details[0]
  326.         force_side = details[1]
  327.  
  328.         join_force_side(force_book, force_side, force_user)
  329.  
  330.     command = input()
  331.  
  332. for key, value in force_book.items():
  333.     if len(value) > 0:
  334.         print(f'Side: {key}, Members: {len(value)}')
  335.         for el in value:
  336.             print(f"! {el}")
  337.  
  338. *************************************************************************************************
  339.  
  340. # 12. SoftUni Exam Results *
  341.  
  342. command = input()
  343. data = {}
  344. all_languages = []
  345. count_languages = []
  346.  
  347. while not command == 'exam finished':
  348.     command = command.split("-")
  349.     if command[1] == "banned":
  350.         name = command[0]
  351.         del data[name]
  352.         command = input()
  353.         continue
  354.     elif not command[1] == 'banned':
  355.         name = command[0]
  356.         language = command[1]
  357.         points = int(command[2])
  358.     if name not in data:
  359.         data[name] = points
  360.     else:
  361.         if points > data[name]:
  362.             data[name] = points
  363.     all_languages += [language]
  364.    
  365.     if language not in count_languages:
  366.         count_languages.append(language)
  367.  
  368.     command = input()
  369.  
  370. print('Results:')
  371. for name, points in data.items():
  372.     print(f'{name} | {points}')
  373.  
  374. print('Submissions:')
  375. for subm in range(len(count_languages)):
  376.     result = count_languages[subm]
  377.     print(f"{count_languages[subm]} - {all_languages.count(result)}")
  378.  
  379. *************************************************************************************************
  380.  
  381.  
Add Comment
Please, Sign In to add comment