fromhelperimportdouble_sha256, merkle_parent_levelhex_hashes= [
'42f6f52f17620653dcc909e58bb352e0bd4bd1381e2955d19c00959a22122b2e',
'94c3af34b9667bf787e1c6a0a009201589755d01d02fe2877cc69b929d2418d4',
'959428d7c48113cb9149d0566bde3d46e98cf028053c522b8fa8f735241aa953',
'a9f27b99d5d108dede755710d4a1ffa2c74af70b4ca71726fa57d68454e609a2',
'62af110031e29de1efcad103b3ad4bec7bdcf6cb9c9f4afdd586981795516577',
'766900590ece194667e9da2984018057512887110bf54fe0aa800157aec796ba',
'e8270fb475763bc8d855cfe45ed98060988c1bdcad2ffc8364f783c98999a208',
'921b8cfd3e14bf41f028f0a3aa88c813d5039a2b1bceb12208535b0b43a5d09e',
'15535864799652347cec66cba473f6d8291541238e58b2e03b046bc53cfe1321',
'1c8af7c502971e67096456eac9cd5407aacf62190fc54188995666a30faf99f0',
'3311f8acc57e8a3e9b68e2945fb4f53c07b0fa4668a7e5cda6255c21558c774d',
]
# bytes.fromhex to get all the hashes in binary# initialize current level to be the hashes# loop until current_level has only 1 element# make the current level the parent level# print the root's hex
Test Driven Exercise
defmerkle_root(hashes):
'''Takes a list of binary hashes and returns the merkle root '''# current level starts as hashes# loop until there's exactly 1 element# current level becomes the merkle parent level# return the 1st item of current_levelpass
merkle-root-jsong-programming-blockchain-demo's People