Advertisement
GeorgiLukanov87

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

Jul 29th, 2022 (edited)
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.43 KB | None | 0 0
  1. 04. Programming Fundamentals Final Exam with FUNCTIONS ! 300/300
  2. https://judge.softuni.org/Contests/Practice/Index/2303#2
  3.  
  4. ## 01. Password Reset
  5. ## 02. Fancy Barcodes
  6. ## 03. Heroes of Code and Logic VII
  7. ====================================================================================================
  8.  
  9. # 01. Password Reset
  10.  
  11.  
  12. def substitute_func(some_details, some_pass):
  13.     substring = some_details[1]
  14.     to_replace = some_details[2]
  15.     if substring in some_pass:
  16.         some_pass = some_pass.replace(substring, to_replace)
  17.         print(some_pass)
  18.     else:
  19.         print('Nothing to replace!')
  20.     return some_pass
  21.  
  22.  
  23. def cut_func(some_details, some_pass):
  24.     index = int(some_details[1])
  25.     length = int(some_details[2])
  26.     some_pass = some_pass[:index] + some_pass[index+length:]
  27.     print(some_pass)
  28.     return some_pass
  29.  
  30.  
  31. def take_odd_func(some_pass):
  32.     some_pass = some_pass[1::2]
  33.     print(some_pass)
  34.     return some_pass
  35.  
  36.  
  37. password = input()
  38. command = input()
  39. while not command == 'Done':
  40.     details = command.split(' ')
  41.     if details[0] == 'TakeOdd':
  42.         password = take_odd_func(password)
  43.  
  44.     elif details[0] == 'Cut':
  45.         password = cut_func(details, password)
  46.  
  47.     elif details[0] == 'Substitute':
  48.         password = substitute_func(details, password)
  49.  
  50.     command = input()
  51.  
  52. print(f"Your password is: {password}")
  53.  
  54.  
  55. ====================================================================================================
  56.  
  57. # 02. Fancy Barcodes
  58.  
  59.  
  60. def extract_datas(some_pattern, some_count):
  61.     valid_datas = []
  62.     for _ in range(some_count):
  63.         data = input()
  64.         result = re.findall(some_pattern, data)
  65.        
  66.         if result:
  67.             valid_datas.append(result[0][1])
  68.         else:
  69.             valid_datas.append([])
  70.     return valid_datas
  71.  
  72.  
  73. def calculate_product_and_print(valid_matches):
  74.     for current_match in valid_matches:
  75.         product_sum = ""
  76.         if current_match:
  77.             for char in current_match:
  78.                 if char.isdigit():
  79.                     product_sum += char
  80.                    
  81.             if product_sum == "":
  82.                 print('Product group: 00')
  83.             else:
  84.                 print(f'Product group: {product_sum}')
  85.         else:
  86.             print('Invalid barcode')
  87.  
  88.  
  89. import re
  90.  
  91. n = int(input())
  92.  
  93. pattern = r'(\@\#{1,})([A-Z][A-Za-z0-9]{4,}[A-Z])\b(\@\#{1,})'
  94. matches = extract_datas(pattern, n)
  95. calculate_product_and_print(matches)
  96.  
  97.  
  98. ====================================================================================================
  99.  
  100. # 03. Heroes of Code and Logic VII
  101.  
  102.  
  103. def create_my_heroes_dict_func(number_of_heroes):
  104.     initial_heroes = {}
  105.     for _ in range(n):
  106.         hero_info = input().split(' ')
  107.         name = hero_info[0]
  108.         hp = int(hero_info[1])
  109.         mp = int(hero_info[2])
  110.         initial_heroes[name] = {'hp': hp, 'mp': mp}
  111.     return initial_heroes
  112.  
  113.  
  114. def heal_func(some_details, some_heroes):
  115.     name = some_details[1]
  116.     amount = int(some_details[2])
  117.     if some_heroes[name]['hp'] + amount >= 100:
  118.         print(f"{name} healed for {100 - some_heroes[name]['hp']} HP!")
  119.         some_heroes[name]['hp'] = 100
  120.     else:
  121.         print(f"{name} healed for {amount} HP!")
  122.         some_heroes[name]['hp'] += amount
  123.     return some_heroes
  124.  
  125.  
  126. def recharge_func(some_details, some_heroes):
  127.     name = some_details[1]
  128.     amount = int(some_details[2])
  129.     if some_heroes[name]['mp'] + amount >= 200:
  130.         print(f"{name} recharged for {200 - some_heroes[name]['mp']} MP!")
  131.         some_heroes[name]['mp'] = 200
  132.     else:
  133.         print(f"{name} recharged for {amount} MP!")
  134.         some_heroes[name]['mp'] += amount
  135.     return some_heroes
  136.  
  137.  
  138. def take_damage_func(some_details, some_heroes):
  139.     name = some_details[1]
  140.     damage = int(some_details[2])
  141.     attacker = some_details[3]
  142.     some_heroes[name]['hp'] -= damage
  143.     if some_heroes[name]['hp'] > 0:
  144.         print(f"{name} was hit for {damage} HP by {attacker} and now has {some_heroes[name]['hp']} HP left!")
  145.     else:
  146.         print(f"{name} has been killed by {attacker}!")
  147.         del some_heroes[name]
  148.  
  149.     return some_heroes
  150.  
  151.  
  152. def cast_spell_func(some_details, some_heroes):
  153.     name = some_details[1]
  154.     mp_needed = int(some_details[2])
  155.     spell_name = some_details[3]
  156.     if some_heroes[name]['mp'] >= mp_needed:
  157.         some_heroes[name]['mp'] -= mp_needed
  158.         print(f"{name} has successfully cast {spell_name} and now has {some_heroes[name]['mp']} MP!")
  159.     else:
  160.         print(f"{name} does not have enough MP to cast {spell_name}!")
  161.     return some_heroes
  162.  
  163.  
  164. def final_print_func(all_heroes):
  165.     for name in all_heroes:
  166.         print(name)
  167.         print(f'  HP: {all_heroes[name]["hp"]}')
  168.         print(f'  MP: {all_heroes[name]["mp"]}')
  169.  
  170.  
  171. n = int(input())
  172. heroes = create_my_heroes_dict_func(n)
  173.  
  174. command = input()
  175. while not command == 'End':
  176.     details = command.split(' - ')
  177.  
  178.     if details[0] == 'CastSpell':
  179.         heroes = cast_spell_func(details, heroes)
  180.  
  181.     elif details[0] == 'TakeDamage':
  182.         heroes = take_damage_func(details, heroes)
  183.  
  184.     elif details[0] == 'Recharge':
  185.         heroes = recharge_func(details, heroes)
  186.  
  187.     elif details[0] == 'Heal':
  188.         heroes = heal_func(details, heroes)
  189.  
  190.     command = input()
  191.  
  192. final_print_func(heroes)
  193.  
  194.  
  195. ====================================================================================================
  196.  
  197.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement