Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # non-specific helper functions
- def chunks(n, l):
- for i in range(0, len(l), n):
- yield l[i:i+n]
- # specific helper functions
- DAYS_PER_MONTH = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]
- def date_to_days(date):
- splitted = date.split('/')
- return sum(DAYS_PER_MONTH[1:int(splitted[0])]) + int(splitted[1])
- def read_guests(fpath):
- return map(
- lambda x: ( x[0], date_to_days(x[1]), date_to_days(x[2]) ),
- chunks(3,
- open(fpath).read().split()
- )
- )
- # main part
- def longest_stay(guests):
- return max(
- map(
- lambda x: ( x[0], 1 + x[2] - x[1] ),
- guests
- ),
- key = lambda x: x[1]
- )
- def max_occupancy(guests):
- current_guests = []
- max_current = 0
- for entry in guests:
- current_guests = filter(lambda x: x > entry[1], current_guests) + [ entry[2] ]
- if len(current_guests) > max_current:
- max_current = len(current_guests)
- return max_current
- # testing
- guests = read_guests("tourists.txt")
- print(longest_stay(guests))
- print(max_occupancy(guests))
Advertisement
Add Comment
Please, Sign In to add comment