Aphatareos

Phonebill

Mar 21st, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.00 KB | None | 0 0
  1.  
  2. # input in format hh:mm:ss,nnn-nnn-nnn
  3.  
  4. inputfile = open("phonebill.txt")
  5.  
  6. allcalls = []
  7. maxduration = 0
  8. maxphone=0
  9. sumcost = 0
  10.  
  11.  
  12. def calcCost(duration):
  13.     # if the call was shorter than 5 min: 3 cents per second
  14.     # 5 min  = 300 seconds
  15.     if duration < 300:
  16.         cost = 3 * duration
  17.     # if the call was longer than 5 min: 150 cents for every started minute
  18.     # per example, if it's exactly 5 min, it's not started the sixth
  19.     elif duration >= 300:
  20.         cost = (hours * 60 + minutes) * 150
  21.         if seconds > 0:
  22.             cost += 150
  23.     return cost
  24.  
  25.  
  26.  
  27. for line in inputfile.readlines():
  28.     inputtime = line
  29.     if line == '\n':
  30.         #print('Empty Line')
  31.         continue
  32.  
  33.     #split time and phone number into list
  34.     splitinput = inputtime.split(',')
  35.     #save into explicit variables for readabilty
  36.     phonenumber = splitinput[1]
  37.     hours, minutes, seconds = [int(n) for n in splitinput[0].split(':')]
  38.     phonelist = splitinput[1].split('-')
  39.     phone = int(''.join(map(str,phonelist)))
  40.     #duration in seconds
  41.     duration = hours * 3600 + minutes * 60 + seconds
  42.  
  43.     # Phone number with longest duration is free
  44.     # if there are multiple phone numbers with the same longest duration, select the smallest phone number
  45.     if duration > maxduration:
  46.         maxduration = duration
  47.         maxphone = phone
  48.     elif duration == maxduration:
  49.         if maxphone > phone:
  50.             maxphone = phone
  51.  
  52.     # add together total cost
  53.     cost = calcCost(duration)
  54.     sumcost += cost
  55.  
  56.     allcalls.append({"Phonenumber":phone, "Cost":cost})
  57.  
  58.     print '{} seconds. Phonenumber {}. Cost: {}. Running Total: {}'.format(duration, phone, cost, sumcost)
  59.  
  60.  
  61. # deduct cost of calls to phonenumber with longest duration
  62.  
  63. sumfree = sum([call["Cost"] for call in allcalls if call["Phonenumber"]==maxphone])
  64.  
  65. sumcost -= sumfree
  66.  
  67.  
  68. print 'Free Calls: {} saved. Phone number {}'.format(sumfree,maxphone)
  69. print 'The total cost of all calls is: {}'.format(sumcost)
Advertisement
Add Comment
Please, Sign In to add comment