1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Lynna [10]
3 years ago
11

Write a program that first reads in the name of an input file and then reads the input file using the file.readlines() method. T

he input file contains an unsorted list of number of seasons followed by the corresponding TV show. Your program should put the contents of the input file into a dictionary where the number of seasons are the keys, and a list of TV shows are the values (since multiple shows could have the same number of seasons).
Sort the dictionary by key (least to greatest) and output the results to a file named output_keys.txt, separating multiple TV shows associated with the same key with a semicolon (;). Next, sort the dictionary by values (alphabetical order), and output the results to a file named output_titles.txt.
Ex: If the input is:
file1.txt
and the contents of file1.txt are:
20
Gunsmoke
30
The Simpsons
10
Will & Grace
14
Dallas
20
Law & Order
12
Murder, She Wrote
the file output_keys.txt should contain:
10: Will & Grace
12: Murder, She Wrote
14: Dallas
20: Gunsmoke; Law & Order
30: The Simpsons
and the file output_titles.txt should contain:
Dallas
Gunsmoke
Law & Order
Murder, She Wrote
The Simpsons
Will & Grace
Note: There is a newline at the end of each output file, and file1.txt is available to download.
currently, my code is:
def readFile(filename):
dict = {}
with open(filename, 'r') as infile:
lines = infile.readlines()
for index in range(0, len(lines) - 1, 2):
if lines[index].strip()=='':continue
count = int(lines[index].strip())
name = lines[index + 1].strip()
if count in dict.keys():
name_list = dict.get(count)
name_list.append(name)
name_list.sort()
else:
dict[count] = [name]
return dict
def output_keys(dict, filename):
with open(filename,'w+') as outfile:
for key in sorted(dict.keys()):
outfile.write('{}: {}\n'.format(key,';'.join(dict.get(key))))
print('{}: {}\n'.format(key,';'.join(dict.get(key))))
def output_titles(dict, filename):
titles = []
for title in dict.values():
titles.extend(title)
with open(filename,'w+') as outfile:
for title in sorted(titles):
outfile.write('{}\n'.format(title))
print(title)
def main():
filename = input()
dict = readFile(filename)
if dict is None:
print('Error: Invalid file name provided: {}'.format(filename))
return
output_filename_1 ='output_keys.txt'
output_filename_2 ='output_titles.txt'
output_keys(dict,output_filename_1)
print()
output_titles(dict,output_filename_2)
main()
The problem is that when I go to submit and the input changes, my output differs.
Output differs. See highlights below. Special character legend Input file2.txt Your output 7: Lux Video Theatre; Medium; Rules of Engagement 8: Barney Miller;Castle; Mama 10: Friends; Modern Family; Smallville;Will & Grace 11: Cheers;The Jeffersons 12: Murder, She Wrote;NYPD Blue 14: Bonanza;Dallas 15: ER 20: Gunsmoke; Law & Order; Law & Order: Special Victims Unit 30: The Simpsons Expected output 7: Rules of Engagement; Medium; Lux Video Theatre 8: Mama; Barney Miller; Castle 10: Will & Grace; Smallville; Modern Family; Friends 11: Cheers; The Jeffersons 12: Murder, She Wrote; NYPD Blue 14: Dallas; Bonanza 15: ER 20: Gunsmoke; Law & Order; Law & Order: Special Victims Unit 30: The Simpsons
Computers and Technology
1 answer:
topjm [15]3 years ago
6 0

Answer:

Explanation:

The following is written in Python. It creates the dictionary as requested and prints it out to the output file as requested using the correct format for various shows with the same number of seasons.The output can be seen in the attached picture below.

mydict = {}

with open("file1.txt", "r") as showFile:

 for line in showFile:

   cleanString = line.strip()

   seasons = 0

   try:

       seasons = int(cleanString)

       print(type(seasons))

   except:

       pass

   if seasons != 0:

       showName = showFile.readline()

       if seasons in mydict:

           mydict[seasons].append(showName.strip())

       else:

           mydict[seasons] = [showName.strip()]

f = open("output.txt", "a")

finalString = ''

for seasons in mydict:

   finalString += str(seasons) + ": "

   for show in mydict[seasons]:

       finalString += show + '; '

   f.write(finalString[:-2] + '\n')

   finalString = ''

f.close()

You might be interested in
Hurry plz
Elanso [62]

Answer: It allows you to locate materials, be aware of your assignments and plan time to get things done.

Hope it helped.

3 0
3 years ago
Which programming scenario would most likely involve this array block?<br><br>SOMEONE PLEASE HELPPP​
attashe74 [19]

Answer:

answer is D

Explanation:

as the block returns the number of array members, the most likely scenario is the last one.

7 0
2 years ago
Which is the best tip for optimizing a TrueView video for viewer engagement?
Ivenika [448]

Answer: (A) Look at engagement rate for targeting and focus on the methods with the highest view through rate

Explanation:

TrueView video enables to post ads in social sites however it is paid by the sponsor only if the ad is viewed fully or in some cases it is viewed only upto 30s of the total size of the video.

So in order to have a larger viewer engagement it is necessary for it to focus on the methods which would garner it highest views.

So option A is correct.

Option B and c are not correct these options would not cater to the needs of the viewer.

5 0
3 years ago
To employ an access key, press and hold down the ____ key as you tap the access key.
Veronika [31]
To employ an access key, press and hold down the Alt key as you tap the access key.
4 0
2 years ago
chapter tests and exams are password protected and must be taken in the lab with a universal kiosk browser
Nesterboy [21]

Yes, when a universal kiosk browser (chrome books) being used in the lab during test and exams is protected by avoiding the students to check the answers in the online.

Explanation:

Universal kiosk browser is helpful to display the content on the web on Chrome books. Chrome books are being safe to make student assessment processes. With the help of this, accessing websites to find the answer to the questions in the assessment process by the student is being avoided.

4 0
2 years ago
Other questions:
  • Which of the following is an occupation management group? Select the choice that best answers the question.
    15·1 answer
  • Which of the following characters at the beginning of a cell signifies to Excel that it should perform a calculation for the val
    13·1 answer
  • Which of the following situations would not require knowledge of networking?
    6·1 answer
  • Karen has opened a new business and is using Google Display Ads to build awareness of her new products. How does Google Display
    6·1 answer
  • An example of software most commonly associated with productivity software is ____.
    12·1 answer
  • Wide area networks are defined by their ability to
    14·2 answers
  • Please help it would mean to world to me❤️ (WORD)
    13·1 answer
  • Secondary hard drive whats its purpose
    5·1 answer
  • It is a JavaScript property used to call a function after a specified time, in milliseconds​
    9·2 answers
  • GIF is example of raster image<br><br><br> RIGHT OR WRONG?
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!