Advertisement
GeorgiLukanov87

05. Programming Fundamentals Final Exam with FUNCTIONS ! 300/300

Jul 29th, 2022 (edited)
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.16 KB | None | 0 0
  1. 05. Programming Fundamentals Final Exam with FUNCTIONS ! 300/300
  2. https://judge.softuni.org/Contests/Practice/Index/2302#2
  3.  
  4. # 01. Activation Keys
  5. # 02. Emoji Detector
  6. # 03. P!rates
  7.  
  8. ===============================================================================================================
  9.  
  10. # 01. Activation Keys
  11.  
  12.  
  13. def slice_func(some_details, some_key):
  14.     start_slice = int(some_details[1])
  15.     end_slice = int(some_details[2])
  16.  
  17.     to_remove = some_key[start_slice:end_slice]
  18.     some_key = some_key.replace(to_remove, "")
  19.  
  20.     print(some_key)
  21.     return some_key
  22.  
  23.  
  24. def flip_func(some_details, some_key):
  25.     make_it_to = some_details[1]
  26.     start_flip = int(some_details[2])
  27.     end_flip = int(some_details[3])
  28.     to_change = some_key[start_flip:end_flip]
  29.  
  30.     if make_it_to == 'Upper':
  31.         some_key = some_key[:start_flip] + to_change.upper() + some_key[end_flip:]
  32.     elif make_it_to == 'Lower':
  33.         some_key = some_key[:start_flip] + to_change.lower() + some_key[end_flip:]
  34.  
  35.     print(some_key)
  36.     return some_key
  37.  
  38.  
  39. def contains_func(some_details, some_key):
  40.     substring_to_check = some_details[1]
  41.     if substring_to_check in some_key:
  42.         print(f"{some_key} contains {substring_to_check}")
  43.     else:
  44.         print(f"Substring not found!")
  45.     return some_key
  46.  
  47.  
  48. key = input()
  49. command = input()
  50. while not command == 'Generate':
  51.     details = command.split('>>>')
  52.     if details[0] == 'Contains':
  53.         key = contains_func(details, key)
  54.  
  55.     elif details[0] == 'Flip':
  56.         key = flip_func(details, key)
  57.  
  58.     elif details[0] == 'Slice':
  59.         key = slice_func(details, key)
  60.  
  61.     command = input()
  62.  
  63. print(f'Your activation key is: {key}')
  64.  
  65.  
  66. ===============================================================================================================
  67.  
  68. # 02. Emoji Detector
  69.  
  70.  
  71. def calculate_threshold_func(some_data):
  72.     total_sum = 1
  73.     for digit in some_data:
  74.         if digit.isdigit():
  75.             total_sum *= int(digit)
  76.     print(f'Cool threshold: {total_sum}')
  77.     return total_sum
  78.  
  79.  
  80. def find_cool_emojis_and_print(some_threshold, some_result):
  81.     cool_emojis = []
  82.     for emoji in some_result:
  83.         emoji_sum = 0
  84.         symbols = emoji[0]
  85.         current_emoji = emoji[1]
  86.         for char in current_emoji:
  87.             emoji_sum += ord(char)
  88.         if emoji_sum > some_threshold:
  89.             cool_emojis.append(symbols + current_emoji + symbols)
  90.     print(f'{len(some_result)} emojis found in the text. The cool ones are:')
  91.     if cool_emojis:
  92.         print(*cool_emojis, sep='\n')
  93.  
  94.  
  95. import re
  96.  
  97. data = input()
  98. pattern = r'(::|\*\*)([A-Z][a-z]{2,})\1'
  99.  
  100. threshold = calculate_threshold_func(data)
  101. result = re.findall(pattern, data)
  102. find_cool_emojis_and_print(threshold, result)
  103.  
  104.  
  105. ===============================================================================================================
  106.  
  107. # 03. P!rates
  108.  
  109. def plunder_func(some_details, some_town):
  110.     plundered_town = some_details[1]
  111.     killed_people = int(some_details[2])
  112.     stolen_gold = int(some_details[3])
  113.    
  114.     some_town[plundered_town]['people'] -= killed_people
  115.     some_town[plundered_town]['gold'] -= stolen_gold
  116.    
  117.     print(f"{plundered_town} plundered! {stolen_gold} gold stolen, {killed_people} citizens killed.")
  118.     if some_town[plundered_town]['gold'] == 0 or some_town[plundered_town]['people'] == 0:
  119.         del some_town[plundered_town]
  120.         print(f"{plundered_town} has been wiped off the map!")
  121.     return some_town
  122.  
  123.  
  124. def prosper_func(some_details, some_town):
  125.     prosper_town = some_details[1]
  126.     prosper_gold = int(some_details[2])
  127.    
  128.     if not prosper_gold < 0:
  129.         some_town[prosper_town]['gold'] += prosper_gold
  130.         print(f"{prosper_gold} gold added to the city treasury."
  131.               f" {prosper_town} now has {some_town[prosper_town]['gold']} gold.")
  132.     else:
  133.         print(f"Gold added cannot be a negative number!")
  134.     return some_town
  135.  
  136.  
  137. def final_print_func(result):
  138.     if result:
  139.         print(f"Ahoy, Captain! There are {len(result)} wealthy settlements to go to:")
  140.         for city in result:
  141.             print(f'{city} -> Population: {result[city]["people"]} citizens, Gold: {result[city]["gold"]} kg')
  142.     else:
  143.         print('Ahoy, Captain! All targets have been plundered and destroyed!')
  144.  
  145.  
  146. all_towns = {}
  147. command1 = input()
  148. while not command1 == 'Sail':
  149.     information = command1.split("||")
  150.    
  151.     town = information[0]
  152.     people = int(information[1])
  153.     gold = int(information[2])
  154.    
  155.     if town not in all_towns:
  156.         all_towns[town] = {'people': people, 'gold': gold}
  157.     else:
  158.         all_towns[town]['people'] += people
  159.         all_towns[town]['gold'] += gold
  160.        
  161.     command1 = input()
  162.  
  163. command2 = input()
  164.  
  165. while not command2 == 'End':
  166.     details = command2.split('=>')
  167.     if details[0] == 'Plunder':
  168.         all_towns = plunder_func(details, all_towns)
  169.  
  170.     elif details[0] == 'Prosper':
  171.         all_towns = prosper_func(details, all_towns)
  172.        
  173.     command2 = input()
  174.  
  175. final_print_func(all_towns)
  176.  
  177. ===============================================================================================================
  178.  
  179.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement