Question

In: Computer Science

Set the random number Pythonlist to variable x and write the code according to the items...

Set the random number Pythonlist to variable x and write the code according to the items below.

(1)Write a code to find the second largest element among List x Element.

(ex.x=[4,4,2,1]->2,x=[5,4,3,1]-4)4)

(2)Write a code to find elements that appear only once in a while.

(ex.x=[4,4,2,1]->[2,1],x=[5,4,3,1]=>[5,4,3,1])

(3)Write a code that generates a list of the frequency of elements for each element of Listx.

(ex.x=[4,4,2,1]->y=[2,2,2,1,1],x=[5,4,3,1]->y=[1,1,1,1])

(4)Write a code to find the value that comes to the center of the list in descending order among the listx members. (*If the number of lists is even, suggest the average value of the two values that come to the center.)

(ex.x=[4,4,2,1]->3=(4+2)/2,x=[5,4,2,1,0]->2)

I am currently using Python and I am a beginner of Python. Tell me the detailed code for the questions (1), (2), (3) and (4).

Solutions

Expert Solution

Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. 

Let me know for any help with any other questions.

Thank You!
===========================================================================

def main():
    # (1)Write a code to find the second largest element among List x Element.
    x = [4, 4, 2, 1]
    print('Second Largest Element:', list(set(x))[-2])

    #2)Write a code to find elements that appear only once in a while.
    x = [4, 4, 2, 1]
    appear_once = [num for num in x if x.count(num)==1]
    print('Elements that appear once:',appear_once)

    #3)Write a code that generates a list of the frequency of elements for each element of Listx.
    x = [4, 4, 2, 1]
    frequency = [x.count(num) for num in x ]
    print('Frequency of each element:',frequency)

    #(4)Write a code to find the value that comes to the center of the list
    x = [4, 4, 2, 1]
    median = x[len(x)//2] if len(x)%2==1 else (x[len(x)//2]+x[len(x)//2-1])/2
    print('Median:',median)
    x = [5, 4, 2, 1, 0]
    median = x[len(x) // 2] if len(x) % 2 == 1 else (x[len(x) // 2] + x[len(x) // 2 - 1]) / 2
    print('Median:', median)


main()

====================================================================


Related Solutions

The random variable x is the number of occurrences of an event over an interval of...
The random variable x is the number of occurrences of an event over an interval of ten minutes. It can be assumed that the probability of an occurrence is the same in any two-time periods of an equal length. It is known that the mean number of occurrences in ten minutes is 5.3. The probability that there are 8 occurrences in ten minutes is a. .0771 b. .0241 c. .1126 d. .9107
Let X be a random variable that is equal to the number of times a 5...
Let X be a random variable that is equal to the number of times a 5 is rolled in three rolls of a fair 5-sided die with the integers 1 through 5 on the sides. What is E[X2 ]? What is E2 [X], that is, (E[X])2 ? Justify your answers briefly
The number of knots in a piece of lumber is a random variable, X. Suppose that...
The number of knots in a piece of lumber is a random variable, X. Suppose that X has a Poisson distribution with E(X) = 4. (a) If four independent pieces of lumber are examined, what is the probability that there is exactly one lumber has no knots? (b) If we consider 100 pieces of lumber, write down the exact expression for the probability that the total number of knots is at least 450? (c) Find a normal approximation to the...
Define the random variable X to be the number of times in the month of June...
Define the random variable X to be the number of times in the month of June (which has 30 days) Susan wakes up before 6am a. X fits binomial distribution, X-B(n,p). What are the values of n and p? c. what is the probability that Susan wakes us up before 6 am 5 or fewer days in June? d. what is the probability that Susan wakes up before 6am more than 12 times?
Determine whether or not the random variable X is a binomial random variable. (a) X is...
Determine whether or not the random variable X is a binomial random variable. (a) X is the number of dots on the top face of a fair die (b) X is the number of hearts in a five card hand drawn (without replacement) from a well shuffled ordinary deck. (c) X is the number of defective parts in a sample of ten randomly selected parts coming from a manufacturing process in which 0.02% of all parts are defective. (d) X...
Consider the observed frequency distribution for the set of random variables. Random Variable, X Frequency, Fo...
Consider the observed frequency distribution for the set of random variables. Random Variable, X Frequency, Fo 0 29 1 96 2 151 3 96 4 28 Total 400 a. Perform a​ chi-square test using alpha=0.05 to determine if the observed frequencies follow the binomial probability distribution when p=0.50 and n=4. b. Determine the​ p-value and interpret its meaning. The​ chi-square test statistic is chi squared, χ2=______ p-value=______
Let X be the random variable representing the number of calls received in an hour by...
Let X be the random variable representing the number of calls received in an hour by a 911 emergency service. A probability distribution of X is given below. Value of X 0 1 2 3 4 Probability P(x) 0.32 ____ ____ 0.16 0.08 (a) Suppose the probability that X = 1 and the probability that X = 2 are the same. What are these probabilities? Incorrect: Your answer is incorrect. (b) What is the probability that at least one call...
The discrete random variable X is the number of passengers waiting at a bus stop. The...
The discrete random variable X is the number of passengers waiting at a bus stop. The table below shows the probability distribution for X. What is the expected value E(X) for this distribution? X 0 1 2 3 Total P(X) .20 .40 .30 .10 1.00 Answer the following questions using the given probability distribution. Expected value E(X) of the number of passengers waiting at the bus stop. Probability that there is at least 1 passenger at the bus stop. Probability...
What is the probability that the random variable X (which is the number of Heads from...
What is the probability that the random variable X (which is the number of Heads from flipping a coin 5 times) is equal to 0?
In the accompanying​ table, the random variable x represents the number of televisions in a household...
In the accompanying​ table, the random variable x represents the number of televisions in a household in a certain country. Determine whether or not the table is a probability distribution. If it is a probability​ distribution, find its mean and standard deviation. LOADING... Click the icon to view the data. If the table is a probability​ distribution, what is its​ mean? Select the correct choice below and fill in any answer boxes within your choice. A. Its mean is nothing....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT