Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Question 1
- from random import shuffle
- def shuff(A):
- while sorted(A) != A: #when the list is not sorted
- shuffle(A) #shuffle the list
- return A #return the sorted list
- A= [3,1,5,6,8,1,5,7]
- B= [1,3,6,4,8,1,1,0,99]
- shuff(A)
- shuff(B)
- #Question 2
- import random
- def finder(A,d):
- n= len(A)
- maxentries= int(n/2) #the number of trials to attempt
- for i in range(maxentries):
- index = random.randint(0,n-1)
- rand = A[index] #a random value from the array
- smaller=[1 for j in A if rand < j] #number of objects smaller than the random value
- location = sum(smaller) #the location of rand if the list is sorted (if there are 4 objects smaller
- #then the index will be 4 with 0 1 2 3 before it)
- 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
- return rand #return rand
- else:
- print("No median found")
- approx_median(range(100), 0.1)
- list= [1,3,4,6,2,7,1,9]
- finder(list,0.5)
Add Comment
Please, Sign In to add comment