Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.5 kB
1
Indexable
Never
def findValuation(reqArea, area, price):
    from collections import defaultdict
    from statistics import mean, stdev
    
    # Step 1: Remove outliers
    prices = defaultdict(list)
    for a, p in zip(area, price):
        prices[a].append(p)
    
    # remove outliers
    for a, p_list in prices.items():
        if len(p_list) > 1:
            avg = mean(p_list)
            sd = stdev(p_list)
            prices[a] = [p for p in p_list if abs(p - avg) <= 3 * sd]
    
    # Step 2: Valuation of the candidate house
    if not prices:
        return 1000
    
    all_areas = sorted(prices.keys())
    if reqArea in all_areas:
        return round(mean(prices[reqArea]))
    
    if reqArea < all_areas[0]:
        x1, y1 = all_areas[0], mean(prices[all_areas[0]])
        x2, y2 = all_areas[1], mean(prices[all_areas[1]])
    elif reqArea > all_areas[-1]:
        x1, y1 = all_areas[-2], mean(prices[all_areas[-2]])
        x2, y2 = all_areas[-1], mean(prices[all_areas[-1]])
    else:
        for i in range(len(all_areas) - 1):
            if all_areas[i] <= reqArea <= all_areas[i + 1]:
                x1, y1 = all_areas[i], mean(prices[all_areas[i]])
                x2, y2 = all_areas[i + 1], mean(prices[all_areas[i + 1]])
                break
    
    # Interpolate or extrapolate the price
    y = y1 + (reqArea - x1) * (y2 - y1) / (x2 - x1)
    y = max(1000, min(y, 10**6))
    return round(y)