Advertisement
Lyubohd

03. Need for Speed III

Jul 30th, 2020
2,346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.89 KB | None | 0 0
  1. n = int(input())
  2.  
  3. cars = {}
  4.  
  5. for i in range(n):
  6.     data = input().split("|")
  7.     car = data[0]
  8.     mileage = int(data[1])
  9.     fuel = int(data[2])
  10.  
  11.     cars[car] = {}
  12.     cars[car]["mileage"] = mileage
  13.     cars[car]["fuel"] = fuel
  14.  
  15. input_command = input()
  16. while "Stop" != input_command:
  17.     tokens = input_command.split(" : ")
  18.     command = tokens[0]
  19.     car = tokens[1]
  20.  
  21.     if "Drive" == command:
  22.         distance = int(tokens[2])
  23.         fuel = int(tokens[3])
  24.         if cars[car]["fuel"] < fuel:
  25.             print("Not enough fuel to make that ride")
  26.         else:
  27.             cars[car]["fuel"] -= fuel
  28.             cars[car]["mileage"] += distance
  29.             print(f"{car} driven for {distance} kilometers. {fuel} liters of fuel consumed.")
  30.             if cars[car]["mileage"] >= 100000:
  31.                 print(f"Time to sell the {car}!")
  32.                 del cars[car]
  33.     elif "Refuel" == command:
  34.         fuel = int(tokens[2])
  35.         current_fuel = cars[car]["fuel"]
  36.         fuel_to_fill = 0
  37.         if current_fuel + fuel <= 75:
  38.             cars[car]["fuel"] += fuel
  39.             fuel_to_fill = fuel
  40.         else:
  41.             fuel_to_fill = 75 - cars[car]["fuel"]
  42.             cars[car]["fuel"] = 75
  43.         print(f"{car} refueled with {fuel_to_fill} liters")
  44.     elif "Revert" == command:
  45.         distance_to_revert = int(tokens[2])
  46.         if cars[car]["mileage"] - distance_to_revert < 10000:
  47.             cars[car]["mileage"] = 10000
  48.         else:
  49.             cars[car]["mileage"] -= distance_to_revert
  50.             print(f"{car} mileage decreased by {distance_to_revert} kilometers")
  51.  
  52.     input_command = input()
  53.  
  54. sorted_cars = sorted(cars.keys(), key=lambda c: (-cars[c]["mileage"], c))
  55. for x in sorted_cars:
  56.     print(f"{x} -> Mileage: {cars[x]['mileage']} kms, Fuel in the tank: {cars[x]['fuel']} lt.")
  57. # "{car} -> Mileage: {mileage} kms, Fuel in the tank: {fuel} lt."
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement