-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBerkley.py
More file actions
48 lines (33 loc) · 1.1 KB
/
Berkley.py
File metadata and controls
48 lines (33 loc) · 1.1 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
import time
def time_input(prompt):
time = input(prompt)
h, m = map(int, time.split(":"))
return h * 60 + m
def min_to_stamp(min):
return f"{min // 60:02d} : {min % 60:02d}"
def berkley():
n = int(input("Enter the number of node :"))
main = time_input("Enter the Main Clock time:(HH:MM): ")
nodes = []
for i in range(n):
node_time = time_input(f"Enter the time for {i} node: ")
nodes.append(node_time)
print("\nBefore Sync:")
for i in range(0, n):
print(f"Clock {i} : {min_to_stamp(nodes[i])}")
print("\nTime difference from Main: ")
diff = []
for i in range(n):
dif = nodes[i] - main
diff.append(dif)
print("\nDifference in times are: ")
for i in range(0, n):
print(f"Clock {i} : {diff[i]}")
offset = sum(diff) // (n + 1)
print(f"\nAverage Offset: {offset:+} min")
print("\nNew synchronized times:")
print(f"Main Clock: {min_to_stamp(main + offset)}")
for i in range(n):
new_t = nodes[i] + (offset - diff[i])
print(f" Clock{i}: {min_to_stamp(new_t)}")
berkley()