Question

In: Computer Science

python3 Let x0,...,xn−1 be n numbers stored in the list x. The median of x, denoted...

python3

Let x0,...,xn−1 be n numbers stored in the list x. The median of x, denoted median(x) is the “middle”-value of the numbers in x in the sense that half of the numbers in x are less than the median and the other half of the numbers in x are greater than the median. Let y denote a copy of x sorted in ascending order. Then median(x)=(y[n+1 2 −1] if n is oddy [n 2−1]+y[n 2] 2 if n is even. Complete the function my_median with the following specifications: • It that takes in one parameter, x, which is a list of numbers. • It returns median(x). • Do not alter x. Evaluating your function my_median(x) should not change the value of the list x. • Do not use Python’s built-in median function.

Solutions

Expert Solution

Raw_code:

# my_median function
def my_median(x):
# creating a sorted list of x and assigning to y
y = sorted(x)
# calculating length of list-x and assigning the length to n
n = len(x)

# checking whether the n is even or not by modular division
if (n%2) == 0:
# if even returing the average of middle two elements of y
return (y[n//2 -1] + y[n//2])/2
else:
# returning the middle element of y
return y[(n+1)//2 -1]

x = [2, 4, 1, 3 ,5, 6]
print("x :", x, " Median of x: ", my_median(x))

x = [4, 5, 2, 1, 3]
print("x :", x, " Median of x: ", my_median(x))


Related Solutions

Consider the sequence: x0=1/6 and xn+1 = 2xn- 3xn2 | for all natural numbers n. Show:...
Consider the sequence: x0=1/6 and xn+1 = 2xn- 3xn2 | for all natural numbers n. Show: a) xn< 1/3 for all n. b) xn>0 for all n. Hint. Use induction. c) show xn isincreasing. d) calculate the limit.
Let x1 > 1 and xn+1 := 2−1/xn for n ∈ N. Show that xn is...
Let x1 > 1 and xn+1 := 2−1/xn for n ∈ N. Show that xn is bounded and monotone. Find the limit. Prove by induction
2. Consider the stochastic process {Xn|n ≥ 0}given by X0 = 1, Xn+1 = I{Xn =...
2. Consider the stochastic process {Xn|n ≥ 0}given by X0 = 1, Xn+1 = I{Xn = 1}Un+1 + I{Xn 6= 1}Vn+1, n ≥ 0, where {(Un, Vn)|n ≥ 1} is an i.i.d. sequence of random variables such that Un is independent of Vn for each n ≥ 1 and U1−1 is Bernoulli(p) and V1−1 is Bernoulli(q) random variables. Show that {Xn|n ≥ 1} is a Markov chain and find its transition matrix. Also find P{Xn = 2}.
Let An = {ai} n i=1 denote a list of n distinct positive integers. The median...
Let An = {ai} n i=1 denote a list of n distinct positive integers. The median mA of An is a value in An such that half the elements in An are less than m (and so, the other half are greater than or equal m). In fact, the median element is said to have a middle rank. (a) Develop an algorithm that uses Sorting to return mA given An. (6%) (b) Now assume that one is given another list...
Prove the following test: Let {xn} be a sequence and lim |Xn| ^1/n = L 1....
Prove the following test: Let {xn} be a sequence and lim |Xn| ^1/n = L 1. If L< 1 then {xn} is convergent to zero 2. If L> 1 then {xn} is divergent
Let x = (x1,...,xn) ∼ N(0,In) be a MVN random vector in Rn. (a) Let U...
Let x = (x1,...,xn) ∼ N(0,In) be a MVN random vector in Rn. (a) Let U ∈ Rn×n be an orthogonal matrix (UTU = UUT = In) and nd the distribution of UTx. Let y = (y1,...,yn) ∼ N(0,Σ) be a MVN random vector in Rn. Let Σ = UΛUT be the spectral decomposition of Σ. (b) Someone claims that the diagonal elements of Λ are nonnegative. Is that true? (c) Let z = UTy and nd the distribution of...
Let a sequence {xn} from n=1 to infinity satisfy x_(n+2)=sqrt(x_(n+1) *xn) for n=1,2 ...... 1. Prove...
Let a sequence {xn} from n=1 to infinity satisfy x_(n+2)=sqrt(x_(n+1) *xn) for n=1,2 ...... 1. Prove that a<=xn<=b for all n>=1 2. Show |x_(n+1) - xn| <= sqrt(b)/(sqrt(a)+sqrt(b)) * |xn - x_(n-1)| for n=2,3,..... 3. Prove {xn} is a cauchy sequence and hence is convergent Please show full working for 1,2 and 3.
4. (Reflected random walk) Let {Xn|n ≥ 0} be as in Q6. Show that Xn+1 =...
4. (Reflected random walk) Let {Xn|n ≥ 0} be as in Q6. Show that Xn+1 = X0 + Zn+1 − Xn m=0 min{0, Xm + Vm+1 − Um+1}, where Zn = Xn m=1 (Vm − Um), n ≥ 1. Q5. (Extreme value process) Let {In|n ≥ 0} be an i.i.d. sequence of Z-valued random variables such that P{I1 = k} = pk, k ∈ Z and pk > 0 for some k > 0. Define Xn = max{I1, I2, ·...
Let S be a set of n numbers. Let X be the set of all subsets...
Let S be a set of n numbers. Let X be the set of all subsets of S of size k, and let Y be the set of all ordered k-tuples (s1, s2,   , sk) such that s1 < s2 <    < sk. That is, X = {{s1, s2,   , sk} | si  S and all si's are distinct}, and Y = {(s1, s2,   , sk) | si  S and s1 < s2 <    < sk}. (a) Define a one-to-one correspondence f : X → Y. Explain...
Let X = {x1,x2,...,xn} a sequence of real numbers. Design an algorithm that in linear time...
Let X = {x1,x2,...,xn} a sequence of real numbers. Design an algorithm that in linear time finds the continue subsequence of elements xi,xi+1,...,x, which product is the maximum. Suppose that the product of an empty subsequence is 1 and observe that the values can be less to 0 and less to 1.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT