from statistics import mean

import numpy as np





def 		__lowerCamelCase							(UpperCAmelCase__       :							list ,	UpperCAmelCase__       :							list ,	UpperCAmelCase__       :							list ,	UpperCAmelCase__       :							int					):
					SCREAMING_SNAKE_CASE           =    0
					# Number of processes finished
					SCREAMING_SNAKE_CASE           =    0
					# Displays the finished process.
					# If it is 0, the performance is completed if it is 1, before the performance.
					SCREAMING_SNAKE_CASE           =    [0] * no_of_process
					# List to include calculation results
					SCREAMING_SNAKE_CASE           =    [0] * no_of_process

					# Sort by arrival time.
					SCREAMING_SNAKE_CASE           =    [burst_time[i] for i in np.argsort(UpperCAmelCase__					)]
					SCREAMING_SNAKE_CASE           =    [process_name[i] for i in np.argsort(UpperCAmelCase__					)]
					arrival_time.sort()

					while no_of_process > finished_process_count:
										SCREAMING_SNAKE_CASE           =    0
										while finished_process[i] == 1:
															i += 1
										if current_time < arrival_time[i]:
															SCREAMING_SNAKE_CASE           =    arrival_time[i]

										SCREAMING_SNAKE_CASE           =    0
										# Index showing the location of the process being performed
										SCREAMING_SNAKE_CASE           =    0
										# Saves the current response ratio.
										SCREAMING_SNAKE_CASE           =    0
										for i in range(0 ,	UpperCAmelCase__					):
															if finished_process[i] == 0 and arrival_time[i] <= current_time:
																				SCREAMING_SNAKE_CASE           =    (burst_time[i] + (current_time - arrival_time[i])) / burst_time[
																				    i
																				]
															if response_ratio < temp:
																				SCREAMING_SNAKE_CASE           =    temp
																				SCREAMING_SNAKE_CASE           =    i

        # Calculate the turn around time
										SCREAMING_SNAKE_CASE           =    current_time + burst_time[loc] - arrival_time[loc]
										current_time += burst_time[loc]
										# Indicates that the process has been performed.
										SCREAMING_SNAKE_CASE           =    1
										# Increase finished_process_count by 1
										finished_process_count += 1

					return turn_around_time





def 		__lowerCamelCase							(UpperCAmelCase__       :							list ,	UpperCAmelCase__       :							list ,	UpperCAmelCase__       :							list ,	UpperCAmelCase__       :							int					):
					SCREAMING_SNAKE_CASE           =    [0] * no_of_process
					for i in range(0 ,	UpperCAmelCase__					):
										SCREAMING_SNAKE_CASE           =    turn_around_time[i] - burst_time[i]
					return waiting_time


if __name__ == "__main__":
							_lowerCamelCase   :				List[Any]     			=						5
							_lowerCamelCase   :				Tuple     			=						['''A''', '''B''', '''C''', '''D''', '''E''']
							_lowerCamelCase   :				str     			=						[1, 2, 3, 4, 5]
							_lowerCamelCase   :				List[str]     			=						[1, 2, 3, 4, 5]

							_lowerCamelCase   :				List[Any]     			=						calculate_turn_around_time(
							    process_name, arrival_time, burst_time, no_of_process
							)
							_lowerCamelCase   :				Any     			=						calculate_waiting_time(
							    process_name, turn_around_time, burst_time, no_of_process
							)

							print('''Process name \tArrival time \tBurst time \tTurn around time \tWaiting time''')
							for i in range(0, no_of_process):
														print(
														    f"""{process_name[i]}\t\t{arrival_time[i]}\t\t{burst_time[i]}\t\t"""
														    f"""{turn_around_time[i]}\t\t\t{waiting_time[i]}"""
														)

							print(f"""average waiting time : {mean(waiting_time):.5f}""")
							print(f"""average turn around time : {mean(turn_around_time):.5f}""")





from random import randint, random





def 		__lowerCamelCase							(UpperCAmelCase__       :							int ,	UpperCAmelCase__       :							int ,	UpperCAmelCase__       :							int ,	UpperCAmelCase__       :							bool = False ,	UpperCAmelCase__       :							bool = False ,	UpperCAmelCase__       :							int = 5 ,	):
					SCREAMING_SNAKE_CASE           =    [[-1] * number_of_cells]  # Create a highway without any car
					SCREAMING_SNAKE_CASE           =    0
					SCREAMING_SNAKE_CASE           =    max(UpperCAmelCase__ ,	0					)
					while i < number_of_cells:
										SCREAMING_SNAKE_CASE           =    (
										    randint(0 ,	UpperCAmelCase__					) if random_speed else initial_speed
										)  # Place the cars
										i += (
										    randint(1 ,	max_speed * 2					) if random_frequency else frequency
										)  # Arbitrary number, may need tuning
					return highway





def 		__lowerCamelCase							(UpperCAmelCase__       :							list ,	UpperCAmelCase__       :							int					):
					SCREAMING_SNAKE_CASE           =    0
					SCREAMING_SNAKE_CASE           =    highway_now[car_index + 1 :]
					for cell in range(len(UpperCAmelCase__					)					):  # May need a better name for this
										if cells[cell] != -1:  # If the cell is not empty then
															return distance  # we have the distance we wanted
										distance += 1
					# Here if the car is near the end of the highway
					return distance + get_distance(UpperCAmelCase__ ,	-1					)





def 		__lowerCamelCase							(UpperCAmelCase__       :							list ,	UpperCAmelCase__       :							float ,	UpperCAmelCase__       :							int					):
					SCREAMING_SNAKE_CASE           =    len(UpperCAmelCase__					)
					# Beforce calculations, the highway is empty
					SCREAMING_SNAKE_CASE           =    [-1] * number_of_cells

					for car_index in range(UpperCAmelCase__					):
										if highway_now[car_index] != -1:
															# Add 1 to the current speed of the car and cap the speed
															SCREAMING_SNAKE_CASE           =    min(highway_now[car_index] + 1 ,	UpperCAmelCase__					)
															# Number of empty cell before the next car
															SCREAMING_SNAKE_CASE           =    get_distance(UpperCAmelCase__ ,	UpperCAmelCase__					) - 1
															# We can't have the car causing an accident
															SCREAMING_SNAKE_CASE           =    min(next_highway[car_index] ,	UpperCAmelCase__					)
															if random() < probability:
																				# Randomly, a driver will slow down
																				SCREAMING_SNAKE_CASE           =    max(next_highway[car_index] - 1 ,	0					)
					return next_highway





def 		__lowerCamelCase							(UpperCAmelCase__       :							list ,	UpperCAmelCase__       :							int ,	UpperCAmelCase__       :							float ,	UpperCAmelCase__       :							int					):
					SCREAMING_SNAKE_CASE           =    len(highway[0]					)

					for i in range(UpperCAmelCase__					):
										SCREAMING_SNAKE_CASE           =    update(highway[i] ,	UpperCAmelCase__ ,	UpperCAmelCase__					)
										SCREAMING_SNAKE_CASE           =    [-1] * number_of_cells

										for car_index in range(UpperCAmelCase__					):
															SCREAMING_SNAKE_CASE           =    next_speeds_calculated[car_index]
															if speed != -1:
																				# Change the position based on the speed (with % to create the loop)
																				SCREAMING_SNAKE_CASE           =    (car_index + speed) % number_of_cells
																				# Commit the change of position
																				SCREAMING_SNAKE_CASE           =    speed
										highway.append(UpperCAmelCase__					)

					return highway


if __name__ == "__main__":
							import doctest

							doctest.testmod()




