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
Which bug type would you suggest for the following bug? The button label "Add to cart" is not fully visible.
harina [27]
Butterfly one I forgot the name metapods evolution
3 0
3 years ago
Three healthcare firms jointly own and share the same cloud resources to meet their computing needs. Which cloud model does this
Stels [109]

Answer:

yes

Explanation:

yall share the same thing

5 0
2 years ago
________ are devices in a computer that are in either the on or off state
Vanyuwa [196]
The appropriate response is Electrical Switches. These are electromechanical gadgets that are utilized as a part of electrical circuits to control, recognize when frameworks are outside their working reaches, flag controllers of the whereabouts of machine individuals and work pieces, give a way to manual control of machine and process capacities, control lighting, et cetera.
6 0
3 years ago
What is true about music from the Romantic period?
avanturin [10]
The answer to the given question above is the third option: <span>It was longer and less constrained than the music of the Classical period. The Romantic Period in music actually has more freedom in design and form. During this period, music were even more artistic and intellectual as well. Romantic Period occurred the late 18th century until early 19th century.</span>
8 0
2 years ago
Read 2 more answers
Given parameters b and h which stand for the base and the height of an isosceles triangle (i.e., a triangle that has two equal s
schepotkina [342]

Answer:

The area of the triangle is calculated as thus:

Area = 0.5 * b * h

To calculate the perimeter of the triangle, the measurement of the slant height has to be derived;

Let s represent the slant height;

Dividing the triangle into 2 gives a right angled triangle;

The slant height, s is calculated using Pythagoras theorem as thus

s = \sqrt{b^2 + h^2}

The perimeter of the triangle is then calculated as thus;

Perimeter = s + s + b

Perimeter = \sqrt{b^2 + h^2} + \sqrt{b^2 + h^2} +b

Perimeter = 2\sqrt{b^2 + h^2} + b

For the volume of the cone,

when the triangle is spin, the base of the triangle forms the diameter of the cone;

Volume = \frac{1}{3} \pi * r^2 * h

Where r = \frac{1}{2} * diameter

So, r = \frac{1}{2}b

So, Volume = \frac{1}{3} \pi * (\frac{b}{2})^2 * h

Base on the above illustrations, the program is as follows;

#include<iostream>

#include<cmath>

using namespace std;

void CalcArea(double b, double h)

{

//Calculate Area

double Area = 0.5 * b * h;

//Print Area

cout<<"Area = "<<Area<<endl;

}

void CalcPerimeter(double b, double h)

{

//Calculate Perimeter

double Perimeter = 2 * sqrt(pow(h,2)+pow((0.5 * b),2)) + b;

//Print Perimeter

cout<<"Perimeter = "<<Perimeter<<endl;

}

void CalcVolume(double b, double h)

{

//Calculate Volume

double Volume = (1.0/3.0) * (22.0/7.0) * pow((0.5 * b),2) * h;

//Print Volume

cout<<"Volume = "<<Volume<<endl;

}

int main()

{

double b, h;

//Prompt User for input

cout<<"Base: ";

cin>>b;

cout<<"Height: ";

cin>>h;

//Call CalcVolume function

CalcVolume(b,h);

//Call CalcArea function

CalcArea(b,h);

//Call CalcPerimeter function

CalcPerimeter(b,h);

 

return 0;

}

3 0
2 years ago
Other questions:
  • Nadia would like to find text in her current document that differentiates CompanyABC from companyabc. Which option should she us
    8·2 answers
  • What are motion graphics?
    9·1 answer
  • Which vendor did IBM select to create the operating system for the IBM PC?
    6·1 answer
  • Your school has been declared a school of technology
    15·1 answer
  • The smallest unit of storage is​
    15·1 answer
  • Which function would you use to make sure all names are prepared for a mailing label? TODAY UPPER PROPER LOWER
    11·1 answer
  • A human interest story is an example of hard news.<br> O True<br> O False HEL
    15·1 answer
  • You are writing a program using the Java language. Which of the following is a requirement of Java syntax?
    6·1 answer
  • I need simple app ideas for basic problems<br>​
    13·1 answer
  • Non linear editing can cause _____ where edit and re-edit and re-edit again can cause video to be less true than the original ma
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!