Advertisement
GeorgiLukanov87

03. Programming Fundamentals Final Exam Retake with FUNCTIONS ! 300/300

Jul 29th, 2022 (edited)
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.00 KB | None | 0 0
  1. 03. Programming Fundamentals Final Exam Retake with FUNCTIONS ! 300/300
  2. https://judge.softuni.org/Contests/Practice/Index/2307#0
  3.    
  4. ## 01. Secret Chat
  5. ## 02. Mirror Words
  6. ## 03. Need for Speed III
  7.  
  8. ==============================================================================================================================
  9.  
  10. # 01. Secret Chat
  11.  
  12.  
  13. def insert_space_func(some_details, some_message):
  14.     index = int(some_details[1])
  15.     some_message = some_message[:index] + ' ' + some_message[index:]
  16.     print(some_message)
  17.     return some_message
  18.  
  19.  
  20. def reverse_func(some_details, some_message):
  21.     substring = some_details[1]
  22.     if substring in some_message:
  23.         some_message = some_message.replace(substring, "", 1)
  24.         some_message += substring[::-1]
  25.         print(some_message)
  26.     else:
  27.         print('error')
  28.     return some_message
  29.  
  30.  
  31. def change_all_func(some_details, some_message):
  32.     substring = some_details[1]
  33.     replacement = some_details[2]
  34.     if substring in some_message:
  35.         some_message = some_message.replace(substring, replacement)
  36.         print(some_message)
  37.     return some_message
  38.  
  39.  
  40. message = input()
  41. command = input()
  42.  
  43. while not command == 'Reveal':
  44.     details = command.split(':|:')
  45.     if details[0] == 'InsertSpace':
  46.         message = insert_space_func(details, message)
  47.  
  48.     elif details[0] == 'Reverse':
  49.         message = reverse_func(details, message)
  50.  
  51.     elif details[0] == 'ChangeAll':
  52.         message = change_all_func(details, message)
  53.  
  54.     command = input()
  55.  
  56. print(f"You have a new text message: {message}")
  57.  
  58.  
  59. ==============================================================================================================================
  60.  
  61. # 02. Mirror Words
  62.  
  63.  
  64. def extracting_datas(matches):
  65.     pairs = []
  66.     pairs_found = 0
  67.     for match in matches:
  68.         pairs_found += 1
  69.         word1 = match[1]
  70.         word2 = match[3]
  71.         if word1 == word2[::-1]:
  72.             to_print = f'{word1} <=> {word2}'
  73.             pairs.append(to_print)
  74.     return pairs, pairs_found
  75.  
  76.  
  77. import re
  78.  
  79. data = input()
  80. pattern = r'(\@|\#)([A-Za-z]{3,})\1(\@|\#)([A-Za-z]{3,})\3'
  81. matches_pairs = re.findall(pattern, data)
  82. valid_pairs, word_pairs_found = extracting_datas(matches_pairs)
  83.  
  84. if word_pairs_found > 0:
  85.     print(f'{word_pairs_found} word pairs found!')
  86. else:
  87.     print('No word pairs found!')
  88.  
  89. if valid_pairs:
  90.     print('The mirror words are:')
  91.     print(', '.join(valid_pairs))
  92. else:
  93.     print('No mirror words!')
  94.  
  95. ==============================================================================================================================
  96.  
  97. # 03. Need for Speed III
  98.  
  99.  
  100. def create_car_dict_func(count_of_cars):
  101.     initial_cars = {}
  102.     for _ in range(count_of_cars):
  103.         input_line = input().split('|')
  104.         car = input_line[0]
  105.         mileage = int(input_line[1])
  106.         fuel = int(input_line[2])
  107.         initial_cars[car] = {'mileage': mileage, 'fuel': fuel}
  108.     return initial_cars
  109.  
  110.  
  111. def drive_func(some_details, some_cars):
  112.     car = some_details[1]
  113.     distance = int(some_details[2])
  114.     fuel = int(some_details[3])
  115.     if some_cars[car]['fuel'] >= fuel:
  116.         some_cars[car]['fuel'] -= fuel
  117.         some_cars[car]['mileage'] += distance
  118.         print(f"{car} driven for {distance} kilometers. {fuel} liters of fuel consumed.")
  119.  
  120.         if some_cars[car]['mileage'] >= 100000:
  121.             print(f'Time to sell the {car}!')
  122.             del some_cars[car]
  123.     else:
  124.         print('Not enough fuel to make that ride')
  125.     return some_cars
  126.  
  127.  
  128. def refuel_func(some_details, some_cars):
  129.     car = some_details[1]
  130.     fuel = int(some_details[2])
  131.     if some_cars[car]['fuel'] + fuel >= 75:
  132.         print(f'{car} refueled with {75 - some_cars[car]["fuel"]} liters')
  133.         some_cars[car]['fuel'] = 75
  134.     else:
  135.         print(f'{car} refueled with {fuel} liters')
  136.         some_cars[car]['fuel'] += fuel
  137.     return some_cars
  138.  
  139.  
  140. def revert_func(some_details, some_cars):
  141.     car = some_details[1]
  142.     kilometers = int(some_details[2])
  143.     some_cars[car]['mileage'] -= kilometers
  144.     if some_cars[car]['mileage'] < 10000:
  145.         some_cars[car]['mileage'] = 10000
  146.     else:
  147.         print(f'{car} mileage decreased by {kilometers} kilometers')
  148.     return some_cars
  149.  
  150.  
  151. def final_print_func(some_cars):
  152.     for car in some_cars:
  153.         print(f"{car} -> Mileage: {some_cars[car]['mileage']} kms, Fuel in the tank: {some_cars[car]['fuel']} lt.")
  154.  
  155.  
  156. n = int(input())
  157. my_cars = create_car_dict_func(n)
  158. command = input()
  159.  
  160. while not command == 'Stop':
  161.     details = command.split(" : ")
  162.     if details[0] == 'Drive':
  163.         my_cars = drive_func(details, my_cars)
  164.     elif details[0] == 'Refuel':
  165.         my_cars = refuel_func(details, my_cars)
  166.     elif details[0] == 'Revert':
  167.         my_cars = revert_func(details, my_cars)
  168.  
  169.     command = input()
  170.  
  171. final_print_func(my_cars)
  172.  
  173. ==============================================================================================================================
  174.  
  175.  
  176.  
  177.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement