Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random, time
- def split_list(alist, wanted_parts=1): ##splits a list into equle chunks
- length = len(alist)
- return [ alist[i*length // wanted_parts: (i+1)*length // wanted_parts] for i in range(wanted_parts)]
- def tester(chunk, i):
- mid = len(chunk)/2
- if i == 0:
- thisnum = chunk[mid]
- if not chunk[mid-1] == chunk[mid]-1 or not chunk[mid+1] == chunk[mid]+1:
- return True, thisnum+1
- else:
- thisnum = chunk[mid-i]
- if not chunk[mid-i-1] == chunk[mid-i]-1 or not chunk[mid-i+1] == chunk[mid-i]+1:
- return True, thisnum-1
- thisnum = chunk[mid+i]
- if not chunk[mid+i-1] == chunk[mid+i]-1 or not chunk[mid+i+1] == chunk[mid+i]+1:
- return True, thisnum+1
- return False, 0
- def scan(chunks):
- found_list = []
- chunk_found = len(chunks)
- mid = len(chunks[0])/2
- for i in range(mid):
- for x in range(len(chunks)):
- if chunk_found > x+1:
- found, num = tester(chunks[x], i)
- if found:
- found_list.append(num)
- chunk_found = x+1
- return min(found_list)
- def main():
- x = range(10000)
- for i in range(5):
- rem = random.randint(0, 9999)
- if rem in x:
- print("removing %s" %rem)
- x.remove(rem)
- else:
- i+=1
- chks = split_list(x, 10)
- return scan(chks)
- if __name__ == "__main__":
- times = []
- for i in range(1000):
- start = time.time()
- lowest_missing = main()
- end = time.time() - start
- times.append(end)
- print("Found the lowest missing value as %s in %ss" %(lowest_missing,end))
- avarage = reduce(lambda x, y: x + y, times) / float(len(times))
- print("avarage time: %s" %avarage)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement