Skip to content

Commit 121abce

Browse files
authored
Format code with psf/black
1 parent f35650b commit 121abce

File tree

1 file changed

+91
-84
lines changed

1 file changed

+91
-84
lines changed
+91-84
Original file line numberDiff line numberDiff line change
@@ -1,142 +1,149 @@
1-
2-
3-
# Shortest job remainig first
4-
# PLease note that arriaval time and burst
5-
#time are inputted as space separated
1+
"""
2+
Shortest job remainig first
3+
Please note arrival time and burst
4+
Please use spaces to separate times entered.
5+
"""
66

77
import pandas as pd
88
from typing import List
9-
9+
1010

1111
def calculate_waitingtime(
12-
arrival_time: List[int],burst_time: List[int],no_of_processes: int
12+
arrival_time: List[int], burst_time: List[int], no_of_processes: int
1313
) -> List[int]:
14-
14+
1515
"""
16-
This function calculates the Waiting Times of each Processes
17-
Return: list of Waiting Time.
16+
Calculate the waiting time of each processes
17+
Return: list of waiting times.
1818
>>> calculate_waitingtime([1,2,3,4],[3,3,5,1],4)
1919
[0, 3, 5, 0]
2020
>>> calculate_waitingtime([1,2,3],[2,5,1],3)
2121
[0, 2, 0]
2222
>>> calculate_waitingtime([2,3],[5,1],2)
2323
[1, 0]
2424
"""
25-
remaining_time=[0]*no_of_processes
26-
waiting_time=[0]*no_of_processes
27-
# Copy the burst time into remaining_time[]
28-
for i in range(no_of_processes):
29-
remaining_time[i] =burst_time[i]
30-
25+
remaining_time = [0] * no_of_processes
26+
waiting_time = [0] * no_of_processes
27+
# Copy the burst time into remaining_time[]
28+
for i in range(no_of_processes):
29+
remaining_time[i] = burst_time[i]
30+
3131
complete = 0
3232
increment_time = 0
3333
minm = 999999999
3434
short = 0
3535
check = False
3636

37-
# Process until all processes gets
38-
# completed
37+
# Process until all processes are completed
3938
while complete != no_of_processes:
4039
for j in range(no_of_processes):
41-
if arrival_time[j]<=increment_time:
42-
if remaining_time[j]>0:
43-
if remaining_time[j]<minm:
44-
minm=remaining_time[j]
45-
short=j
46-
check=True
47-
48-
if check == False:
40+
if arrival_time[j] <= increment_time:
41+
if remaining_time[j] > 0:
42+
if remaining_time[j] < minm:
43+
minm = remaining_time[j]
44+
short = j
45+
check = True
46+
47+
if not check:
4948
increment_time += 1
5049
continue
51-
remaining_time[short]-=1
52-
53-
minm = remaining_time[short]
54-
if minm == 0:
50+
remaining_time[short] -= 1
51+
52+
minm = remaining_time[short]
53+
if minm == 0:
5554
minm = 999999999
56-
57-
if remaining_time[short] == 0:
55+
56+
if remaining_time[short] == 0:
5857
complete += 1
5958
check = False
6059

61-
# Find finish time of current
62-
# process
60+
# Find finish time of current process
6361
finish_time = increment_time + 1
6462

65-
# Calculate waiting time
66-
finar=finish_time - arrival_time[short]
67-
waiting_time[short] = (finar - burst_time[short])
63+
# Calculate waiting time
64+
finar = finish_time - arrival_time[short]
65+
waiting_time[short] = finar - burst_time[short]
6866

69-
if waiting_time[short] < 0:
67+
if waiting_time[short] < 0:
7068
waiting_time[short] = 0
71-
72-
# Increment time
69+
70+
# Increment time
7371
increment_time += 1
7472
return waiting_time
73+
74+
7575
def calculate_turnaroundtime(
7676
burst_time: List[int], no_of_processes: int, waiting_time: List[int]
77-
) -> List[int]:
77+
) -> List[int]:
7878
"""
79-
This function calculates the Turn Around Times of each Processes
80-
Return: list of Turn Around Time.
79+
Calculate the turn around time of each Processes
80+
Return: list of turn around times.
8181
>>> calculate_turnaroundtime([3,3,5,1], 4, [0,3,5,0])
8282
[3, 6, 10, 1]
8383
>>> calculate_turnaroundtime([3,3], 2, [0,3])
8484
[3, 6]
8585
>>> calculate_turnaroundtime([8,10,1], 3, [1,0,3])
8686
[9, 10, 4]
8787
"""
88-
turn_around_time=[0]*no_of_processes
89-
for i in range(no_of_processes):
90-
turn_around_time[i] = burst_time[i] + waiting_time[i]
88+
turn_around_time = [0] * no_of_processes
89+
for i in range(no_of_processes):
90+
turn_around_time[i] = burst_time[i] + waiting_time[i]
9191
return turn_around_time
92+
93+
9294
def calculate_average_times(
93-
waiting_time: List[int],turn_around_time: List[int], no_of_processes: int
94-
):
95+
waiting_time: List[int], turn_around_time: List[int], no_of_processes: int
96+
):
9597
"""
9698
This function calculates the average of the waiting & turnaround times
9799
Prints: Average Waiting time & Average Turn Around Time
98100
>>> calculate_average_times([0,3,5,0],[3,6,10,1],4)
99-
Average waiting time = 2.00000
100-
Average turn around time = 5.0
101+
Average waiting time = 2.00000
102+
Average turn around time = 5.0
101103
>>> calculate_average_times([2,3],[3,6],2)
102-
Average waiting time = 2.50000
103-
Average turn around time = 4.5
104+
Average waiting time = 2.50000
105+
Average turn around time = 4.5
104106
>>> calculate_average_times([10,4,3],[2,7,6],3)
105-
Average waiting time = 5.66667
106-
Average turn around time = 5.0
107+
Average waiting time = 5.66667
108+
Average turn around time = 5.0
107109
"""
108110
total_waiting_time = 0
109111
total_turn_around_time = 0
110-
for i in range(no_of_processes):
111-
total_waiting_time = total_waiting_time + waiting_time[i]
112-
total_turn_around_time = total_turn_around_time + turn_around_time[i]
113-
print("Average waiting time = %.5f "%(
114-
total_waiting_time /no_of_processes) )
115-
print("Average turn around time = ",
116-
total_turn_around_time / no_of_processes)
117-
112+
for i in range(no_of_processes):
113+
total_waiting_time = total_waiting_time + waiting_time[i]
114+
total_turn_around_time = total_turn_around_time + turn_around_time[i]
115+
print("Average waiting time = %.5f" % (total_waiting_time / no_of_processes))
116+
print("Average turn around time =", total_turn_around_time / no_of_processes)
117+
118+
118119
if __name__ == "__main__":
119-
120-
print("Enter How Many Process You want To Enter")
121-
no_of_processes=int(input())
122-
burst_time=[0]*no_of_processes
123-
arrival_time=[0]*no_of_processes
124-
processes=list(range(1,no_of_processes+1))
120+
print("Enter how many process you want to analyze")
121+
no_of_processes = int(input())
122+
burst_time = [0] * no_of_processes
123+
arrival_time = [0] * no_of_processes
124+
processes = list(range(1, no_of_processes + 1))
125125

126126
for i in range(no_of_processes):
127-
print("Enter The Arrival time and Brust time for Process:--"+str(i+1))
128-
arrival_time[i], burst_time[i]=map(int,input().split())
129-
waiting_time=calculate_waitingtime(arrival_time,burst_time,no_of_processes)
130-
bt=burst_time
131-
n=no_of_processes
132-
wt=waiting_time
133-
turn_around_time=calculate_turnaroundtime(bt,n,wt)
134-
calculate_average_times(waiting_time,turn_around_time, no_of_processes)
135-
processes=list(range(1,no_of_processes+1))
136-
fcfs=pd.DataFrame(list(zip(processes,burst_time,
137-
arrival_time,waiting_time,turn_around_time)),
138-
columns=['Process','BurstTime','ArrivalTime','WaitingTime','TurnAroundTime'])
139-
140-
# Printing the dataFrame
141-
pd.set_option('display.max_rows', fcfs.shape[0]+1)
142-
print(fcfs)
127+
print("Enter the arrival time and brust time for process:--" + str(i + 1))
128+
arrival_time[i], burst_time[i] = map(int, input().split())
129+
waiting_time = calculate_waitingtime(arrival_time, burst_time, no_of_processes)
130+
bt = burst_time
131+
n = no_of_processes
132+
wt = waiting_time
133+
turn_around_time = calculate_turnaroundtime(bt, n, wt)
134+
calculate_average_times(waiting_time, turn_around_time, no_of_processes)
135+
processes = list(range(1, no_of_processes + 1))
136+
fcfs = pd.DataFrame(
137+
list(zip(processes, burst_time, arrival_time, waiting_time, turn_around_time)),
138+
columns=[
139+
"Process",
140+
"BurstTime",
141+
"ArrivalTime",
142+
"WaitingTime",
143+
"TurnAroundTime",
144+
],
145+
)
146+
147+
# Printing the dataFrame
148+
pd.set_option("display.max_rows", fcfs.shape[0] + 1)
149+
print(fcfs)

0 commit comments

Comments
 (0)