fork download
  1. import numpy as np
  2. from numpy.random import randint
  3.  
  4. def nearest( list, num ):
  5. return list[ np.abs(np.asarray(list) - num).argmin() ]
  6.  
  7. if __name__ == "__main__":
  8.  
  9. upper = 1000000000
  10. lsize = upper//10
  11. list = randint(1 , high = upper , size = lsize)
  12. print( f' 1~{upper} , size = {lsize} ')
  13.  
  14. n1 = randint(1 , high = upper )
  15. print( n1 , nearest( list , n1 ) )
  16. n1 = randint(1 , high = upper )
  17. print( n1 , nearest( list , n1 ) )
  18.  
  19.  
Success #stdin #stdout 2.05s 2370952KB
stdin
Standard input is empty
stdout
 1~1000000000 , size = 100000000 
455009418 455009415
828398845 828398836