highest response ratio next hrrn scheduling is a nonpreemptive discipline it was developed as modification of shortest job next or shortest job first sjn or sjf to mitigate the problem of process starvation https en wikipedia orgwikihighestresponserationext calculate the turn around time of each processes return the turn around time time for each process calculateturnaroundtimea b c 3 5 8 2 4 6 3 2 4 7 calculateturnaroundtimea b c 0 2 4 3 5 7 3 3 6 11 number of processes finished displays the finished process if it is 0 the performance is completed if it is 1 before the performance list to include calculation results sort by arrival time if the current time is less than the arrival time of the process that arrives first among the processes that have not been performed change the current time index showing the location of the process being performed saves the current response ratio calculate the turn around time indicates that the process has been performed increase finishedprocesscount by 1 calculate the waiting time of each processes return the waiting time for each process calculatewaitingtimea b c 2 4 7 2 4 6 3 0 0 1 calculatewaitingtimea b c 3 6 11 3 5 7 3 0 1 4 calculate the turn around time of each processes return the turn around time time for each process calculate_turn_around_time a b c 3 5 8 2 4 6 3 2 4 7 calculate_turn_around_time a b c 0 2 4 3 5 7 3 3 6 11 number of processes finished displays the finished process if it is 0 the performance is completed if it is 1 before the performance list to include calculation results sort by arrival time if the current time is less than the arrival time of the process that arrives first among the processes that have not been performed change the current time index showing the location of the process being performed saves the current response ratio calculate the turn around time indicates that the process has been performed increase finished_process_count by 1 calculate the waiting time of each processes return the waiting time for each process calculate_waiting_time a b c 2 4 7 2 4 6 3 0 0 1 calculate_waiting_time a b c 3 6 11 3 5 7 3 0 1 4
from statistics import mean

import numpy as np


def calculate_turn_around_time(
    process_name: list, arrival_time: list, burst_time: list, no_of_process: int
) -> list:
    

    current_time = 0
    
    finished_process_count = 0
    
    
    finished_process = [0] * no_of_process
    
    turn_around_time = [0] * no_of_process

    
    burst_time = [burst_time[i] for i in np.argsort(arrival_time)]
    process_name = [process_name[i] for i in np.argsort(arrival_time)]
    arrival_time.sort()

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

        response_ratio = 0
        
        loc = 0
        
        temp = 0
        for i in range(no_of_process):
            if finished_process[i] == 0 and arrival_time[i] <= current_time:
                temp = (burst_time[i] + (current_time - arrival_time[i])) / burst_time[
                    i
                ]
            if response_ratio < temp:
                response_ratio = temp
                loc = i

        
        turn_around_time[loc] = current_time + burst_time[loc] - arrival_time[loc]
        current_time += burst_time[loc]
        
        finished_process[loc] = 1
        
        finished_process_count += 1

    return turn_around_time


def calculate_waiting_time(
    process_name: list, turn_around_time: list, burst_time: list, no_of_process: int
) -> list:
    

    waiting_time = [0] * no_of_process
    for i in range(no_of_process):
        waiting_time[i] = turn_around_time[i] - burst_time[i]
    return waiting_time


if __name__ == "__main__":
    no_of_process = 5
    process_name = ["A", "B", "C", "D", "E"]
    arrival_time = [1, 2, 3, 4, 5]
    burst_time = [1, 2, 3, 4, 5]

    turn_around_time = calculate_turn_around_time(
        process_name, arrival_time, burst_time, no_of_process
    )
    waiting_time = 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(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}")