Question

Python programming! Can you redo this program with comment and with clear ident. Part II -...

Python programming!

Can you redo this program with comment and with clear ident.

Part II - Read student names

Ask the user for a file containing possible first names (for this exercise use firstnames.txt attached to the assignment or download it from here: https://drive.google.com/file/d/1U-eOg8znWEoOcIPOWTT8due-svjNdnPc/view?usp=sharing).

Read the file for all the names. Remember the names are not case sensitive and some may appear a few times.

Calculate and output:

  • How many unique names are in the file? Number only
  • Which names appeared more than once? Output the names and how many were omitted.
  • What is the longest and shortest names (in terms of number of letters)? Display the names and lengths (all the possibilities)
  • For each student, assign a first name. What is the most and least common names? How many of each?
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. 

If you are satisfied with the solution, please rate the answer. Let me know for any help with any other questions.

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

def main():
    filename ='F:\\firstnames.txt' # update the file name path here
    name_dict={}
    with open(filename,'r') as infile:
        for line in infile:
            line = line.strip()
            if line in name_dict.keys():
                name_dict[line]+=1
            else:
                name_dict[line]=1

    # uniques names
    counter=0
    for name in name_dict.keys():
        counter+= 1 if name_dict.get(name)==1 else 0
    print('Unique Name Count: {:,}'.format(counter))
    # more than once name:
    print('Names that occured more than once')
    total_names=0
    count_names=0
    for name,count in name_dict.items():
        if count>1:
            print(name)
            count_names+=1
        total_names+=count
    print('There were {:,} names which occured more than once'.format(count_names))
    print('Names omitted count {:,}'.format(total_names-count_names))

    longest=''
    shortest=' '*100
    for name in name_dict.keys():
        if len(name)>len(longest):longest=name
        if len(name)<len(shortest):shortest=name
    print('Longest Name: {} contains {} letters'.format(longest,len(longest)))
    print('Shortest Name: {} contains {} letter(s)'.format(shortest,len(shortest)))
    sorted_names = sorted(name_dict.items(),key=lambda pair:pair[1])
    print('Least Common Name\n====================')
    for name,freq in sorted_names:
        if freq==1: print(name)
    largest_freq = sorted_names[-1][1]
    print('Most Common Name\n====================')
    for name, freq in sorted_names:
        if freq == largest_freq: print(name)

main()

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

Add a comment
Know the answer?
Add Answer to:
Python programming! Can you redo this program with comment and with clear ident. Part II -...
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
  • Consider python please for this problem: I did a python program to read the first 20 lines from a...

    Consider python please for this problem: I did a python program to read the first 20 lines from a file and here is the code holder = 20 lines = 3 file = "123456.pssm" _file = ".txt" while 1:         out_file = input("[OUTPUT] - Please enter the name of file: ") + _file         if (not _file):         print("Filename not specified - Please Try Again\n")         continue     break with open(_file, "a") as e:     with open(file, "r") as f:         for num, line in enumerate(f, 1):                        ...

  • Please write comments with the program. Python programming!! Part III - write student data In this...

    Please write comments with the program. Python programming!! Part III - write student data In this part the program should create a .csv file and fill it with generated student information. The program should: Ask the user for a name for the .csv file (use your own name for the exercise) Create the .csv file with columns named - ID, first-name, GPA Based on the information received in part I, generate and fill students information in the CSV file Tell...

  • (IN PYTHON) You are to develop a Python program that will read the file Grades-1.txt that...

    (IN PYTHON) You are to develop a Python program that will read the file Grades-1.txt that you have been provided on Canvas. That file has a name and 3 grades on each line. You are to ask the user for the name of the file and how many grades there are per line. In this case, it is 3 but your program should work if the files was changed to have more or fewer grades per line. The name and...

  • c++ CSI Lab 6 This program is to be written to accomplish same objectives, as did...

    c++ CSI Lab 6 This program is to be written to accomplish same objectives, as did the program Except this time we modularize our program by writing functions, instead of writing the entire code in main. The program must have the following functions (names and purposes given below). Fot o tentoutefill datere nedefremfite You will have to decide as to what arguments must be passed to the functions and whether such passing must be by value or by reference or...

  • Hello! I have this python Homework due tonight that I don't know how to do. Here...

    Hello! I have this python Homework due tonight that I don't know how to do. Here is a document with the data in the csv file, as I didn't know how to share it https://docs.google.com/document/d/1bDJVR2MqWKInvw5u0r3fOG3-CBmu3BEiPZwlaq_CShQ/edit?usp=sharing Activity #3: On the class website is a CSV file containing weather data from Coulter Field (in Bryan) for 3 years (1 day is missing for some reason!); the data was taken from Weather Underground (wunderground.com). There are different versions of the file for Windows...

  • Need C programming help. I've started to work on the program, however I struggle when using...

    Need C programming help. I've started to work on the program, however I struggle when using files and pointers. Any help is appreciated as I am having a hard time comleting this code. #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LINE 100 #define MAX_NAME 30 int countLinesInFile(FILE* fPtr); int findPlayerByName(char** names, char* target, int size); int findMVP(int* goals, int* assists, int size); void printPlayers(int* goals, int* assists, char** names, int size); void allocateMemory(int** goals, int** assists, char*** names, int size);...

  • Within this C program change the input to a file instead of individual input from the...

    Within this C program change the input to a file instead of individual input from the user. You must take the input file name on the command line. Be sure to have simple error checking to be sure the file exists and was successfully opened. The input file will have the following format: First line: Number of students Second Line: Number of grades Third Line: Student Names, space delimited Fourth+ Line(s): Grades for all students for one assignment, space delimited....

  • C Program In this assignment you'll write a program that encrypts the alphabetic letters in a...

    C Program In this assignment you'll write a program that encrypts the alphabetic letters in a file using the Vigenère cipher. Your program will take two command line parameters containing the names of the file storing the encryption key and the file to be encrypted. The program must generate output to the console (terminal) screen as specified below. Command Line Parameters Your program must compile and run from the command line. The program executable must be named “vigenere” (all lower...

  • PYTHON. Continues off another code(other code is below). I don't understand this. Someone please help! Comment...

    PYTHON. Continues off another code(other code is below). I don't understand this. Someone please help! Comment the lines please so I can understand. There are short and med files lengths for each the list of names/ids and then search id file. These are the input files: https://codeshare.io/aVQd46 https://codeshare.io/5M3XnR https://codeshare.io/2W684E https://codeshare.io/5RJwZ4 LinkedList ADT to store student records(code is below). Using LinkedList ADT instead of the Python List. You will need to use the Student ADT(code is below) Imports the Student class...

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