Question

Implement the following problem as a self-contained python module. Write a program that continually prompts the...

Implement the following problem as a self-contained python module.

Write a program that continually prompts the user for positive integer values and stores them in a list. You should stop prompting when the user enters a negative integer value.

When the user is done entering values, you should print the list of integers they have provided in sorted order. You should then compute the mean and standard deviation of the values in the list.

Recall that the mean is just the arithmetic average...the sum of all the values divided by the number of values. In mathematics notation we represent the mean with the Greek letter mu, ??.

In statistics, standard deviation is the square root of variance and measures how much, on average, values deviate from the mean. So consider a list of N numbers, with mean mu, ??:

Variance = ??2 = Σ(????−??)2 ?? where Ni is the value at position i in the list. Standard

Deviation = ?? = √??2 = √Variance

You should not be using any built in modules that calculate variance/standard deviation for you, you need to code this on your own.

In order to make your program as modular as possible, however, you should define your own functions to carry out the computation. These functions should be:

• print_sorted – takes a list of integers as input and prints them in sorted order

• compute_mean – takes a list of integers as input and returns the mean (average) of the list

• compute_variance – takes a list of integers as input and returns the variance of the list. (Note that this method can call the computeMean method.)

• compute_standard_dev – takes the variance as input and returns the standard deviation (just the square root of variance).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import math
def print_sorted(A):#prints the list in sorted order
    n = len(A)
    print("Sorted list :",end="")
    for i in range(n):
        for j in range(0, n-i-1):
            if A[j] < A[j+1] :
                A[j], A[j+1] = A[j+1], A[j]
    print(A)
def compute_mean(A):#method to compute and return mean
    sum=0
    for i in A:
        sum=sum+i
    return sum/len(A)
def compute_variance(A):#method to compute variance
    u = compute_mean(A)
    p=0
    for i in A:
        p=p+i*i
    p=p/len(A)
    p=p-(u*u)
    return p
def compute_standard_dev(v):#returns standard deviation
    return math.sqrt(v)
  
l=[]
while(True):#to read user input
    n=int(input("Enter a positive number:(negative number to quit ):"))
    if(n<0):#if negative number is entered
        break
    l=l+[n]#adding it to list
print_sorted(l)
u = compute_mean(l)
print("Mean is :",u)
v = compute_variance(l)
print("Varaiance is :",v)
print("Standard deviation is :",compute_standard_dev(v))

  

output:

>>>
Enter a positive number:(negative number to quit ):1
Enter a positive number:(negative number to quit ):2
Enter a positive number:(negative number to quit ):3
Enter a positive number:(negative number to quit ):4
Enter a positive number:(negative number to quit ):5
Enter a positive number:(negative number to quit ):6
Enter a positive number:(negative number to quit ):7
Enter a positive number:(negative number to quit ):8
Enter a positive number:(negative number to quit ):9
Enter a positive number:(negative number to quit ):-1
Sorted list :[9, 8, 7, 6, 5, 4, 3, 2, 1]
Mean is : 5.0
Varaiance is : 6.66666666667
Standard deviation is : 2.58198889747
>>>

Add a comment
Know the answer?
Add Answer to:
Implement the following problem as a self-contained python module. Write a program that continually prompts the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi. Could you help me write the below program? Please don't use any header other than...

    Hi. Could you help me write the below program? Please don't use any header other than iostream, no Chegg class, no argc/argv. Nothing too advanced. Thank you! For this assignment you are implement a program that can be used to compute the variance and standard deviation of a set of values. In addition your solution should use the functions specified below that you must also implement. These functions should be used wherever appropriate in your solution. With the exception of...

  • I really need help with this python programming assignment Program Requirements For part 2, i need...

    I really need help with this python programming assignment Program Requirements For part 2, i need the following functions • get floats(): It take a single integer argument and returns a list of floats. where it was something like this def get_floats(n):   lst = []   for i in range(1,n+1):     val = float(input('Enter float '+str(i)+': '))     lst.append(val)   return lst • summer(): This non-void function takes a single list argument, and returns the sum of the list. However, it does not use...

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • C Programming QUESTION 9 Write a program that prompts for and reads four integer input values,...

    C Programming QUESTION 9 Write a program that prompts for and reads four integer input values, then a single character command. Submit your program as a .c file named midterm_prog2.c. Note that, similarly to your programming assignments, some percentage of your grade will depend on proper programming style. Your program will calculate and print a new value based on the command that's entered, as follows: 'A' or 'a': Calculate the average of the four input values 'S' or 's': Calculate...

  • Python Problem: Variance Explanation: Problem 1: Computing variance For this problem, you will write a function...

    Python Problem: Variance Explanation: Problem 1: Computing variance For this problem, you will write a function variance that takes a list whose elements are numbers (floats or ints), and returns their variance, a single number. (If you don't remember how to compute variance, check the lecture notebook; it's one of the intermediate steps in computing standard deviation.) You should not use NumPy or any other Python libraries for this problem. For now, worry only about correctness, not efficiency. Passing an...

  • Python program - Write a Python program, in a file called sortList.py, which, given a list...

    Python program - Write a Python program, in a file called sortList.py, which, given a list of names, sorts the names into alphabetical order. Use a one dimensional array to hold the list of names. To do the sorting use a simple sorting algorithm that repeatedly takes an element from the unsorted list and puts it in alphabetical order within the same list. Initially the entire list is unsorted. As each element is placed in alphabetical order, the elements in...

  • Python 3.7 to be used. Just a simple design a program that depends on its own. You should also not need to import anythi...

    Python 3.7 to be used. Just a simple design a program that depends on its own. You should also not need to import anything. No code outside of a function! Median List Traversal and Exception Handling Create a menu-driven program that will accept a collection of non-negative integers from the keyboard, calculate the mean and median values and display those values on the screen. Your menu should have 6 options 50% below 50% above Add a number to the list/array...

  • Question: In C++ Write a correct and complete C++ program th... Bookmark In C++ Write a...

    Question: In C++ Write a correct and complete C++ program th... Bookmark In C++ Write a correct and complete C++ program that inputs 20 integer values from the user and performs three calculations. In the main function, input the values from the user. As part of your answer, write a sub-function, named calcAvg (), which takes two arguments and returns the average of all the integers. Also write another sub-function, named reverseArray (), which takes two arguments and reverses the...

  • Python Code Write a program using functions and mainline logic which prompts the user to enter...

    Python Code Write a program using functions and mainline logic which prompts the user to enter a number. The number must be at least 5 and at most 20. (In other words, between 5 and 20, inclusive.) The program then generates that number of random integers and stores them in a list. The random integers should range from 0 to 100. (You can use a wider range if you want, but the lower end of the range must be at...

  • 1. Write a program that prompts the user to enter three integers and display the integers...

    1. Write a program that prompts the user to enter three integers and display the integers in non-decreasing order. You can assume that all numbers are valid. For example: Input Result 140 -5 10 Enter a number: Enter a number: Enter a number: -5, 10, 140 import java.util.Scanner; public class Lab01 { public static void main(String[] args) { Scanner input = new Scanner(System.in);    } } ---------------------------------------------------------------------------------------------------------------------------- 2. Write a program that repeatedly prompts the user for integer values from...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT