-
Notifications
You must be signed in to change notification settings - Fork 0
/
dailycoding059.py
70 lines (51 loc) · 1.64 KB
/
dailycoding059.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
This problem was asked by Google.
Implement a file syncing algorithm for two computers over a low-bandwidth
network. What if we know the files in the two computers are mostly the same?
solution:
https://github.com/vineetjohn/daily-coding-problem/blob/master/solutions/problem_059.py
Idk this problem is gross.
"""
import hashlib
m = hashlib.md5
class MerkleNode:
def __init__(self):
self.parent = None
self.node_hash = None
class MerkleDirectory(MerkleNode):
def __init__(self):
MerkleNode.__init__(self)
self.children = list()
self.is_dir = True
def recalculate_hash(self):
if self.children:
collated_hash = ""
for child in self.children:
collated_hash += child.node_hash
self.node_hash = m(collated_hash.encode()).hexdigest()
class MerkleFile(MerkleNode):
def __init__(self):
MerkleNode.__init__(self)
self.node_contents = None
self.is_dir = False
def update_contents(self, new_contents):
self.node_hash = m(new_contents.encode()).hexdigest()
self.node_contents = new_contents
if self.parent:
self.parent.recalculate_hash()
def add_to_directory(self, dir_node):
self.parent = dir_node
dir_node.children.append(self)
while dir_node:
dir_node.recalculate_hash()
dir_node = dir_node.parent
a_1 = MerkleFile()
b_1 = MerkleDirectory()
a_1.update_contents("abc")
a_1.add_to_directory(b_1)
a_1.update_contents("abcd")
a_2 = MerkleFile()
b_2 = MerkleDirectory()
a_2.update_contents("abc")
a_2.add_to_directory(b_2)
print("Passed")