Question

In: Statistics and Probability

PYTHON IS3073 1. A group of statisticians at a local college has asked you to create...

PYTHON IS3073

1. A group of statisticians at a local college has asked you to create a set of functions that compute the median and mode of a set of numbers, as defined in Section 5.4. Define these functions in a module named stats.py. Also include a function named mean, which computes the average of a set of numbers. Each function should expect a list of numbers as an argument and return a single number. Each function should return 0 if the list is empty. Include a main function that tests the three statistical functions with a given list.

7. Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order.

8. A file concordance tracks the unique words in a file and their frequencies. Write a program that displays a concordance for a file. The program should output the unique words and their frequencies in alphabetical order. Variations are to track sequences of two words and their frequencies, or n words and their frequencies.

Solutions

Expert Solution

1) The Python code to compute mean, median and mode of a set of data is given below.

Input the data as an array to each functions.

def mean(data):
    total = 0
    num_elements =len(data)
    if(num_elements  == 0):
        return(0)
    for item in data:
        total = total + item
    mu = total / (num_elements) # alternately mean = (total / len(data))
    return (mu)

def median(data):
    num_elements =len(data)
    if(num_elements  == 0):
        return(0)
    data.sort()
    # determine even or odd
    even = True
    if len(data)%2 == 1:
        even = False
    if even:
        slice_start = (len(data)//2) - 1
        slice_end   = (len(data)//2) + 1
        me      = sum(data[slice_start:slice_end]) / 2
    else:
        me      = data[len(data)//2]
    return (me)

def mode(data):
    num_elements =len(data)
    if(num_elements  == 0):
        return(0)
    hits = []
    for item in data:
        tally = data.count(item)
        values = (tally, item)
    # Only add one entry for each number in the set
        if values not in hits:
           hits.append(values)
    hits.sort(reverse=True)
    if hits[0][0]>hits[1][0]:
        return(hits[0][1])
    else:
        print("\nThere is not a mode")
        return(0)

if __name__ == "__main__":
    data = [2,4,6,5,7,8]
    print("\nThe mean is:", mean(data))
    print("\nThe median is:", median(data))
    print("\nThe mode is:", mode(data))

The output is:

The mean is: 5.333333333333333

The median is: 5.5

There is not a mode

The mode is: 0

We are required to answer only one question. Post the remaining questions as another post.


Related Solutions

Problem Description A local veterinarian at The Pet Boutique has asked you to create a program...
Problem Description A local veterinarian at The Pet Boutique has asked you to create a program for her office to create invoices for her patient’s office visits. When a customer brings their pet to the boutique, the clerk gets the Customer’s name, address, phone number and email address, as well as the pets name, pet type, pet age, and pet weight. After the customer sees the Veterinarian, the clerk determines the charges for the services, and medications for the office...
If the president of your college called you in and asked how a community college  could create...
If the president of your college called you in and asked how a community college  could create a competitive advantage for itself, what would you recommend? (Be sure to consider the costs as well as the benefits of your proposal and the economic aspect.
If the president of your college called you in and asked how community college could create...
If the president of your college called you in and asked how community college could create a competitive advantage for itself, what would you recommend? (Be sure to consider the costs as well as the benefits of your proposal). i. Write a clear, economics based response to the discussion question. ii. Cite references used to form your response, ie., newspapers, magazines, websites, textbooks, etc.
You are volunteering for a local children's softball league. They asked you to create an app...
You are volunteering for a local children's softball league. They asked you to create an app or a Web application to help in their operations. You decide to create a database. The league consists of several teams. Players are assigned to teams at the start of the season. It also collects parents' contact information, and charges season and optional equipment rental fees. League volunteers include officials, referees, coaches, and first aid attendants. Parents also volunteer to help at individual practices...
A group of 100 college students and 100 college graduates was asked to reply to the...
A group of 100 college students and 100 college graduates was asked to reply to the question, "In general, do you enjoy reading a college textbook?" The following are the frequency counts for their responses on the 3-option item. Usually Sometimes Never Students 30 25 45 Graduates 50 20 30 The critical value that you would use for an alpha level of .10 in the problem comparing college students' and graduates' enjoyment of reading textbooks is?
You have been asked to serve as a consultant for a local support group for cancer...
You have been asked to serve as a consultant for a local support group for cancer patients. They would like you to do the following: 1) provide a presentation on health literacy; 2) draft a handout on how to find quality information related to cancer care, study findings, and treatment implications based upon study findings. As you prepare for both of these charges, what would be some key points and resources that you would want to share with the group?
Your local community college is opening a new campus in a different city and has asked...
Your local community college is opening a new campus in a different city and has asked your software firm to create a student roster program. You first have to develop a class that represents the roster of students who are currently studying in this local community college. What components and attributes will need to be included in the Student class? Why?  Please include a Python code sample.
Python Problem Problem 1: In this problem you are asked to write a Python program to...
Python Problem Problem 1: In this problem you are asked to write a Python program to find the greatest and smallest elements in the list. The user gives the size of the list and its elements (positive and negative integers) as the input. Sample Output: Enter size of the list: 7 Enter element: 2 Enter element: 3 Enter element: 4 Enter element: 6 Enter element: 8 Enter element: 10 Enter element: 12 Greatest element in the list is: 12 Smallest...
You have been asked to create a python program that will ask the user how many...
You have been asked to create a python program that will ask the user how many tickets the user would like to purchase for graduation. The user will then choose the number of tickets needed and the program will list the cost of the tickets, a service fee, tax, and the total of the purchase. Specifications: The program should only accept whole numbers when receiving input for the number of tickets needed. No calculations should be performed if input is...
You have been asked by your local elementary school principal to address a group of parents...
You have been asked by your local elementary school principal to address a group of parents about the problem of obesity. Among the topics you have been asked to talk about are good nutrition, exercise, and lifestyle changes. Since all your parents are adult, list five principles of adult learning and describe how you would incorporate them into your teaching plan?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT