Giter Site home page Giter Site logo

python-exercises's People

Contributors

dtia avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

python-exercises's Issues

airbnb_coding_challenge

Would you consider removing this repository or making it private? I'd like to reuse our (Airbnb) old coding challenge for a hiring experiment and your repo is the only place with a solution that I found on Google.

Thanks!

Simpler approach

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()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.