-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalAvgMethod.py
More file actions
52 lines (41 loc) · 1.46 KB
/
GlobalAvgMethod.py
File metadata and controls
52 lines (41 loc) · 1.46 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def to_min(time):
h, m = time.split(":")
return int(h) * 60 + int(m)
def calculate_skew(curr, agreed):
return curr - agreed
def sync_clock(avg, curr):
abs_avg = abs(avg)
if avg > 0:
return curr - abs_avg, "Decrease"
elif avg < 0:
return curr + abs_avg, "Increase"
else:
return curr, "No Change"
agreed_time = to_min(input("Enter the agreed upon time (HH:MM): "))
n = int(input("Enter the number of machines: "))
time_list = input("Enter current time of {n} machines (HH:MM): ").split()
curr_time = {}
for i in range(n):
curr_time[i + 1] = to_min(time_list[i])
print("\nAgreed Upon Time (in min): ", agreed_time)
initial_skew = []
for i in range(1, n + 1):
skew = calculate_skew(curr_time[i], agreed_time)
initial_skew.append(skew)
print(f"Machine {i}: Initial Time = {curr_time[i]} minutes, Skew = {skew} minutes")
for i in range(1, n + 1):
curr_time[i] += 5
final_skew = []
for i in range(1, n + 1):
skew = calculate_skew(curr_time[i], agreed_time)
final_skew.append(skew)
print(
f"Machine {i}: Final Time After 5 minutes = {curr_time[i]} minutes, Skew = {skew} minutes"
)
for i in range(1, n + 1):
avg_skew = (initial_skew[i - 1] + final_skew[i - 1]) / 2
print(f"Machine {i}: Average Skew = {avg_skew} minutes")
new_time, action = sync_clock(avg_skew, curr_time[i])
print(
f"Machine {i}: Current Time = {curr_time[i]} minutes, After Sync: {new_time} minutes, Action: {action}"
)