gmo61520

Untitled

Feb 16th, 2016
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 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 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(map(
  28. lambda x: x[0],
  29. guests
  30. )))
  31.  
  32. def longest_stay(fpath):
  33. """ Return info about the tourist with the longest stay
  34.  
  35. :param fpath: string, path to a datafile
  36. :return: name(string) - the name of the tourist
  37. number_of_nights (int) the length of the stay
  38. """
  39. guests = read_guests(fpath)
  40. return max(
  41. map(
  42. lambda x: ( x[0], 1 + x[2] - x[1] ),
  43. guests
  44. ),
  45. key = lambda x: x[1]
  46. )
  47.  
  48. def max_occupancy(fpath):
  49. guests = read_guests(fpath)
  50. current_guests = []
  51. max_current = 0
  52. for entry in sorted(guests, key = lambda x: x[1]):
  53. current_guests = list(filter(lambda x: x > entry[1], current_guests)) + [ entry[2] ]
  54. if len(current_guests) > max_current:
  55. max_current = len(current_guests)
  56. return max_current
  57.  
  58. def guests_for_night(fpath, from_date, to_date):
  59. guests = read_guests(fpath)
  60. from_value = date_to_days(from_date)
  61. to_value = date_to_days(to_date)
  62. return list(map(
  63. lambda x: x[0],
  64. filter(
  65. lambda x: x[1] <= from_value and x[2] >= to_value,
  66. guests
  67. )
  68. ))
  69.  
  70. # testing
  71. print(number_of_unique_tourists("tourists.txt"))
  72. print(longest_stay("tourists.txt"))
  73. print(max_occupancy("tourists.txt"))
  74. print(guests_for_night("tourists.txt", "01/16", "01/17"))
Advertisement
Add Comment
Please, Sign In to add comment