gmo61520

Untitled

Feb 16th, 2016
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 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[2] - x[1],
  25. guests
  26. )
  27. )
  28.  
  29. def max_occupancy(guests):
  30. current_guests = []
  31. max_current = 0
  32. for entry in guests:
  33. current_guests = filter(lambda x: x > entry[1], current_guests) + [ entry[2] ]
  34. if len(current_guests) > max_current:
  35. max_current = len(current_guests)
  36. return max_current
  37.  
  38. # testing
  39. guests = read_guests("text.txt")
  40. print(longest_stay(guests))
  41. print(max_occupancy(guests))
Advertisement
Add Comment
Please, Sign In to add comment