Advertisement
GeorgiLukanov87

01. Programming Fundamentals Final Exam Retake With FUNCTIONS ! 300/300

Jul 28th, 2022 (edited)
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.20 KB | None | 0 0
  1. # 01. Programming Fundamentals Final Exam Retake With FUNCTIONS ! 300/300
  2. # https://judge.softuni.org/Contests/Practice/Index/2525#0
  3.  
  4. ## 01. The Imitation Game
  5. ## 02. Ad Astra
  6. ## 03. The Pianist
  7.  
  8. =======================================================================================================
  9.  
  10. # 01. The Imitation Game
  11.  
  12.  
  13. def change_all_func(message, substring, replacement):
  14.     if substring in message:
  15.         message = message.replace(substring, replacement)
  16.     return message
  17.  
  18.  
  19. def insert_func(message, index, value):
  20.     message = message[:index] + value + message[index:]
  21.     return message
  22.  
  23.  
  24. def move_func(message, num_of_letters):
  25.     message = message[num_of_letters:] + message[:num_of_letters]
  26.     return message
  27.  
  28.  
  29. message = input()
  30. command = input()
  31.  
  32. while not command == 'Decode':
  33.     details = command.split('|')
  34.     if details[0] == 'ChangeAll':
  35.         substring = details[1]
  36.         replacement = details[2]
  37.         message = change_all_func(message, substring, replacement)
  38.  
  39.     elif details[0] == 'Insert':
  40.         index = int(details[1])
  41.         value = details[2]
  42.         message = insert_func(message, index, value)
  43.  
  44.     elif details[0] == 'Move':
  45.         num_of_letters = int(details[1])
  46.         message = move_func(message, num_of_letters)
  47.  
  48.     command = input()
  49.  
  50. print(f'The decrypted message is: {message}')
  51.  
  52.  
  53. =======================================================================================================
  54.  
  55. # 02. Ad Astra
  56.  
  57.  
  58. def extract_datas(some_matches):
  59.     my_data_list = []
  60.     calories_sum = []
  61.     for information in some_matches:
  62.         string_to_print = f"Item: {information[1]}, Best before: {information[2]}, Nutrition: {information[3]}"
  63.         my_data_list.append(string_to_print)
  64.         calories_sum.append(int(information[3]))
  65.     return my_data_list, sum(calories_sum) // 2000
  66.  
  67.  
  68. def final_print(some_data, some_days):
  69.     print(f'You have food to last you for: {some_days} days!')
  70.     if some_data:
  71.         for datas in some_data:
  72.             print(datas)
  73.  
  74.  
  75. import re
  76.  
  77. data = input()
  78. pattern = r'(\#|\|)([A-Za-z\s*]+)\1(\d{2}\/\d{2}\/\d{2})\1(\d{1,5})\1'
  79. matched = re.findall(pattern, data)
  80. extracted_datas, days = extract_datas(matched)
  81. final_print(extracted_datas, days)
  82.  
  83.  
  84. =======================================================================================================
  85.  
  86. # 03. The Pianist
  87.  
  88.  
  89. def add_func(some_datas):
  90.     piece = some_datas[1]
  91.     composer = some_datas[2]
  92.     key = some_datas[3]
  93.     if piece not in collection.keys():
  94.         collection[piece] = {'composer': composer, 'key': key}
  95.         print(f"{piece} by {composer} in {key} added to the collection!")
  96.     else:
  97.         print(f'{piece} is already in the collection!')
  98.  
  99.  
  100. def remove_func(some_datas):
  101.     piece = some_datas[1]
  102.     if piece in collection.keys():
  103.         del collection[piece]
  104.         print(f"Successfully removed {piece}!")
  105.     else:
  106.         print(f"Invalid operation! {piece} does not exist in the collection.")
  107.  
  108.  
  109. def change_key_func(some_datas):
  110.     piece = some_datas[1]
  111.     new_key = some_datas[2]
  112.     if piece in collection.keys():
  113.         collection[piece]["key"] = new_key
  114.         print(f"Changed the key of {piece} to {new_key}!")
  115.     else:
  116.         print(f"Invalid operation! {piece} does not exist in the collection.")
  117.  
  118.  
  119. n = int(input())
  120. collection = {}
  121. for _ in range(n):
  122.     data = input().split("|")
  123.     piece = data[0]
  124.     composer = data[1]
  125.     key = data[2]
  126.     if composer not in collection:
  127.         collection[piece] = {}
  128.         collection[piece] = {'composer': composer, 'key': key}
  129.     else:
  130.         collection[piece].update({'composer': composer, 'key': key})
  131.  
  132. command = input()
  133.  
  134. while not command == 'Stop':
  135.     details = command.split('|')
  136.     if details[0] == 'Add':
  137.         add_func(details)
  138.  
  139.     elif details[0] == 'Remove':
  140.         remove_func(details)
  141.  
  142.     elif details[0] == 'ChangeKey':
  143.         change_key_func(details)
  144.  
  145.     command = input()
  146.  
  147. for key, value in collection.items():
  148.     print(f'{key} -> Composer: {value["composer"]}, Key: {value["key"]}')
  149.  
  150.    
  151. =======================================================================================================
  152.  
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement