(SOLVED) python or C++

Discipline: IT

Type of Paper: Question-Answer

Academic Level: Undergrad. (yrs 3-4)

Paper Format: APA

Pages: 1 Words: 275

Question

Busy intersection There is a busy intersection between two one-way streets: Main Street and 1st Avenue. Cars passing through the intersection can only pass through one at a time. When multiple cars arrive at the intersection at the same time, two queues can build up - one for each street. Cars are added to the queue in the order at which they arrive. Unfortunately, there is no traffic light to help control traffic when multiple cars arrive at the same time. So, the local residents have devised their own system for determining which car has priority to pass through the intersection: - If in the previous second, no car passed through the intersection, then the first car in the queue for 1 st Avenue goes first. - If in the previous second, a car passed through the intersection on 1st Avenue, then the first car in the queue for Avenue goes first. - If in the previous second, a car passed through the intersection on Main Street, then the first car in the queue for Main Street goest first. Passing through the intersection takes 1 second. For each car, find the time when they will pass through the intersection. Function Description Complete the function getResult in the editor below. getResult has the following parameters: int arrival [n]: an array of integers where the value at index is the time in seconds when the car arrives at the intersection. If arrival[i] arrival[j] and , then car arrives before car . int street[n]: an array of integers where the value at index is the street on which the car is traveling: Street and 1 for 1 st Avenue. Returns: an array of integers where the value at index is the time when the car will pass through the intersection Constraints - - for - arrival[i] arrival[i for - street for

2. Busy intersection
There is a busy intersection between two one-way streets: Main Street and 1st Avenue. Cars passing throu

Expert Answer
Ans: python code for the given instructions:


def getResult(arrival, street):
# Initialize variables for current time and last car's street
curr_time = 0
last_street = -1
n = len(arrival)
# Create a list to store the passing times for each car
passing_times = [0] * n
# Iterate through each car's arrival time and street
for i in range(n):
# If this is the first car or the last car was on the other street
if i == 0 or last_street != street[i]:
curr_time = arrival[i] + 1
# If the last car was on the same street
else:
curr_time = max(curr_time + 1, arrival[i])
passing_times[i] = curr_time
last_street = street[i]
return passing_times

arv = [1,2,4,5]
srt = [2,4,5,6]
print(getResult(arv, srt))

  • Explanation for step 1
Python code that can be used to solve the problem described:
Step 2/2
Output of the code is:

[2, 3, 5, 6]