Created
March 26, 2017 11:52
-
-
Save s-c-p/b70c9203cc977537b1df181c5ed13efe to your computer and use it in GitHub Desktop.
An algorithm that assigns unique (values) hashes to all dates in an year.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time, json | |
big = ["January", "March", "May", "July", "August", "October", "December"] | |
small = ["April", "June", "September", "November"] | |
class DateHasher(object): | |
""" duh... """ | |
def __init__(self, dayMultiplier=12, monthMultiplier=31): | |
self.dayMultiplier = dayMultiplier | |
self.monthMultiplier = monthMultiplier | |
return | |
def calcHash(self, dayNum, monthNum): | |
dateHash = self.monthMultiplier*monthNum + self.dayMultiplier*dayNum | |
return dateHash | |
def numOfDaysIn(monthName): | |
if monthName in big: | |
return 31 | |
elif monthName == "February": | |
return 29 | |
else: | |
return 30 | |
def shortcutDo(): | |
hashes = list() | |
months = big+small+["February"] | |
getDateHash = DateHasher().calcHash | |
for aMonth in months: | |
monthNum = time.strptime(aMonth, "%B").tm_mon | |
for i in range(1, numOfDaysIn(aMonth)+1): | |
hashes.append( | |
getDateHash(i, monthNum) | |
) | |
return len(set(hashes)) | |
# dateHash = 29*m + 12*d | |
# dateHash = 17*m + 57*d | |
# not working | |
# dateHash = 3*m + 2*d | |
# dateHash = 29*m + 11*d | |
# dateHash = 3*m + 12*d | |
def do(): | |
hashes = dict() | |
getDateHash = DateHasher().calcHash | |
months = big+small+["February"] | |
for aMonth in months: | |
monthNum = time.strptime(aMonth, "%B").tm_mon | |
for i in range(1, numOfDaysIn(aMonth)+1): | |
dh = getDateHash(i, monthNum) | |
if dh in hashes.keys(): | |
raise ValueError("\n\n\tHash Collision at: {}, {}".format(aMonth, i)) | |
else: | |
hashes[dh] = "%(aMonth)s, %(i)d" % locals() | |
# # February | |
# for i in range(1, 29+1): | |
# dh = getDateHash(dayNum=i, monthNum=2) | |
# if dh in hashes.keys(): | |
# raise ValueError("\n\n\tHash Collision at: February, "+str(dayNum)) | |
# else: | |
# hashes[dh] = "February, %(i)d" % locals() | |
# # 31 day months | |
# for aMonth in big: | |
# monthNum = time.strptime(aMonth, "%B").tm_mon | |
# for i in range(1, 31+1): | |
# dh = getDateHash(i, monthNum) | |
# if dh in hashes.keys(): | |
# raise ValueError("\n\n\tHash Collision at: {}, {}".format(aMonth, i)) | |
# else: | |
# hashes[dh] = "%(aMonth)s, %(i)d" % locals() | |
# # 30 day months | |
# for aMonth in small: | |
# monthNum = time.strptime(aMonth, "%B").tm_mon | |
# for i in range(1, 30+1): | |
# dh = getDateHash(i, monthNum) | |
# if dh in hashes.keys(): | |
# raise ValueError("\n\n\tHash Collision at: {}, {}".format(aMonth, i)) | |
# else: | |
# hashes[dh] = "%(aMonth)s, %(i)d" % locals() | |
return hashes | |
def main(): | |
print(shortcutDo()) | |
# return | |
ans = do() | |
# print(len(ans)) | |
print(json.dumps(ans, sort_keys=True, indent=4)) | |
return | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment