Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Given an array of integers where every integer occurs three times except for one integer, which only occurs once, find and return the non-duplicated integer.
- For example, given [6, 1, 3, 3, 3, 6, 6], return 1. Given [13, 19, 13, 13], return 19.
- Do this in O(N) time and O(1) space.
- '''
- from timeit import default_timer as timer
- from random import randrange, shuffle, _sha512
- # Function 1
- def solve1(myList):
- uniques = list()
- for i in range(len(myList)):
- for unique in uniques:
- if myList[i] == unique:
- break
- uniques.append(myList[i])
- counters = list()
- for i in range(len(uniques)):
- counters.append(0)
- for i in range(len(myList)):
- for unique in uniques:
- if myList[i] == unique:
- counters[i] += 1
- # So, now my counters list is ready
- for i in range(len(counters)):
- if counters[i] == 1:
- return uniques[i]
- # Function 2
- def solve2(myList):
- myList = sorted(myList)
- # Now, that "myList" is sorted, I can compare its element with the previous and next one. If my current element is equal with the previous one and
- # the next one, I am sure that this element appears exactly 3 times, so I walk on. Otherwise, I have to return this element.
- answer = -1000
- for i in range(1, len(myList)-1):
- if myList[i] != myList[i-1] and myList[i] != myList[i+1]:
- answer = myList[i]
- # The above code works when this element is only in the MEDIUM positions of the array, otherwise the value of "answer" remains -1000
- # This means that maybe the first or last element is the answer
- if answer == -1000:
- if myList[0] == myList[1]:
- # The answer is not in the list's beginning, but in the end
- answer = myList[len(myList)-1]
- else:
- answer = myList[0]
- return answer
- # Function 3
- def prettyPrintWithTime(function, myList):
- start = timer()
- result = function(myList)
- end = timer()
- elapsed = 10**6 * (end - start)
- print("Using function " + str(function.__name__) + ": " + str(myList) + " ----> " + str(result) + " (" + str(round(elapsed, 2)) + " μs).")
- # Function 4
- def generateRandomList(n1, n2):
- N = randrange(n1, n2) # 7 <= N <= 11
- # My final list will contain form 7 * 3 + 1 = 22 elements to 11 * 3 + 1 = 34 elements
- myList = list()
- myList.append(randrange(20))
- for i in range(1, N):
- r = randrange(myList[len(myList)-1] + 1, myList[len(myList)-1] + 21)
- myList.append(r)
- # These will be the different elements except the last one, that I will keep it twice
- newList = list()
- for i in range(len(myList)):
- newList.append(myList[i])
- newList.append(myList[i])
- newList.append(myList[i])
- # Last step is to add a brand-new different value
- exists = False
- new = randrange(200)
- for element in myList:
- if element == new:
- exists = True
- break
- while exists == True:
- new = randrange(200)
- for element in myList:
- if element == new:
- exists = True
- break
- newList.append(new)
- shuffle(newList)
- return newList
- # MAIN FUNCTION
- list1 = [6, 1, 3, 3, 3, 6, 6]
- list2 = [13, 19, 13, 13]
- list3 = generateRandomList(7, 12)
- prettyPrintWithTime(solve1, list1)
- prettyPrintWithTime(solve2, list1)
- prettyPrintWithTime(solve1, list2)
- prettyPrintWithTime(solve2, list2)
- prettyPrintWithTime(solve1, list3)
- prettyPrintWithTime(solve2, list3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement