Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 05. Programming Fundamentals Final Exam with FUNCTIONS ! 300/300
- https://judge.softuni.org/Contests/Practice/Index/2302#2
- # 01. Activation Keys
- # 02. Emoji Detector
- # 03. P!rates
- ===============================================================================================================
- # 01. Activation Keys
- def slice_func(some_details, some_key):
- start_slice = int(some_details[1])
- end_slice = int(some_details[2])
- to_remove = some_key[start_slice:end_slice]
- some_key = some_key.replace(to_remove, "")
- print(some_key)
- return some_key
- def flip_func(some_details, some_key):
- make_it_to = some_details[1]
- start_flip = int(some_details[2])
- end_flip = int(some_details[3])
- to_change = some_key[start_flip:end_flip]
- if make_it_to == 'Upper':
- some_key = some_key[:start_flip] + to_change.upper() + some_key[end_flip:]
- elif make_it_to == 'Lower':
- some_key = some_key[:start_flip] + to_change.lower() + some_key[end_flip:]
- print(some_key)
- return some_key
- def contains_func(some_details, some_key):
- substring_to_check = some_details[1]
- if substring_to_check in some_key:
- print(f"{some_key} contains {substring_to_check}")
- else:
- print(f"Substring not found!")
- return some_key
- key = input()
- command = input()
- while not command == 'Generate':
- details = command.split('>>>')
- if details[0] == 'Contains':
- key = contains_func(details, key)
- elif details[0] == 'Flip':
- key = flip_func(details, key)
- elif details[0] == 'Slice':
- key = slice_func(details, key)
- command = input()
- print(f'Your activation key is: {key}')
- ===============================================================================================================
- # 02. Emoji Detector
- def calculate_threshold_func(some_data):
- total_sum = 1
- for digit in some_data:
- if digit.isdigit():
- total_sum *= int(digit)
- print(f'Cool threshold: {total_sum}')
- return total_sum
- def find_cool_emojis_and_print(some_threshold, some_result):
- cool_emojis = []
- for emoji in some_result:
- emoji_sum = 0
- symbols = emoji[0]
- current_emoji = emoji[1]
- for char in current_emoji:
- emoji_sum += ord(char)
- if emoji_sum > some_threshold:
- cool_emojis.append(symbols + current_emoji + symbols)
- print(f'{len(some_result)} emojis found in the text. The cool ones are:')
- if cool_emojis:
- print(*cool_emojis, sep='\n')
- import re
- data = input()
- pattern = r'(::|\*\*)([A-Z][a-z]{2,})\1'
- threshold = calculate_threshold_func(data)
- result = re.findall(pattern, data)
- find_cool_emojis_and_print(threshold, result)
- ===============================================================================================================
- # 03. P!rates
- def plunder_func(some_details, some_town):
- plundered_town = some_details[1]
- killed_people = int(some_details[2])
- stolen_gold = int(some_details[3])
- some_town[plundered_town]['people'] -= killed_people
- some_town[plundered_town]['gold'] -= stolen_gold
- print(f"{plundered_town} plundered! {stolen_gold} gold stolen, {killed_people} citizens killed.")
- if some_town[plundered_town]['gold'] == 0 or some_town[plundered_town]['people'] == 0:
- del some_town[plundered_town]
- print(f"{plundered_town} has been wiped off the map!")
- return some_town
- def prosper_func(some_details, some_town):
- prosper_town = some_details[1]
- prosper_gold = int(some_details[2])
- if not prosper_gold < 0:
- some_town[prosper_town]['gold'] += prosper_gold
- print(f"{prosper_gold} gold added to the city treasury."
- f" {prosper_town} now has {some_town[prosper_town]['gold']} gold.")
- else:
- print(f"Gold added cannot be a negative number!")
- return some_town
- def final_print_func(result):
- if result:
- print(f"Ahoy, Captain! There are {len(result)} wealthy settlements to go to:")
- for city in result:
- print(f'{city} -> Population: {result[city]["people"]} citizens, Gold: {result[city]["gold"]} kg')
- else:
- print('Ahoy, Captain! All targets have been plundered and destroyed!')
- all_towns = {}
- command1 = input()
- while not command1 == 'Sail':
- information = command1.split("||")
- town = information[0]
- people = int(information[1])
- gold = int(information[2])
- if town not in all_towns:
- all_towns[town] = {'people': people, 'gold': gold}
- else:
- all_towns[town]['people'] += people
- all_towns[town]['gold'] += gold
- command1 = input()
- command2 = input()
- while not command2 == 'End':
- details = command2.split('=>')
- if details[0] == 'Plunder':
- all_towns = plunder_func(details, all_towns)
- elif details[0] == 'Prosper':
- all_towns = prosper_func(details, all_towns)
- command2 = input()
- final_print_func(all_towns)
- ===============================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement