Apr-24-2018, 12:45 PM
Here's a quiz of a Math professor on Twitter:
![[Image: ewX81x]](https://ibb.co/ewX81x)
He says the results are:
1) 1 / 930
2) 1 / 14880
I did this code (python3) and while the first result is the same, the second is not the same.
I don't understand why the 2nd answer with my code is about 0.002.
I investigated my script for hours and i don't find any mistake...
Do you confirm that the code is right?
He says the results are:
1) 1 / 930
2) 1 / 14880
I did this code (python3) and while the first result is the same, the second is not the same.
I don't understand why the 2nd answer with my code is about 0.002.
I investigated my script for hours and i don't find any mistake...
Do you confirm that the code is right?
import random def bubblesortOnePass(l): if len(l) == 1: return l else: for i in range(len(l)): try: if l[i] > l[i+1]: l[i], l[i+1] = l[i+1], l[i] except: if l[-2] > l[-1]: l[-2],l[-1] = l[-1],l[-2] return l def bubblesortTwoPass(l): global counter if len(l) == 1 or counter == 2: return l else: for i in range(len(l)): try: if l[i] > l[i+1]: l[i], l[i+1] = l[i+1], l[i] except: if l[-2] > l[-1]: l[-2],l[-1] = l[-1],l[-2] counter += 1 return bubblesortTwoPass(l[:-1]) + [l[-1]] def generateList(): L = [] remaining = [i for i in range(1,41)] while len(L) < 40: random_N = random.choice(remaining) L.append(random_N) remaining.remove(random_N) return L N = 1000000 totalOnePass = 0 totalTwoPass = 0 for i in range(N): L = generateList() f = L[19] K = L.copy() bubblesortOnePass(L) if f == L[29]: totalOnePass += 1 counter = 0 After2pass = bubblesortTwoPass(K) if f == After2pass[29]: totalTwoPass += 1 print("Probability after One Pass is {}".format(float(totalOnePass / N))) print("Probability after Two Pass is {}".format(float(totalTwoPass / N)))