Created
May 15, 2019 09:26
-
-
Save Louis-Saglio/e68fd44faf947ec33ad44fa690317843 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python3.7 | |
def read_stdin() -> list: | |
""" | |
Returns a list of str read from stdin. | |
Use whitespaces as word separator. | |
""" | |
# 0 is the file descriptor of stdin. | |
# A more clever way would have been to use sys.stdin, but it is forbidden for pedagogic reasons | |
with open(0) as stdin: | |
# Cannot use readline because stdin is stored in live memory and is forgotten once it is read. | |
# So if you use readline against it, you will read only the first line | |
return stdin.read().split() | |
def parse_start_end_time_stamps(time_stamps: list): | |
""" | |
Get list of str which represents a pair of semi-colon separated time-stamps | |
Returns a generator which yields pairs of start & end time-stamps | |
""" | |
for line in time_stamps: | |
yield [int(time_stamp) for time_stamp in line.split(":")] | |
def count_second_usage_nbr(time_stamps) -> dict: | |
""" | |
Counts the number of times a specific second has been spent in a call. | |
Returns a dict with time-stamps as keys and the number of time each time-stamp has been spent in a call as value | |
""" | |
call_summary = {} | |
for boundaries in time_stamps: | |
for time_stamp in range(*boundaries): # Assumes that start is inclusive & end is exclusive | |
if time_stamp in call_summary: | |
call_summary[time_stamp] += 1 | |
else: | |
call_summary[time_stamp] = 1 | |
return call_summary | |
def main(): | |
print(max(count_second_usage_nbr(parse_start_end_time_stamps(read_stdin())).values())) | |
def test(): | |
generator = parse_start_end_time_stamps(["14:42", "0:100"]) | |
assert next(generator) == [14, 42] | |
assert next(generator) == [0, 100] | |
assert count_second_usage_nbr(((1, 2), (0, 3), (1, 4))) == {0: 1, 1: 3, 2: 2, 3: 1} | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment