Advertisement
GeorgiLukanov87

04. Programming Fundamentals Final Exam 300/300

Jul 14th, 2022
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.30 KB | None | 0 0
  1. # 04. Programming Fundamentals Final Exam 300/300
  2. # https://judge.softuni.org/Contests/Practice/Index/2303#0
  3. #
  4. # 01. Password Reset
  5. # 02. Fancy Barcodes
  6. # 03. Heroes of Code and Logic VI
  7. ==============================================================================================
  8.  
  9. # 01. Password Reset
  10.  
  11. password = input()
  12. command = input()
  13. while not command == 'Done':
  14.     raw_password = ""
  15.     command = command.split()
  16.     if command[0] == 'TakeOdd':
  17.         for odd_indx in range(len(password)):
  18.             if odd_indx % 2 != 0:
  19.                 raw_password += password[odd_indx]
  20.         password = raw_password
  21.  
  22.     elif command[0] == 'Cut':
  23.         index = int(command[1])
  24.         length = int(command[2])
  25.         password = password[:index] + password[index+length:]
  26.  
  27.     elif command[0] == 'Substitute':
  28.         substring = command[1]
  29.         substitute = command[2]
  30.         if substring in password:
  31.             password = password.replace(substring, substitute)
  32.         else:
  33.             print("Nothing to replace!")
  34.             command = input()
  35.             continue
  36.  
  37.     print(password)
  38.     command = input()
  39.  
  40. print(f"Your password is: {password}")
  41.  
  42. ==============================================================================================
  43.  
  44. # 02. Fancy Barcodes
  45.  
  46. import re
  47.  
  48.  
  49. def calculate_sum(some_string: str):
  50.     sum_of_digits = ""
  51.     for char in some_string:
  52.         if char.isdigit():
  53.             sum_of_digits += char
  54.     if len(sum_of_digits) > 0:
  55.         return sum_of_digits
  56.     return "00"
  57.  
  58.  
  59. n = int(input())
  60.  
  61. pattern = r'(\@)(\#+)([A-Z][A-Za-z0-9]{4,}[A-Z])(\@)(\#+)'
  62.  
  63. for b_code in range(n):
  64.     data = input()
  65.     valid_data = re.findall(pattern, data)
  66.     if len(valid_data) == 0:
  67.         print('Invalid barcode')
  68.     else:
  69.         for el in valid_data:
  70.             current_word = el[2]
  71.             final_result = calculate_sum(current_word)
  72.             print(f'Product group: {final_result}')
  73.  
  74. ==============================================================================================
  75.  
  76. # 03. Heroes of Code and Logic VI
  77.  
  78. n_heroes = int(input())
  79. heroes = {}
  80. command = input()
  81.  
  82. for _ in range(n_heroes):
  83.     command = command.split()
  84.    
  85.     name = command[0]
  86.     hp = int(command[1])
  87.     mp = int(command[2])
  88.     if name not in heroes:
  89.         heroes[name] = [hp] + [mp]
  90.     else:
  91.         heroes[name] += hp
  92.         heroes[name] += mp
  93.        
  94.     command = input()
  95.    
  96. while not command == 'End':
  97.     command = command.split(' - ')
  98.     if command[0] == 'CastSpell':
  99.         name = command[1]
  100.         mp_needed = int(command[2])
  101.         spell_name = command[3]
  102.         if heroes[name][1] >= mp_needed:
  103.             heroes[name][1] -= mp_needed
  104.             print(f"{name} has successfully cast {spell_name} and now has {heroes[name][1]} MP!")
  105.         else:
  106.             print(f"{name} does not have enough MP to cast {spell_name}!")
  107.  
  108.     elif command[0] == 'TakeDamage':
  109.         name = command[1]
  110.         damage = int(command[2])
  111.         attacker = command[3]
  112.         heroes[name][0] -= damage
  113.         if heroes[name][0] > 0:
  114.             print(f'{name} was hit for {damage} HP by {attacker} and now has {heroes[name][0]} HP left!')
  115.         else:
  116.             print(f"{name} has been killed by {attacker}!")
  117.             del heroes[name]
  118.  
  119.     elif command[0] == 'Recharge':
  120.         name = command[1]
  121.         amount = int(command[2])
  122.         temp_mana = heroes[name][1]
  123.         if temp_mana + amount >= 200:
  124.             heroes[name][1] = 200
  125.             print(f"{name} recharged for {200-temp_mana} MP!")
  126.         else:
  127.             heroes[name][1] += amount
  128.             print(f"{name} recharged for {amount} MP!")
  129.  
  130.     elif command[0] == 'Heal':
  131.         name = command[1]
  132.         amount = int(command[2])
  133.         temp_health = heroes[name][0]
  134.         if temp_health + amount >= 100:
  135.             heroes[name][0] = 100
  136.             print(f"{name} healed for {100 - temp_health} HP!")
  137.         else:
  138.             heroes[name][0] += amount
  139.             print(f"{name} healed for {amount} HP!")
  140.  
  141.     command = input()
  142.    
  143. for hero in heroes:
  144.     print(hero)
  145.     print(f'  HP: {heroes[hero][0]}')
  146.     print(f'  MP: {heroes[hero][1]}')
  147.  
  148. ==============================================================================================
  149.  
  150.  
  151.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement