-
Notifications
You must be signed in to change notification settings - Fork 1
/
ScheduleByRatio.py
35 lines (29 loc) · 1.17 KB
/
ScheduleByRatio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
################################################################################
## Copyright(c) Arsalan Khairani, 2014 ##
## Scheduling Application ##
## ##
## Example of Greedy algorithm that determines sequence of task over shared ##
## resources ##
################################################################################
def getscore(a, b):
# ration is optimal however you can also do difference
# return a - b
return a / b
def main():
file = open("jobs.txt")
count = int(file.readline())
i = 0
res = []
while i < count:
li = [int(j) for j in file.readline().strip().split(" ")]
res.append((getscore(li[0], li[1]), li[0], li[1]))
i += 1
final = sorted(res, reverse = True)
minimum = 0
completionTime = 0
for entry in final:
completionTime += entry[2]
minimum += ( entry[1] * completionTime )
print (minimum)
if __name__ == "__main__":
main()