Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
1-liner with groupby solution in Creative category for Call to Home by roman.bratishchev
from itertools import groupby
def total_cost(calls: list[str]) -> int:
return sum( (total_m:=sum(m for _,m in m_grp))+max(0,total_m-100) for _,m_grp in groupby(
[(d,-(-int(s)//60)) for d,_,s in map(str.split, calls)],
key=lambda e: e[0]
))
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"
Aug. 17, 2024
Comments: