Skip to content

Simpler approach #2

Open
Open
@n00b0dyy

Description

@n00b0dyy

import csv
import datetime

def main():
properties_file = 'properties.csv'
calendar_file = 'calendar.csv'
searches_file = 'searches.csv'
output_file = 'search_results.csv'

properties = load_properties(properties_file)
calendar = load_calendar(calendar_file)
searches = load_searches(searches_file)

results = []

for search in searches:
    search_id, lat, lng, checkin, checkout = search
    valid_properties = filter_properties(properties, lat, lng)
    available_properties = filter_availability(valid_properties, calendar, checkin, checkout)
    sorted_properties = sorted(available_properties, key=lambda x: x[1])[:10]

    for rank, (property_id, total_price) in enumerate(sorted_properties, start=1):
        results.append([search_id, rank, property_id, total_price])

write_results(output_file, results)

def load_properties(file_path):
properties = {}
with open(file_path, 'r') as file:
reader = csv.reader(file)
next(reader) # Skip header
for row in reader:
property_id, lat, lng, price = int(row[0]), float(row[1]), float(row[2]), int(row[3])
properties[property_id] = {'lat': lat, 'lng': lng, 'price': price}
return properties

def load_calendar(file_path):
calendar = {}
with open(file_path, 'r') as file:
reader = csv.reader(file)
next(reader) # Skip header
for row in reader:
date, availability, price = row[1], int(row[2]), int(row[3]) if row[3] else None
if row[0] not in calendar:
calendar[row[0]] = {}
calendar[row[0]][date] = {'availability': availability, 'price': price}
return calendar

def load_searches(file_path):
searches = []
with open(file_path, 'r') as file:
reader = csv.reader(file)
next(reader) # Skip header
for row in reader:
searches.append((int(row[0]), float(row[1]), float(row[2]), row[3], row[4]))
return searches

def filter_properties(properties, lat, lng):
return {id: prop for id, prop in properties.items() if
lat - 1 <= prop['lat'] <= lat + 1 and
lng - 1 <= prop['lng'] <= lng + 1}

def filter_availability(properties, calendar, checkin, checkout):
available_properties = []
checkin_date = datetime.date.fromisoformat(checkin)
checkout_date = datetime.date.fromisoformat(checkout)
for property_id, prop in properties.items():
total_price = 0
is_available = True
for single_date in daterange(checkin_date, checkout_date):
date_str = single_date.isoformat()
if property_id in calendar and date_str in calendar[property_id]:
day_info = calendar[property_id][date_str]
if day_info['availability'] == 0:
is_available = False
break
total_price += day_info['price'] if day_info['price'] else prop['price']
else:
total_price += prop['price']
if is_available:
available_properties.append((property_id, total_price))
return available_properties

def daterange(start_date, end_date):
for n in range(int((end_date - start_date).days)):
yield start_date + datetime.timedelta(n)

def write_results(file_path, results):
with open(file_path, 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['search_id', 'rank', 'property_id', 'total_price'])
writer.writerows(results)

if name == 'main':
main()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions