Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a function named area_codes that prints information about the most commonly occurring area code in a file of telephone numbers. Your function accepts

Write a function named area_codes that prints information about the most commonly occurring area code in a

Write a function named area_codes that prints information about the most commonly occurring area code in a file of telephone numbers. Your function accepts a string parameter representing a filename of input. The input file contains a collection of telephone numbers, one per line, in the following format. Each phone number begins with a three-digit area code. You may assume that every line of the file is in exactly the format shown, without any other characters or formatting or spacing, and that every phone number in the file is unique. 650-723-2273 206-685-2181 800-356-9377 800-347-3288 650-725-7411 520-297-6312 206-543-1695 800-266-2278 206-543-2969 Your function should open and read the contents of this input file and figure out which area code occurs most frequently in the data. If multiple area codes are tied for being the most frequent, print the numerically smallest of the ones that tied. For example, in the data at right, the most common area codes are 800 and 206, each of which has 3 phone numbers with that area code, so your function should consider 206 to be the most commonly occurring area code. After determining which is the most common area code, your function should prout all of the phone numbers from that area code, in sorted numerical order, one per line. For example, if the input above at right comes from a file named phonenumbers.txt, then the call of area_codes ("phone numbers, txt") should print the following console output: 206-543-1695 206-543-2969 206-685-2181 If the file is missing or unreadable, your function should produce no output. If the file exists, you may assume that it contains at least one phone number, that every line of input in the file is in the exact valid format described above, and that every phone number in the file will be unique. Constraints: You may open and read the file only once. Do not re-open it or rewind the stream. You should choose an efficient solution. Choose data structures intelligently and use them properly. You may create one collection (stack, queue, set, map, etc.) or nested/compound structure as auxiliary storage. A nested structure, such as a set of lists, counts as one collection. A temporary collection variable that is merely a replica or reference to some other collection (such as, v= mylist.remove()) is fine and does not count as a second structure. (You can have as many simple variables as you like, such as ints or strings.)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

def areacodesfilename try datamap of lists maxCount0 maxCountcode999 wi... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Java Programming

Authors: Joyce Farrell

9th edition

1337397075, 978-1337397070

More Books

Students also viewed these Programming questions