Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # input in format hh:mm:ss,nnn-nnn-nnn
- inputfile = open("phonebill.txt")
- allcalls = []
- maxduration = 0
- maxphone=0
- sumcost = 0
- def calcCost(duration):
- # if the call was shorter than 5 min: 3 cents per second
- # 5 min = 300 seconds
- if duration < 300:
- cost = 3 * duration
- # if the call was longer than 5 min: 150 cents for every started minute
- # per example, if it's exactly 5 min, it's not started the sixth
- elif duration >= 300:
- cost = (hours * 60 + minutes) * 150
- if seconds > 0:
- cost += 150
- return cost
- for line in inputfile.readlines():
- inputtime = line
- if line == '\n':
- #print('Empty Line')
- continue
- #split time and phone number into list
- splitinput = inputtime.split(',')
- #save into explicit variables for readabilty
- phonenumber = splitinput[1]
- hours, minutes, seconds = [int(n) for n in splitinput[0].split(':')]
- phonelist = splitinput[1].split('-')
- phone = int(''.join(map(str,phonelist)))
- #duration in seconds
- duration = hours * 3600 + minutes * 60 + seconds
- # Phone number with longest duration is free
- # if there are multiple phone numbers with the same longest duration, select the smallest phone number
- if duration > maxduration:
- maxduration = duration
- maxphone = phone
- elif duration == maxduration:
- if maxphone > phone:
- maxphone = phone
- # add together total cost
- cost = calcCost(duration)
- sumcost += cost
- allcalls.append({"Phonenumber":phone, "Cost":cost})
- print '{} seconds. Phonenumber {}. Cost: {}. Running Total: {}'.format(duration, phone, cost, sumcost)
- # deduct cost of calls to phonenumber with longest duration
- sumfree = sum([call["Cost"] for call in allcalls if call["Phonenumber"]==maxphone])
- sumcost -= sumfree
- print 'Free Calls: {} saved. Phone number {}'.format(sumfree,maxphone)
- print 'The total cost of all calls is: {}'.format(sumcost)
Advertisement
Add Comment
Please, Sign In to add comment