Advertisement
AceScottie

list_scanner_advanced.py

Apr 28th, 2019
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.55 KB | None | 0 0
  1. import random, time
  2.  
  3.    
  4. def split_list(alist, wanted_parts=1): ##splits a list into equle chunks
  5.     length = len(alist)
  6.     return [ alist[i*length // wanted_parts: (i+1)*length // wanted_parts] for i in range(wanted_parts)]
  7.    
  8. def tester(chunk, i):
  9.     mid = len(chunk)/2
  10.     if i == 0:
  11.         thisnum = chunk[mid]
  12.         if not chunk[mid-1] == chunk[mid]-1 or not chunk[mid+1] == chunk[mid]+1:
  13.             return True, thisnum+1
  14.     else:
  15.         thisnum = chunk[mid-i]
  16.         if not chunk[mid-i-1] == chunk[mid-i]-1 or not chunk[mid-i+1] == chunk[mid-i]+1:
  17.             return True, thisnum-1
  18.         thisnum = chunk[mid+i]
  19.         if not chunk[mid+i-1] == chunk[mid+i]-1 or not chunk[mid+i+1] == chunk[mid+i]+1:
  20.             return True, thisnum+1
  21.     return False, 0
  22.  
  23. def scan(chunks):
  24.     found_list = []
  25.     chunk_found = len(chunks)
  26.     mid = len(chunks[0])/2
  27.     for i in range(mid):
  28.         for x in range(len(chunks)):
  29.             if chunk_found > x+1:
  30.                 found, num = tester(chunks[x], i)
  31.                 if found:
  32.                     found_list.append(num)
  33.                     chunk_found = x+1
  34.     return min(found_list)
  35.  
  36. def main():
  37.     x = range(10000)
  38.     for i in range(5):
  39.         rem = random.randint(0, 9999)
  40.         if rem in x:
  41.             print("removing %s" %rem)
  42.             x.remove(rem)
  43.         else:
  44.             i+=1
  45.     chks = split_list(x, 10)
  46.     return scan(chks)
  47.    
  48. if __name__ == "__main__":
  49.     times = []
  50.     for i in range(1000):
  51.         start = time.time()
  52.         lowest_missing = main()
  53.         end = time.time() - start
  54.         times.append(end)
  55.         print("Found the lowest missing value as %s in %ss" %(lowest_missing,end))
  56.     avarage = reduce(lambda x, y: x + y, times) / float(len(times))
  57.     print("avarage time: %s" %avarage)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement