Skip to content

Commit 228dd10

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 2f5f159 commit 228dd10

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

scheduling/shortest_job_first.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ def calculate_average_times(
9595
return avg_wait, avg_turn
9696

9797

98-
def plot_gantt_chart(timeline: list[tuple[int, int, int]], processes: list[int]) -> None:
98+
def plot_gantt_chart(
99+
timeline: list[tuple[int, int, int]], processes: list[int]
100+
) -> None:
99101
"""Plot a Gantt chart for process execution."""
100102
fig, ax = plt.subplots(figsize=(10, 2))
101103
colors = plt.cm.tab10.colors # Nice color set
@@ -108,14 +110,24 @@ def plot_gantt_chart(timeline: list[tuple[int, int, int]], processes: list[int])
108110
edgecolor="black",
109111
label=f"P{processes[pid]}",
110112
)
111-
ax.text((start + end) / 2, 0, f"P{processes[pid]}", ha="center", va="center", color="white", fontsize=9)
113+
ax.text(
114+
(start + end) / 2,
115+
0,
116+
f"P{processes[pid]}",
117+
ha="center",
118+
va="center",
119+
color="white",
120+
fontsize=9,
121+
)
112122

113123
ax.set_xlabel("Time")
114124
ax.set_yticks([])
115125
ax.set_title("Gantt Chart - Shortest Job Remaining First (SJF Preemptive)")
116126
handles, labels = plt.gca().get_legend_handles_labels()
117127
by_label = dict(zip(labels, handles))
118-
ax.legend(by_label.values(), by_label.keys(), bbox_to_anchor=(1.05, 1), loc="upper left")
128+
ax.legend(
129+
by_label.values(), by_label.keys(), bbox_to_anchor=(1.05, 1), loc="upper left"
130+
)
119131
plt.tight_layout()
120132
plt.show()
121133

@@ -132,14 +144,24 @@ def plot_gantt_chart(timeline: list[tuple[int, int, int]], processes: list[int])
132144
print(f"Enter the arrival time and burst time for process {i + 1}:")
133145
arrival_time[i], burst_time[i] = map(int, input().split())
134146

135-
waiting_time, timeline = calculate_waitingtime(arrival_time, burst_time, no_of_processes)
136-
turn_around_time = calculate_turnaroundtime(burst_time, no_of_processes, waiting_time)
147+
waiting_time, timeline = calculate_waitingtime(
148+
arrival_time, burst_time, no_of_processes
149+
)
150+
turn_around_time = calculate_turnaroundtime(
151+
burst_time, no_of_processes, waiting_time
152+
)
137153
calculate_average_times(waiting_time, turn_around_time, no_of_processes)
138154

139155
# Display results table
140156
df = pd.DataFrame(
141157
list(zip(processes, arrival_time, burst_time, waiting_time, turn_around_time)),
142-
columns=["Process", "ArrivalTime", "BurstTime", "WaitingTime", "TurnAroundTime"],
158+
columns=[
159+
"Process",
160+
"ArrivalTime",
161+
"BurstTime",
162+
"WaitingTime",
163+
"TurnAroundTime",
164+
],
143165
)
144166
pd.set_option("display.max_rows", df.shape[0] + 1)
145167
print("\n--- Process Table ---")

scheduling/shortest_remaining_time_first.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
from statistics import mean
1010

1111

12-
def calculate_waiting_times(burst_times: list[int], arrival_times: list[int]) -> list[int]:
12+
def calculate_waiting_times(
13+
burst_times: list[int], arrival_times: list[int]
14+
) -> list[int]:
1315
"""
1416
Calculate the waiting times of processes using SRTF scheduling.
1517
@@ -39,7 +41,11 @@ def calculate_waiting_times(burst_times: list[int], arrival_times: list[int]) ->
3941
while complete != n:
4042
# Find process with minimum remaining time at current time
4143
for j in range(n):
42-
if arrival_times[j] <= t and remaining_times[j] < min_remaining and remaining_times[j] > 0:
44+
if (
45+
arrival_times[j] <= t
46+
and remaining_times[j] < min_remaining
47+
and remaining_times[j] > 0
48+
):
4349
min_remaining = remaining_times[j]
4450
shortest = j
4551
check = True
@@ -95,4 +101,3 @@ def calculate_turn_around_times(
95101
)
96102
print(f"\nAverage waiting time = {mean(waiting_times):.5f}")
97103
print(f"Average turn around time = {mean(turn_around_times):.5f}")
98-

0 commit comments

Comments
 (0)