Question

In: Computer Science

Using python Create a function that inputs a list of numbers and outputs the median of...

Using python

Create a function that inputs a list of numbers and outputs the median of the numbers.
sort them and them show the output.

Solutions

Expert Solution

SOLUTION:

NOTE: Explanation is done in the code itself, Reading the data can be done in other ways also take the number of items, and loop through and read the data. I have used a one-line statement to read the data. Don't give space at the end while entering the data since it will through an error if we give space since if we give space split will separate using space so that it will create an empty item at end of the list since we are doing the int conversion it will through error, So i am also providing another way or reading data also.

CODE:

##defining the function which takes the list as an input and checks the median
def median(data):
##sort method is used to sort data of list
data.sort()
##printing the data to check whether it is sorted or not
print(data)
if(len(data)/2!=0):
##if there are an odd number of items then the middle term is median
print("Median is ",data[len(data)//2])
else:
##if there are even number of items then median is the average of middle-term and its next term
print("Median is ",(data[len(data)//2]+data[len(data)//2])/2)


##reading the data in a single line separated by spaces
##we are using split to separate string apart and then by the map we are converting it to integer since it is the map we need to convert it to list
data=list(map(int,input("Enter numbers ").split(" ")))

##call the function median to get the median
median(data)

SOLUTION:

OUTPUT:

ANOTHER WAY OF READING DATA AND CONVERTING TO INT :


def median(data):
##sort method is used to sort data of list
data.sort()
##printing the data to check whether it is sorted or not
print(data)
if(len(data)/2!=0):
##if there are odd number of items then middle term is median
print("Median is ",data[len(data)//2])
else:
##if there are even number of items then medaian is the average of middle term and its next term
print("Median is ",(data[len(data)//2]+data[len(data)//2])/2)


##reading data
data=input("Enter numbers ").split(" ")
##creating empty list to store data
data1=[]
for i in range(len(data)):
##if data is not space we need to append it to the list converting it to int
##we are doing this to avoid any extra spaces that are given during entering data
if(data[i]!=''):
data1.append(int(data[i]))

##call median
median(data1)

CODE IMAGE:

OUTPUT:


Related Solutions

Using Python, create a function whose inputs are two matrices and whose output is the element...
Using Python, create a function whose inputs are two matrices and whose output is the element wise product of the two matrices.
USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
In Python syntax, create a list of 10 numbers (any numbers). Create your list 3 times,...
In Python syntax, create a list of 10 numbers (any numbers). Create your list 3 times, each time using a different syntax to define the list. Write a while loop that prints the numbers from 1 to 10. Convert your previous loop into a for loop. Rewrite your for loop to only have one number in the range. Write a for loop to print out each item of your list of 10 numbers, all on the same line, separated by...
In Python syntax, create a list of 10 numbers (any numbers). Create your list 3 times,...
In Python syntax, create a list of 10 numbers (any numbers). Create your list 3 times, each time using a different syntax to define the list. Write a while loop that prints the numbers from 1 to 10. Convert your previous loop into a for loop. Rewrite your for loop to only have one number in the range. Write a for loop to print out each item of your list of 10 numbers, all on the same line, separated by...
In python I want to create a function that takes in a linked list. Using recursion...
In python I want to create a function that takes in a linked list. Using recursion only, I want to check if the linked list is sorted. How do i do this?
Write a python program to create a list of integers using random function. Use map function...
Write a python program to create a list of integers using random function. Use map function to process the list on the series: x + x2/2! + x3/3! + ….n and store the mapped elements in another list. Assume the value of n as 10
Write a function that inputs a list of numbers and a percentile value from 0 to...
Write a function that inputs a list of numbers and a percentile value from 0 to 1 and have the function return the element that is at that percentile (or greater than). You can use this website for the steps https://www.indeed.com/career-advice/career-development/how-to-calculate-percentile check out the step for “Follow these steps to calculate the kth percentile” def percentileReturn(list1,percentile):#function which returns the list of elements which are greater than or equal to the percentile list2=[i for i in list1 if i>=percentile] #creating list2...
Python Create a Python script file called hw3.py. Ex. 1. Write a program that inputs numbers...
Python Create a Python script file called hw3.py. Ex. 1. Write a program that inputs numbers from the user until they enter a 0 and computes the product of all these numbers and outputs it. Hint: use the example from the slides where we compute the sum of a list of numbers, but initialize the variable holding the product to 1 instead of 0. print("Enter n") n = int(input()) min = n while n != 0: if n < min:...
Write a function in Python to compute the sample mean of a list of numbers. The...
Write a function in Python to compute the sample mean of a list of numbers. The return value should be the sample mean and the input value should be the list of numbers. Do not use a built-in function for the mean. Show an example of your function in use.    Thank You
Using what you know about how to take inputs and make outputs to the console, create...
Using what you know about how to take inputs and make outputs to the console, create a check printing application. Your application will read in from the user an employee's name and salary, and print out a Console Check similar to the following. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> > | $1,000,000 | > > > > ___Pay to the Order of___ Johnny PayCheck > >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Please only use technique from chapter 1 through 3 in C++ from control structure through object 9th edition
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT