Skip to content

Commit e7041a8

Browse files
Ale3androsScclauss
andauthored
Added first come first served scheduling (TheAlgorithms#1722)
* Added FCFS * Fixed spelling error * Rename fcfs.py to first_come_first_served.py * Fixed FCFS and added tests. * Made changes requested * Use enumerate() instead of range(len()) Co-authored-by: Christian Clauss <[email protected]>
1 parent dacf1d0 commit e7041a8

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

scheduling/first_come_first_served.py

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Implementation of First Come First Served scheduling algorithm
2+
# In this Algorithm we just care about the order that the processes arrived
3+
# without carring about their duration time
4+
# https://en.wikipedia.org/wiki/Scheduling_(computing)#First_come,_first_served
5+
from typing import List
6+
7+
8+
def calculate_waiting_times(duration_times: List[int]) -> List[int]:
9+
"""
10+
This function calculates the waiting time of some processes that have a
11+
specified duration time.
12+
Return: The waiting time for each process.
13+
>>> calculate_waiting_times([5, 10, 15])
14+
[0, 5, 15]
15+
>>> calculate_waiting_times([1, 2, 3, 4, 5])
16+
[0, 1, 3, 6, 10]
17+
>>> calculate_waiting_times([10, 3])
18+
[0, 10]
19+
"""
20+
waiting_times = [0] * len(duration_times)
21+
for i in range(1, len(duration_times)):
22+
waiting_times[i] = duration_times[i - 1] + waiting_times[i - 1]
23+
return waiting_times
24+
25+
26+
def calculate_turnaround_times(
27+
duration_times: List[int], waiting_times: List[int]
28+
) -> List[int]:
29+
"""
30+
This function calculates the turnaround time of some processes.
31+
Return: The time difference between the completion time and the
32+
arrival time.
33+
Practically waiting_time + duration_time
34+
>>> calculate_turnaround_times([5, 10, 15], [0, 5, 15])
35+
[5, 15, 30]
36+
>>> calculate_turnaround_times([1, 2, 3, 4, 5], [0, 1, 3, 6, 10])
37+
[1, 3, 6, 10, 15]
38+
>>> calculate_turnaround_times([10, 3], [0, 10])
39+
[10, 13]
40+
"""
41+
return [duration_time + waiting_times[i] for i, duration_time in enumerate(duration_times)]
42+
43+
44+
def calculate_average_turnaround_time(turnaround_times: List[int]) -> float:
45+
"""
46+
This function calculates the average of the turnaround times
47+
Return: The average of the turnaround times.
48+
>>> calculate_average_turnaround_time([0, 5, 16])
49+
7.0
50+
>>> calculate_average_turnaround_time([1, 5, 8, 12])
51+
6.5
52+
>>> calculate_average_turnaround_time([10, 24])
53+
17.0
54+
"""
55+
return sum(turnaround_times) / len(turnaround_times)
56+
57+
58+
def calculate_average_waiting_time(waiting_times: List[int]) -> float:
59+
"""
60+
This function calculates the average of the waiting times
61+
Return: The average of the waiting times.
62+
>>> calculate_average_waiting_time([0, 5, 16])
63+
7.0
64+
>>> calculate_average_waiting_time([1, 5, 8, 12])
65+
6.5
66+
>>> calculate_average_waiting_time([10, 24])
67+
17.0
68+
"""
69+
return sum(waiting_times) / len(waiting_times)
70+
71+
72+
if __name__ == "__main__":
73+
# process id's
74+
processes = [1, 2, 3]
75+
76+
# ensure that we actually have processes
77+
if len(processes) == 0:
78+
print("Zero amount of processes")
79+
exit()
80+
81+
# duration time of all processes
82+
duration_times = [19, 8, 9]
83+
84+
# ensure we can match each id to a duration time
85+
if len(duration_times) != len(processes):
86+
print("Unable to match all id's with their duration time")
87+
exit()
88+
89+
# get the waiting times and the turnaround times
90+
waiting_times = calculate_waiting_times(duration_times)
91+
turnaround_times = calculate_turnaround_times(duration_times, waiting_times)
92+
93+
# get the average times
94+
average_waiting_time = calculate_average_waiting_time(waiting_times)
95+
average_turnaround_time = calculate_average_turnaround_time(turnaround_times)
96+
97+
# print all the results
98+
print("Process ID\tDuration Time\tWaiting Time\tTurnaround Time")
99+
for i, process in enumerate(processes):
100+
print(
101+
f"{process}\t\t{duration_times[i]}\t\t{waiting_times[i]}\t\t{turnaround_times[i]}"
102+
)
103+
print(f"Average waiting time = {average_waiting_time}")
104+
print(f"Average turn around time = {average_turnaround_time}")

0 commit comments

Comments
 (0)