Guest User

Untitled

a guest
Feb 18th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. #Question 1
  2.  
  3. from random import shuffle
  4. def shuff(A):
  5. while sorted(A) != A: #when the list is not sorted
  6. shuffle(A) #shuffle the list
  7. return A #return the sorted list
  8.  
  9. A= [3,1,5,6,8,1,5,7]
  10. B= [1,3,6,4,8,1,1,0,99]
  11.  
  12. shuff(A)
  13. shuff(B)
  14.  
  15. #Question 2
  16.  
  17. import random
  18. def finder(A,d):
  19. n= len(A)
  20. maxentries= int(n/2) #the number of trials to attempt
  21.  
  22. for i in range(maxentries):
  23. index = random.randint(0,n-1)
  24. rand = A[index] #a random value from the array
  25. smaller=[1 for j in A if rand < j] #number of objects smaller than the random value
  26. location = sum(smaller) #the location of rand if the list is sorted (if there are 4 objects smaller
  27. #then the index will be 4 with 0 1 2 3 before it)
  28.  
  29. if ((n*(1-d)/2 < location) and (location< n*(1+d)/2)): #if rand happens to be in the range specified of the sorted list
  30. return rand #return rand
  31. else:
  32. print("No median found")
  33.  
  34. approx_median(range(100), 0.1)
  35.  
  36. list= [1,3,4,6,2,7,1,9]
  37. finder(list,0.5)
Add Comment
Please, Sign In to add comment