# -*- coding: utf-8 -*-
"""
Created on Wed Jan  9 21:29:29 2019

@author: Faradars-pc2
"""

import bisect 
import random

def find_closest(majmooe, ozv):
    
    i = bisect.bisect_left(majmooe, ozv)
    if i == len(majmooe):
        return i - 1
    elif majmooe[i] == ozv:
        return i
    elif i > 0:
        j = i - 1
        if majmooe[i] - ozv > ozv - majmooe[j]:
            return j
    return i    

important_numbers = []
for i in range(10):
    new_number = random.randint(0, 1000)
    bisect.insort(important_numbers, new_number)

print(important_numbers)

closest_index = find_closest(important_numbers, 500) 
print("Closest value to 500: ", important_numbers[closest_index])   

closest_index = find_closest(important_numbers, -250) 
print("Closest value to -250: ", important_numbers[closest_index])   

closest_index = find_closest(important_numbers, 1100) 
print("Closest value to 1100: ", important_numbers[closest_index])       
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    