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 number_of_unique_tourists(fpath):
- """Return number of unique tourists loaded from file
- :param fpath: string, path to a datafile
- :return:int - number of unique tourists
- """
- guests = read_guests(fpath)
- return len(set(map(
- lambda x: x[0],
- guests
- )))
- def longest_stay(fpath):
- """ Return info about the tourist with the longest stay
- :param fpath: string, path to a datafile
- :return: name(string) - the name of the tourist
- number_of_nights (int) the length of the stay
- """
- guests = read_guests(fpath)
- return max(
- map(
- lambda x: ( x[0], 1 + x[2] - x[1] ),
- guests
- ),
- key = lambda x: x[1]
- )
- def max_occupancy(fpath):
- guests = read_guests(fpath)
- current_guests = []
- max_current = 0
- for entry in sorted(guests, key = lambda x: x[1]):
- 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
- def guests_for_night(fpath, from_date, to_date):
- guests = read_guests(fpath)
- from_value = date_to_days(from_date)
- to_value = date_to_days(to_date)
- return map(
- lambda x: x[0],
- filter(
- lambda x: x[1] <= from_value and x[2] >= to_value,
- guests
- )
- )
- # testing
- print(number_of_unique_tourists("tourists.txt"))
- print(longest_stay("tourists.txt"))
- print(max_occupancy("tourists.txt"))
- print(guests_for_night("tourists.txt", "01/16", "01/17"))
Advertisement
Add Comment
Please, Sign In to add comment