homeworkstudyhelp

Our Services

Get 15% Discount on your First Order

4.9 LAB: Sorting TV Shows (dictionaries and lists)   Write a…

4.9 LAB: Sorting TV Shows (dictionaries and lists)

 

Write a program that first reads in the name of an input file and then reads the input file using the file.readlines() method. The 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 (greatest to least) and output the results to a file named output_keys.txt. Separate multiple TV shows associated with the same key with a semicolon (;), ordering by appearance in the input file. Next, sort the dictionary by values (in reverse 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: End each output file with a newline, and file1.txt is available to download.
 

my code so far:

file_name = input()
user_file = open(file_name)
input_list = user_file.readlines()

my_diction = {}

for index in range (len(input_list)):
   if index % 2 == 0:
       diction_key = input_list[index].strip(‘\n’)
       if diction_key not in my_diction:
           my_diction[diction_key] = []
   else:
       my_diction[diction_key].append(input_list[index].strip(‘\n’))

sorted_keys = sorted(my_diction.keys())
output_file = ”
tv_show_list = []

for d_key in sorted_keys:
   output_file += d_key.lstrip(‘0’) + ‘: ‘
   for tvshow in my_diction[d_key]:
       output_file += tvshow + ‘; ‘
       tv_show_list.append(tvshow)
   output_file = output_file[:-2] + ‘\n’
print(output_file)
file = open(“output_keys.txt”, “w”)
file.write(output_file)
file.close()

tv_show_list.sort()

sorted_list = ”
for tv_show in tv_show_list:
   sorted_list += tv_show + ‘\n’
print(sorted_list)
file = open(“output_titles.txt”, “w”)
file.write(sorted_list)
file.close

 

my output is incorrect:

1:Compare outputkeyboard_arrow_up

0 / 1

 

10: Will & Grace 12: Murder, She Wrote 14: Dallas 20: Gunsmoke; Law & Order 30: The Simpsons Dallas Gunsmoke Law & Order Murder, She Wrote The Simpsons Will & Grace

 

Output differs. See highlights below. Special character legend

 

Input

file1.txt

 

Your file content

10: Will & Grace 12: Murder, She Wrote 14: Dallas 20: Gunsmoke; Law & Order 30: The Simpsons

 

Expected file content

30: The Simpsons 20: Gunsmoke; Law & Order 14: Dallas 12: Murder, She Wrote 10: Will & Grace

 

2:Compare outputkeyboard_arrow_up

0 / 1

 

10: Will & Grace 12: Murder, She Wrote 14: Dallas 20: Gunsmoke; Law & Order 30: The Simpsons Dallas Gunsmoke Law & Order Murder, She Wrote The Simpsons Will & Grace

 

Output differs. See highlights below. Special character legend

 

Input

file1.txt

 

Your file content

Dallas Gunsmoke Law & Order Murder, She Wrote The Simpsons Will & Grace

 

Expected file content

Will & Grace The Simpsons Murder, She Wrote Law & Order Gunsmoke Dallas

 

 please help where am I go wrong here?

this is my code that I have written.

 

Share This Post

Email
WhatsApp
Facebook
Twitter
LinkedIn
Pinterest
Reddit

Order a Similar Paper and get 15% Discount on your First Order

Related Questions

Explain to your friends what the System Development Life Cycle is….

Explain to your friends what the System Development Life Cycle is. Then motivate the importance of each core process of the System Development Life Cycle (SDLC). Substantiate your arguments with well researched examples illustrating the consequences of engaging in these processes negligently.

Good afternoon   My MATLAB optimization code for a turbojet engine…

Good afternoon   My MATLAB optimization code for a turbojet engine in order to maximize and minimize the following functions with various constraints is not working and even when plotting in 3D it appears some errors:Could someone help me get it working please?   function [f, g] = turbojet_objective(x) %

Overview Object modeling is an important part of system analysis…

Overview Object modeling is an important part of system analysis and design. Since object-oriented languages such as Java and C++ have become more common, systems analysts also need to be able to design a system from an object-oriented perspective. Object modeling involves thinking about the objects and classes that will

1)Requirement clarity index for a project is computed based on the…

1)Requirement clarity index for a project is computed based on the understanding of requirements by: A)Development Team B) Testing team C)Business Analysts D)Development and testing teams 2)Techniques used by static code analysis tool is: A)Control Analysis B)Fault/Failure Analysis C)Interface Analysis D)All the above 3)If a application is a most critical