-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq4.py
49 lines (41 loc) · 1.29 KB
/
q4.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
import hashlib
# Shard list with partitions
# Each value is the upper limit of the shard, including the value itself
# values are written in hex with lowercase letters
shards = [ '0a000000000000000000000000000000',
'40000000000000000000000000000000',
'80000000000000000000000000000000',
'c0000000000000000000000000000000',
]
#
# Calculate on which shard the user
#
def getShard(username):
# receive username and calculate the hash
hash = hashlib.md5(username).hexdigest()
print "hash\t= %s" % hash # debug
n = 0
while(True):
# try to fit in shard n range
if(hash <= shards[n]):
return n #exit the function, returning the shard number
# if not, go to next
else:
n = n + 1
#if is the last shard
if(n == len(shards)):
return n #exit the function, returning the shard number
#
#main script to test the function
#
# usernames for test
usernames = [ 'fgh45658',
'luigi',
'Luciana',
'mario',
'someuser',
'test'
]
for username in usernames:
print "user\t= %s" % username # debug
print "shard#\t= %s\n" % getShard(username)