gmo61520

Untitled

Feb 16th, 2016
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 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 (
  14. ( x[0], date_to_days(x[1]), date_to_days(x[2]) ) for x in
  15. chunks(3,
  16. open(fpath).read().split()
  17. )
  18. )
  19.  
  20. # main part
  21. def number_of_unique_tourists(fpath):
  22. """Return number of unique tourists loaded from file
  23. :param fpath: string, path to a datafile
  24. :return:int - number of unique tourists
  25. """
  26. guests = read_guests(fpath)
  27. return len(set(x[0] for x in guests))
  28.  
  29. def longest_stay(fpath):
  30. """ Return info about the tourist with the longest stay
  31.  
  32. :param fpath: string, path to a datafile
  33. :return: name(string) - the name of the tourist
  34. number_of_nights (int) the length of the stay
  35. """
  36. guests = read_guests(fpath)
  37. return max(
  38. ( ( x[0], 1 + x[2] - x[1] ) for x in guests ),
  39. key = lambda x: x[1]
  40. )
  41.  
  42. def max_occupancy(fpath):
  43. guests = read_guests(fpath)
  44. current_guests, max_current = [], 0
  45. for entry in sorted(guests, key = lambda x: x[1]):
  46. current_guests = list(filter(lambda x: x > entry[1], current_guests)) + [ entry[2] ]
  47. if len(current_guests) > max_current:
  48. max_current = len(current_guests)
  49. return max_current
  50.  
  51. def guests_for_night(fpath, from_date, to_date):
  52. guests = read_guests(fpath)
  53. from_value = date_to_days(from_date)
  54. to_value = date_to_days(to_date)
  55. return list(
  56. x[0] for x in
  57. filter(
  58. lambda x: x[1] <= from_value and x[2] >= to_value,
  59. guests
  60. )
  61. )
  62.  
  63. # testing
  64. print(number_of_unique_tourists("tourists.txt"))
  65. print(longest_stay("tourists.txt"))
  66. print(max_occupancy("tourists.txt"))
  67. print(guests_for_night("tourists.txt", "01/16", "01/17"))
Advertisement
Add Comment
Please, Sign In to add comment