Advertisement
GeorgiLukanov87

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

Jul 28th, 2022 (edited)
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.39 KB | None | 0 0
  1. # 02. Programming Fundamentals Final Exam with FUNCTIONS ! 300/300
  2. # https://judge.softuni.org/Contests/Practice/Index/2518#2
  3.  
  4. ## 01. World Tour
  5. ## 02. Destination Mapper
  6. ## 03. Plant Discovery
  7.  
  8. ======================================================================================================
  9.  
  10. # 01. World Tour
  11.  
  12.  
  13. def is_index_valid(some_index, some_data):
  14.     if 0 <= some_index < len(some_data):
  15.         return True
  16.     return False
  17.  
  18.  
  19. def add_stop_func(main_data, some_data):
  20.     index = int(main_data[1])
  21.     string = main_data[2]
  22.     if is_index_valid(index, some_data):
  23.         some_data = some_data[:index] + string + some_data[index:]
  24.         print(some_data)
  25.     return some_data
  26.  
  27.  
  28. def remove_stop_func(main_data, some_data):
  29.     start_index = int(main_data[1])
  30.     end_index = int(main_data[2])
  31.     if is_index_valid(start_index, some_data) and is_index_valid(end_index, some_data):
  32.         to_remove = some_data[start_index:end_index + 1]
  33.         some_data = some_data.replace(to_remove, "")
  34.     print(some_data)
  35.     return some_data
  36.  
  37.  
  38. def switch_func(main_data, some_data):
  39.     old_string = main_data[1]
  40.     new_string = main_data[2]
  41.     if old_string in some_data:
  42.         some_data = some_data.replace(old_string, new_string)
  43.     print(some_data)
  44.     return some_data
  45.  
  46.  
  47. data = input()
  48. command = input()
  49. while not command == 'Travel':
  50.     details = command.split(":")
  51.     if details[0] == 'Add Stop':
  52.         data = add_stop_func(details, data)
  53.  
  54.     elif details[0] == 'Remove Stop':
  55.         data = remove_stop_func(details, data)
  56.  
  57.     elif details[0] == 'Switch':
  58.         data = switch_func(details, data)
  59.  
  60.     command = input()
  61.  
  62. print(f"Ready for world tour! Planned stops: {data}")
  63.  
  64.  
  65. ======================================================================================================
  66.  
  67. # 02. Destination Mapper
  68.  
  69.  
  70. def extract_datas(some_pattern, some_data):
  71.     all_destinations = []
  72.     travel_points = 0
  73.     matched = re.findall(patter, data)
  74.     for match in matched:
  75.         current_destination = match[1]
  76.         all_destinations.append(current_destination)
  77.         for _ in current_destination:
  78.             travel_points += 1
  79.     return all_destinations, travel_points
  80.  
  81.  
  82. import re
  83.  
  84. data = input()
  85. patter = r'(\=|\/)([A-Z][a-zA-Z]{2,})\1'
  86. result, total_travel_points = extract_datas(patter, data)
  87.  
  88. print(f'Destinations: {", ".join(result)}')
  89. print(f'Travel Points: {total_travel_points}')
  90.  
  91.  
  92. ======================================================================================================
  93.  
  94. # 03. Plant Discovery
  95.  
  96.  
  97. def create_plant_dict_func(plants_number):
  98.     initial_plants = {}
  99.     for _ in range(plants_number):
  100.         info = input().split('<->')
  101.         current_name = info[0]
  102.         rarity = int(info[1])
  103.         initial_plants[current_name] = {'rarity': rarity, 'rating': 0}
  104.     return initial_plants
  105.  
  106.  
  107. def rate_func(some_details, some_plants):
  108.     current_name = some_details[1]
  109.     rating = int(some_details[-1])
  110.     if some_plants[current_name]['rating'] == 0:
  111.         some_plants[current_name]['rating'] += rating
  112.     else:
  113.         some_plants[current_name]['rating'] += rating
  114.         some_plants[current_name]['rating'] /= 2
  115.     return some_plants
  116.  
  117.  
  118. def update_func(some_details, some_plants):
  119.     current_name = some_details[1]
  120.     new_rarity = int(some_details[-1])
  121.     some_plants[current_name]['rarity'] = new_rarity
  122.     return some_plants
  123.  
  124.  
  125. def reset_func(some_details, some_plants):
  126.     current_name = some_details[1]
  127.     some_plants[current_name]['rating'] = 0
  128.     return some_plants
  129.  
  130.  
  131. n = int(input())
  132. plants = create_plant_dict_func(n)
  133. command = input()
  134. while not command == 'Exhibition':
  135.     details = command.split()
  136.     if details[1] not in plants:
  137.         print('error')
  138.         command = input()
  139.         continue
  140.     else:
  141.         if details[0] == 'Rate:':
  142.             plants = rate_func(details, plants)
  143.  
  144.         elif details[0] == 'Update:':
  145.             plants = update_func(details, plants)
  146.  
  147.         elif details[0] == 'Reset:':
  148.             plants = reset_func(details, plants)
  149.  
  150.         command = input()
  151.  
  152. print('Plants for the exhibition:')
  153. for name, v in plants.items():
  154.     print(f'- {name}; Rarity: {plants[name]["rarity"]}; Rating: {plants[name]["rating"]:.2f}')
  155.    
  156. ======================================================================================================
  157.  
  158.  
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement