Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 03. Programming Fundamentals Final Exam Retake with FUNCTIONS ! 300/300
- https://judge.softuni.org/Contests/Practice/Index/2307#0
- ## 01. Secret Chat
- ## 02. Mirror Words
- ## 03. Need for Speed III
- ==============================================================================================================================
- # 01. Secret Chat
- def insert_space_func(some_details, some_message):
- index = int(some_details[1])
- some_message = some_message[:index] + ' ' + some_message[index:]
- print(some_message)
- return some_message
- def reverse_func(some_details, some_message):
- substring = some_details[1]
- if substring in some_message:
- some_message = some_message.replace(substring, "", 1)
- some_message += substring[::-1]
- print(some_message)
- else:
- print('error')
- return some_message
- def change_all_func(some_details, some_message):
- substring = some_details[1]
- replacement = some_details[2]
- if substring in some_message:
- some_message = some_message.replace(substring, replacement)
- print(some_message)
- return some_message
- message = input()
- command = input()
- while not command == 'Reveal':
- details = command.split(':|:')
- if details[0] == 'InsertSpace':
- message = insert_space_func(details, message)
- elif details[0] == 'Reverse':
- message = reverse_func(details, message)
- elif details[0] == 'ChangeAll':
- message = change_all_func(details, message)
- command = input()
- print(f"You have a new text message: {message}")
- ==============================================================================================================================
- # 02. Mirror Words
- def extracting_datas(matches):
- pairs = []
- pairs_found = 0
- for match in matches:
- pairs_found += 1
- word1 = match[1]
- word2 = match[3]
- if word1 == word2[::-1]:
- to_print = f'{word1} <=> {word2}'
- pairs.append(to_print)
- return pairs, pairs_found
- import re
- data = input()
- pattern = r'(\@|\#)([A-Za-z]{3,})\1(\@|\#)([A-Za-z]{3,})\3'
- matches_pairs = re.findall(pattern, data)
- valid_pairs, word_pairs_found = extracting_datas(matches_pairs)
- if word_pairs_found > 0:
- print(f'{word_pairs_found} word pairs found!')
- else:
- print('No word pairs found!')
- if valid_pairs:
- print('The mirror words are:')
- print(', '.join(valid_pairs))
- else:
- print('No mirror words!')
- ==============================================================================================================================
- # 03. Need for Speed III
- def create_car_dict_func(count_of_cars):
- initial_cars = {}
- for _ in range(count_of_cars):
- input_line = input().split('|')
- car = input_line[0]
- mileage = int(input_line[1])
- fuel = int(input_line[2])
- initial_cars[car] = {'mileage': mileage, 'fuel': fuel}
- return initial_cars
- def drive_func(some_details, some_cars):
- car = some_details[1]
- distance = int(some_details[2])
- fuel = int(some_details[3])
- if some_cars[car]['fuel'] >= fuel:
- some_cars[car]['fuel'] -= fuel
- some_cars[car]['mileage'] += distance
- print(f"{car} driven for {distance} kilometers. {fuel} liters of fuel consumed.")
- if some_cars[car]['mileage'] >= 100000:
- print(f'Time to sell the {car}!')
- del some_cars[car]
- else:
- print('Not enough fuel to make that ride')
- return some_cars
- def refuel_func(some_details, some_cars):
- car = some_details[1]
- fuel = int(some_details[2])
- if some_cars[car]['fuel'] + fuel >= 75:
- print(f'{car} refueled with {75 - some_cars[car]["fuel"]} liters')
- some_cars[car]['fuel'] = 75
- else:
- print(f'{car} refueled with {fuel} liters')
- some_cars[car]['fuel'] += fuel
- return some_cars
- def revert_func(some_details, some_cars):
- car = some_details[1]
- kilometers = int(some_details[2])
- some_cars[car]['mileage'] -= kilometers
- if some_cars[car]['mileage'] < 10000:
- some_cars[car]['mileage'] = 10000
- else:
- print(f'{car} mileage decreased by {kilometers} kilometers')
- return some_cars
- def final_print_func(some_cars):
- for car in some_cars:
- print(f"{car} -> Mileage: {some_cars[car]['mileage']} kms, Fuel in the tank: {some_cars[car]['fuel']} lt.")
- n = int(input())
- my_cars = create_car_dict_func(n)
- command = input()
- while not command == 'Stop':
- details = command.split(" : ")
- if details[0] == 'Drive':
- my_cars = drive_func(details, my_cars)
- elif details[0] == 'Refuel':
- my_cars = refuel_func(details, my_cars)
- elif details[0] == 'Revert':
- my_cars = revert_func(details, my_cars)
- command = input()
- final_print_func(my_cars)
- ==============================================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement