Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 04. Programming Fundamentals Final Exam with FUNCTIONS ! 300/300
- https://judge.softuni.org/Contests/Practice/Index/2303#2
- ## 01. Password Reset
- ## 02. Fancy Barcodes
- ## 03. Heroes of Code and Logic VII
- ====================================================================================================
- # 01. Password Reset
- def substitute_func(some_details, some_pass):
- substring = some_details[1]
- to_replace = some_details[2]
- if substring in some_pass:
- some_pass = some_pass.replace(substring, to_replace)
- print(some_pass)
- else:
- print('Nothing to replace!')
- return some_pass
- def cut_func(some_details, some_pass):
- index = int(some_details[1])
- length = int(some_details[2])
- some_pass = some_pass[:index] + some_pass[index+length:]
- print(some_pass)
- return some_pass
- def take_odd_func(some_pass):
- some_pass = some_pass[1::2]
- print(some_pass)
- return some_pass
- password = input()
- command = input()
- while not command == 'Done':
- details = command.split(' ')
- if details[0] == 'TakeOdd':
- password = take_odd_func(password)
- elif details[0] == 'Cut':
- password = cut_func(details, password)
- elif details[0] == 'Substitute':
- password = substitute_func(details, password)
- command = input()
- print(f"Your password is: {password}")
- ====================================================================================================
- # 02. Fancy Barcodes
- def extract_datas(some_pattern, some_count):
- valid_datas = []
- for _ in range(some_count):
- data = input()
- result = re.findall(some_pattern, data)
- if result:
- valid_datas.append(result[0][1])
- else:
- valid_datas.append([])
- return valid_datas
- def calculate_product_and_print(valid_matches):
- for current_match in valid_matches:
- product_sum = ""
- if current_match:
- for char in current_match:
- if char.isdigit():
- product_sum += char
- if product_sum == "":
- print('Product group: 00')
- else:
- print(f'Product group: {product_sum}')
- else:
- print('Invalid barcode')
- import re
- n = int(input())
- pattern = r'(\@\#{1,})([A-Z][A-Za-z0-9]{4,}[A-Z])\b(\@\#{1,})'
- matches = extract_datas(pattern, n)
- calculate_product_and_print(matches)
- ====================================================================================================
- # 03. Heroes of Code and Logic VII
- def create_my_heroes_dict_func(number_of_heroes):
- initial_heroes = {}
- for _ in range(n):
- hero_info = input().split(' ')
- name = hero_info[0]
- hp = int(hero_info[1])
- mp = int(hero_info[2])
- initial_heroes[name] = {'hp': hp, 'mp': mp}
- return initial_heroes
- def heal_func(some_details, some_heroes):
- name = some_details[1]
- amount = int(some_details[2])
- if some_heroes[name]['hp'] + amount >= 100:
- print(f"{name} healed for {100 - some_heroes[name]['hp']} HP!")
- some_heroes[name]['hp'] = 100
- else:
- print(f"{name} healed for {amount} HP!")
- some_heroes[name]['hp'] += amount
- return some_heroes
- def recharge_func(some_details, some_heroes):
- name = some_details[1]
- amount = int(some_details[2])
- if some_heroes[name]['mp'] + amount >= 200:
- print(f"{name} recharged for {200 - some_heroes[name]['mp']} MP!")
- some_heroes[name]['mp'] = 200
- else:
- print(f"{name} recharged for {amount} MP!")
- some_heroes[name]['mp'] += amount
- return some_heroes
- def take_damage_func(some_details, some_heroes):
- name = some_details[1]
- damage = int(some_details[2])
- attacker = some_details[3]
- some_heroes[name]['hp'] -= damage
- if some_heroes[name]['hp'] > 0:
- print(f"{name} was hit for {damage} HP by {attacker} and now has {some_heroes[name]['hp']} HP left!")
- else:
- print(f"{name} has been killed by {attacker}!")
- del some_heroes[name]
- return some_heroes
- def cast_spell_func(some_details, some_heroes):
- name = some_details[1]
- mp_needed = int(some_details[2])
- spell_name = some_details[3]
- if some_heroes[name]['mp'] >= mp_needed:
- some_heroes[name]['mp'] -= mp_needed
- print(f"{name} has successfully cast {spell_name} and now has {some_heroes[name]['mp']} MP!")
- else:
- print(f"{name} does not have enough MP to cast {spell_name}!")
- return some_heroes
- def final_print_func(all_heroes):
- for name in all_heroes:
- print(name)
- print(f' HP: {all_heroes[name]["hp"]}')
- print(f' MP: {all_heroes[name]["mp"]}')
- n = int(input())
- heroes = create_my_heroes_dict_func(n)
- command = input()
- while not command == 'End':
- details = command.split(' - ')
- if details[0] == 'CastSpell':
- heroes = cast_spell_func(details, heroes)
- elif details[0] == 'TakeDamage':
- heroes = take_damage_func(details, heroes)
- elif details[0] == 'Recharge':
- heroes = recharge_func(details, heroes)
- elif details[0] == 'Heal':
- heroes = heal_func(details, heroes)
- command = input()
- final_print_func(heroes)
- ====================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement