Question

Ask the user for the name of a file. Read a list of numbers of unknown...

Ask the user for the name of a file. Read a list of numbers of unknown length from the file. Find and print the MEDIAN and the MODE(S) of the set of numbers. Do not use python statistics functions to find the medium or mode

To find the MODE(S) Create a dictionary using the numbers as the keys, and the values are how often that number appears in the list. For instance, with this list [1,1,5], the dictionary would be frequency = [1:2, 5:1].

0 0
Add a comment Improve this question Transcribed image text
Answer #1
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.

Thank You !

Note: Please update the filename in the below line of code
 filename = 'F:\\numbers.txt'

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

def read_numbers(filename):
    try:
        with open(filename, 'r') as infile:
            numbers = [eval(num) for num in infile.readlines() if len(num.strip()) != 0]
            return numbers
    except:
        return None


def main():
    filename = 'F:\\numbers.txt'
    numbers = read_numbers(filename)
    if numbers is None:
        print('Unable to read data from file: {}'.format(filename))
        return

    frequency_dict = {}
    for num in numbers:
        if num in frequency_dict.keys():
            frequency_dict[num] += 1
        else:
            frequency_dict[num] = 1

    length = len(numbers)
    numbers.sort()
    if length%2==0:
        first_num =numbers[(length-1)//2]
        second_num =numbers[(length-1)//2]
        median = (first_num+second_num)/2
    else:
        median = numbers[length//2]

    max_frequency = max(frequency_dict.values())
    modes=[]
    for num,count in frequency_dict.items():
        if count==max_frequency:
            modes.append(num)

    print('Mode: {}'.format(','.join([str(n) for n in modes])))
    print('Median: {}'.format(median))

main()

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

Add a comment
Know the answer?
Add Answer to:
Ask the user for the name of a file. Read a list of numbers of unknown...
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
  • Could anyone help add to my python code? I now need to calculate the mean and...

    Could anyone help add to my python code? I now need to calculate the mean and median. In this programming assignment you are to extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. You are to create a program called numstat2.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The...

  • Topics: list, file input/output (Python) You will write a program that allows the user to read...

    Topics: list, file input/output (Python) You will write a program that allows the user to read grade data from a text file, view computed statistical values based on the data, and to save the computed statistics to a text file. You will use a list to store the data read in, and for computing the statistics. You must use functions. The data: The user has the option to load a data file. The data consists of integer values representing student...

  • (Python 3) Write a program that reads the contents of a text file. The program should...

    (Python 3) Write a program that reads the contents of a text file. The program should then create a dictionary in which the keys are individual words found in the file and the values are the number of times each word appears and a list that contains the line numbers in the file where the word (the key) is found. Then the program will create another text file. The file should contain an alphabetical listing of the words that are...

  • In python Count the frequency of each word in a text file. Let the user choose...

    In python Count the frequency of each word in a text file. Let the user choose a filename to read. 1. The program will count the frequency with which each word appears in the text. 2. Words which are the spelled the same but differ by case will be combined. 3. Punctuation should be removed 4. If the file does not exist, use a ‘try-execption’ block to handle the error 5. Output will list the words alphabetically, with the word...

  • Use C++ Read numbers from a file and find the sum of numbers. Perform the following...

    Use C++ Read numbers from a file and find the sum of numbers. Perform the following operations Read the console input and create a file.txt. $ in the console input is considered as an end of content for a file. Close the file after creation. Open the file in a read mode and read the numbers from a file and print the sum of all numbers. Input 77 124 2333 $          Where $ indicate an end of console input...

  • java Part 1 Create a NetBeans project that asks for a file name. The file should...

    java Part 1 Create a NetBeans project that asks for a file name. The file should contain an unknown quantity of double numeric values with each number on its own line. There should be no empty lines. Open the file and read the numbers. Print the sum, average, and the count of the numbers. Be sure to label the outputs very clearly. Read the file values as Strings and use Double.parseDouble() to convert them. Part 2 Create a NetBeans project...

  • ***NEED SOLUTION IN PYTHON *** 1. Create a main function which only gets executed when the...

    ***NEED SOLUTION IN PYTHON *** 1. Create a main function which only gets executed when the file is directly executed Hint: def main(): pass if __name__ == '__main__': main() 2. Your programs will use a file called scores.txt to store a bunch of user-provided numbers. These can be int or float but their values must be between 0 and 100. Inside the main function, display the current list of numbers to the user and ask if they want to update...

  • Please use python 3!!!! # Write a function "get_file" to ask the user for a file...

    Please use python 3!!!! # Write a function "get_file" to ask the user for a file name. # Return an open file handle to the users file in "read" mode. # Use exception handling to deal with the case that the user's file # does not exist, printing an error saying "File does not exist, try again" # and trying again f = get_file() f.close()

  • write in python Create a program that will ask the user for a list of integers,...

    write in python Create a program that will ask the user for a list of integers, all on one line separated by a slash. construct a list named "adj_numbers" which contains the users input numbers after subtracting 2 from each number. then print the resulting list. the input statement and print statement are given to you in the starting code. a sample run of the program is as follows: Enter a list of integers, separated by slashes: 7/3/6/8 resulting list:...

  • Code in Python. Ask the user to enter the backend error log file name ("ErrorLog.txt") Read...

    Code in Python. Ask the user to enter the backend error log file name ("ErrorLog.txt") Read the file (Errorlog.txt) [Sun Mar 7 16:02:00 2018] [notice] Apache/1.3.29 (Unix) configured -- resuming normal operations [Sun Mar 7 16:02:00 2018] [info] Server built: Feb 27 2018 13:56:37 [Sun Mar 7 16:02:00 2018] [notice] Accept mutex: sysvsem (Default: sysvsem) [Sun Mar 7 16:05:49 2018] [info] [client 64.242.88.10] (104)Connection reset by peer: client stopped connection before send body completed [Sun Mar 7 16:45:56 2018] [info]...

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
ADVERTISEMENT