How do I find out who stayed the maximum for the night? Name and total number of days?

How do I find out who stayed the maximum for the night? Name and total number of days? (date format MM / DD)

eg

text file contains

Robin 01/11 01/15 Mike 02/10 02/12 John 01/15 02/15 

Expected Result

 ('john', 30 ) 

my code

 def longest_stay(fpath): with open(fpath,'r')as f_handle: stay=[] for line in f_handle: name, a_date, d_date = line.strip().split() diff = datetime.strptime(d_date, "%m/%d") -datetime.strptime(a_date, "%m/%d") stay.append(abs(diff.days+1)) return name,max(stay) 

It always returns a name.

+5
source share
2 answers

It can also be implemented using pandas. I think it will be much easier with pandas.

One of the problems that I find is how you want to handle it when you have a lot of time for maximum nights. I turned to this in the following code.

 import pandas as pd from datetime import datetime as dt def longest_stay(fpath): # Reads the text file as Dataframe data = pd.read_csv(fpath + 'test.txt', sep=" ", header = None) # adding column names to the Data frame data.columns = ['Name', 'a_date', 'd_date'] # Calculating the nights for each customer data['nights'] = datetime.strptime(d_date, "%m/%d") - datetime.strptime(a_date, "%m/%d") # Slicing the data frame by applying the condition and getting the Name of the customer and nights as a tuple (as expected) longest_stay = tuple( data.ix[data.nights == data.nights.max(), {'Name', 'nights'}]) # In case if many stayed for the longest night. Returns a list of tuples. longest_stay = [tuple(x) for x in longest_stay] return longest_stay 
+1
source

Your code crashes, but does not save the name, because name will be set to the name in the file, because you only store the days when you go, so you always see the last name.

You also add + 1, which does not seem to be correct, since you should not add or include the last day, since the person does not stay that night. Your code really outputs ('John', 32) correct name randomly, because it is the last in your example file, and the day off is 1.

Just keep an eye on the best, which include the name and day you go, using the days left as a measure, and return them at the end:

 from datetime import datetime from csv import reader def longest_stay(fpath): with open(fpath,'r')as f_handle: mx,best = None, None for name, a_date, d_date in reader(f_handle,delimiter=" "): days = (datetime.strptime(d_date, "%m/%d") - datetime.strptime(a_date, "%m/%d")).days # first iteration or we found if best is None or mx < days: best = name, days return best 

Outout:

 In [13]: cat test.txt Robin 01/11 01/15 Mike 02/10 02/12 John 01/15 02/15 In [14]: longest_stay("test.txt") # 31 days not including the last day as a stay Out[14]: ('John', 31) 

You need to use abs only if the format is not always in start-end format, but you should know that it could get the wrong output using the abs value if your dates were years.

0
source

Source: https://habr.com/ru/post/1243097/


All Articles