Created
July 14, 2011 17:17
-
-
Save whs/1082924 to your computer and use it in GitHub Desktop.
HLP Hackathon r1-3
This file contains hidden or 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
| from __future__ import division | |
| import datetime, hashlib, math | |
| inp = open("file1325.txt").read().strip() | |
| # split to sections | |
| inp = inp.split("\r\n\r\n") | |
| timeRate = [] | |
| inp[0] = inp[0].split("\r\n") | |
| for x,i in enumerate(inp[0]): | |
| i = i.split(" ") | |
| if x+1 < len(inp[0]): | |
| t = inp[0][x+1].split(" ")[0].split(":") | |
| ends = datetime.time(int(t[0]), int(t[1])) | |
| else: | |
| ends = datetime.time(23, 59) | |
| t = i[0].split(":") | |
| timeRate.append((datetime.time(int(t[0]), int(t[1])), ends, float(i[1]), float(i[2]))) | |
| timeRate = timeRate[::-1] #reverse for faster scan | |
| #print timeRate | |
| # special numbers | |
| spec = {} | |
| inp[1] = inp[1].split("\r\n") | |
| for i in inp[1]: | |
| i = i.split(" ") | |
| if i[0] not in spec: | |
| spec[i[0]] = {} | |
| spec[i[0]][i[1]] = (float(i[2]), float(i[3])) | |
| #print spec | |
| # index the call logs | |
| inp[2] = inp[2].split("\r\n") | |
| callLog = {} | |
| callHashing = {} | |
| for i in inp[2]: | |
| i = i.split(" ") | |
| hash = hashlib.sha1(i[2]+i[3]).hexdigest() | |
| if (i[4] == "C" and hash in callLog) or (i[2] in callHashing and i[3] in callHashing[i[2]]): | |
| if i[2] not in callHashing: | |
| callHashing[i[2]] = {} | |
| if i[3] not in callHashing[i[2]]: | |
| callHashing[i[2]][i[3]] = 0 | |
| if i[4] == "C": | |
| callHashing[i[2]][i[3]] += 1 | |
| hash = hashlib.sha1(i[2]+i[3]+str(callHashing[i[2]][i[3]])).hexdigest() | |
| if hash not in callLog: | |
| callLog[hash] = { | |
| "from": i[2], | |
| "to": i[3], | |
| "call": datetime.datetime.strptime(i[0]+" "+i[1], "%Y-%m-%d %H:%M:%S") | |
| } | |
| if i[4] == "H": | |
| if 'hangup' in callLog[hash]: | |
| raise Exception, "already hang up" | |
| callLog[hash]['hangup'] = datetime.datetime.strptime(i[0]+" "+i[1], "%Y-%m-%d %H:%M:%S") | |
| callLog[hash]['duration'] = callLog[hash]['hangup'] - callLog[hash]['call'] | |
| #print callLog | |
| # then process the cost | |
| totalPay = 0.0 | |
| for hash,i in callLog.iteritems(): | |
| # 1: calculate the time called | |
| duration = int(math.ceil(i['duration'].seconds/60)) | |
| # int(math.ceil((i['duration'].days*24*60 | |
| if i['duration'].days > 0: | |
| raise Exception, "very long call" | |
| # 2: calculate the rate used | |
| # 2.1: friend rate | |
| startRate = -1 | |
| minRate = -1 | |
| if i['from'] in spec and i['to'] in spec[i['from']]: | |
| startRate = spec[i['from']][i['to']][0] | |
| minRate = spec[i['from']][i['to']][1] | |
| else: | |
| # 2.2: time rate | |
| for x in timeRate: | |
| if x[0] <= i['call'].time(): #and x[1] > i['call'].time(): | |
| startRate = x[2] | |
| minRate = x[3] | |
| break | |
| if startRate == -1 or minRate == -1: | |
| raise Exception, "can't find rate for "+`i` | |
| if "hangup" not in i: | |
| raise Exception, "pending call "+`i` | |
| # call must be already initialized for duration to be calculated | |
| # 3: calculate | |
| payment = startRate | |
| duration -= 3 | |
| duration = max(duration, 0) | |
| payment += minRate * duration | |
| totalPay += payment | |
| print totalPay |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment