Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Easy with defaultdict solution in Clear category for Call to Home by furtadobb
"""
First 100 (one hundred) minutes in one day are priced at 1 coin per minute;
After 100 minutes in one day, each minute costs 2 coins per minute;
All calls are rounded up to the nearest minute. For example 59 sec ≈ 1 min, 61 sec ≈ 2 min;
Calls count to the day when they began. For example, if a call was started 2014-01-01 23:59:59,
then it should be counted to 2014-01-01;
"""
import datetime
import math
from collections import defaultdict
def cost(minutes):
if minutes > 100:
return (minutes - 100) * 2 + 100
return minutes
def total_cost(calls) -> int:
repo = defaultdict(int)
total = 0
info = [(datetime.datetime.strptime(c[:19], '%Y-%m-%d %X'), c.split(' ')[-1]) for c in calls]
for c in info:
repo[c[0].date()] += math.ceil(int(c[1]) / 60)
for c in repo.values():
total += cost(c)
return total
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert total_cost(("2014-01-01 01:12:13 181",
"2014-01-02 20:11:10 600",
"2014-01-03 01:12:13 6009",
"2014-01-03 12:13:55 200")) == 124, "Base example"
assert total_cost(("2014-02-05 01:00:00 1",
"2014-02-05 02:00:00 1",
"2014-02-05 03:00:00 1",
"2014-02-05 04:00:00 1")) == 4, "Short calls but money..."
assert total_cost(("2014-02-05 01:00:00 60",
"2014-02-05 02:00:00 60",
"2014-02-05 03:00:00 60",
"2014-02-05 04:00:00 6000")) == 106, "Precise calls"
Dec. 20, 2020
Comments: