Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Hi, can this be done without dictionary? # importing heap... import heapq # reading from inputPS11.txt f = open(inputPS11.txt, r) # readline() for reading a
Hi, can this be done without dictionary?
# importing heap... import heapq # reading from inputPS11.txt f = open("inputPS11.txt", "r") # readline() for reading a line... # storing each line in time_intervals... time_intervals = f.readline() # car_dict to store whether the date is for start interval date or end interval date... car_dict = {} # dates to store all the interval dates... dates = [] # while loop for reading line by line... while time_intervals!="": # print(file_line) # start_date and end_date to fetch starting date and ending date from time_intervals in YYYY-MM-DD format... start_date = time_intervals[1:-1:].split(", ")[0] end_date = time_intervals[1:-1:].split(", ")[1] # storing in dictionary that date is for start or end... car_dict[start_date] = "S" car_dict[end_date] = "E" # adding all dates in list of dates... dates.append(start_date) dates.append(end_date) # use realine() to read next line time_intervals = f.readline() # closing the file... f.close() # heapify list of dates... heapq.heapify(dates) # heapified dates... # print("Heapified Dates:-> ", dates) # count to store number of cars needed at a time... count = 0 # max_count to store maximum value of count... # this max_count is the minimum number of Cars that the Car Rental Company needs to rent # such that all customers will always have access to a car when they need one... max_count = 0 # while loop for counting number of cars needed at a time... # storing maximum value of count in max_count... while len(dates)>0: d = heapq.heappop(dates) if car_dict[d]=="S": count += 1 else: count -= 1 max_count = max(max_count, count) print("Minimum numbers of cars needed:-> " + str(max_count)) # opening outputPS11.txt for writing... f = open("outputPS11.txt", "w") # writing 1st line... f.write("Cars - " + str(max_count) + " ") # end of file... f.write(" ") # closing the file... f.close()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started