gmo61520

Untitled

Feb 16th, 2016
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. # non-specific helper functions
  2. def chunks(n, l):
  3. for i in range(0, len(l), n):
  4. yield l[i:i+n]
  5.  
  6. # specific helper functions
  7. DAYS_PER_MONTH = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]
  8. def date_to_days(date):
  9. splitted = date.split('/')
  10. return sum(DAYS_PER_MONTH[1:int(splitted[0])]) + int(splitted[1])
  11.  
  12. def read_guests(fpath):
  13. return map(
  14. lambda x: ( x[0], date_to_days(x[1]), date_to_days(x[2]) ),
  15. chunks(3,
  16. open(fpath).read().split()
  17. )
  18. )
  19.  
  20. # main part
  21. def longest_stay(guests):
  22. return max(
  23. map(
  24. lambda x: ( x[0], 1 + x[2] - x[1] ),
  25. guests
  26. ),
  27. key = lambda x: x[1]
  28. )
  29.  
  30. def max_occupancy(guests):
  31. current_guests = []
  32. max_current = 0
  33. for entry in guests:
  34. current_guests = filter(lambda x: x > entry[1], current_guests) + [ entry[2] ]
  35. if len(current_guests) > max_current:
  36. max_current = len(current_guests)
  37. return max_current
  38.  
  39. # testing
  40. guests = read_guests("tourists.txt")
  41. print(longest_stay(guests))
  42. print(max_occupancy(guests))
Advertisement
Add Comment
Please, Sign In to add comment