Untitled

mail@pastecode.io avatar
unknown
python
22 days ago
4.0 kB
1
Indexable
Never
#!/bin/python3

import math
import os
import random
import re
import sys



#
# Complete the 'valuation' function below.
#
# The function is expected to return a LONG_INTEGER.
# The function accepts following parameters:
#  1. LONG_INTEGER reqArea
#  2. LONG_INTEGER_ARRAY area
#  3. LONG_INTEGER_ARRAY price
#
import statistics as stat
import numpy as np

def valuation(reqArea, area, price):
    # Write your code here
    
    house_list={}
    
    for i in range(len(area)):
        if area[i] not in house_list:
            house_list[area[i]]=[price[i]]
        else:
            house_list[area[i]].append(price[i])
    
    final_area=[]
    final_price=[]
            
    for i in range(len(area)):
        price_i=price[i]
        area_i= area[i]
        compList=house_list[area_i].copy()
        compList.remove(price_i)
        
        if compList:
            mean_price = mean_price=stat.mean(compList)
            sd=stat.pstdev(compList)
            
            dif = abs(price_i - mean_price)
            if dif > 3*sd:
                continue
        
        final_area.append(area_i)
        final_price.append(price_i)
        
    if not final_area:
        answer = round(1000*reqArea)
        return max(1000,min(answer,1000000))
        
    if len(final_area)==1:
        answer=round(final_price[0])
        return max(1000, min(answer,1000000))
    
    smaller_area=None
    larger_area=None
    for i in final_area:
        if i <reqArea and (smaller_area is None or i > smaller_area):
            smaller_area=i
        if i >reqArea and (larger_area is None or i < larger_area):
            larger_area=i
    
    if smaller_area is not None and larger_area is not None:
        smaller_indices = [i for i,a in enumerate(final_area) if a==smaller_area]
        larger_indices = [i for i,a in enumerate(final_area) if a == larger_area]
        smaller_mean_price=sum([final_price[i] for i in smaller_indices])/len(smaller_indices)
        larger_mean_price=sum([final_price[i] for i in larger_indices])/len(larger_indices)
        
        calculated_price=smaller_mean_price + ((larger_mean_price-smaller_mean_price)/(larger_area - smaller_area))*(reqArea-smaller_area)
        result=round(calculated_price)
        return max(1000,min(result, 1000000))
        
    if smaller_area is not None and larger_area is None:
        largest_areas=sorted(set(final_area), reverse=True)[:2]
        mean_prices=[np.mean([p for a, p in zip(final_area, final_price) if a == area_i]) for area_i in largest_areas]
        if len(largest_areas)==2:
            calculated_price2=mean_prices[1] + (reqArea - largest_areas[1])*(mean_prices[1]-mean_prices[0])/(largest_areas[1]-largest_areas[0])
            final_answer=round(calculated_price2)
        
            return max(1000, min(final_answer, 1000000))
        return max(1000,min(round(mean_prices[0]),1000000))
    
    if smaller_area is None and larger_area is not None:
        smallest_areas=sorted(set(final_area))[:2]
        mean_prices=[np.mean([p for a,p in zip(final_area,final_price) if a == area_i]) for area_i in smallest_areas]
        
        if len(smallest_areas)==2:
            calculated_price3=mean_prices[0]+(reqArea - smallest_areas[0])*(mean_prices[1]-mean_prices[0])/(smallest_areas[1]-smallest_areas[0])
            final_answer=round(calculated_price3)
            return max(1000,min(final_answer,1000000))
        return max(1000,min(round(mean_prices[0]),1000000))
    return 1000
        
if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    reqArea = int(input().strip())

    area_count = int(input().strip())

    area = []

    for _ in range(area_count):
        area_item = int(input().strip())
        area.append(area_item)

    price_count = int(input().strip())

    price = []

    for _ in range(price_count):
        price_item = int(input().strip())
        price.append(price_item)

    result = valuation(reqArea, area, price)

    fptr.write(str(result) + '\n')

    fptr.close()
Leave a Comment