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
Andreyy89
3 years ago
6

9.21 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 (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:
Engineering
1 answer:
natka813 [3]3 years ago
8 0

The following code or the program will be used

<u>Explanation:</u>

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]

           print(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('Enter input file name: ')

   dict = readFile(filename)

   if dict is None:

       print('Error: Invalid file name provided: {}'.format(filename))

       return

   print(dict)

   output_filename_1 ='output_keys.txt'

   output_filename_2 ='output_titles.txt'

   output_keys(dict,output_filename_1)

   output_titles(dict,output_filename_2)  

main()

You might be interested in
Uestion 10
Dimas [21]
Tryna boost my score for college stuff could you give me the brainiest and a thanks? Hope you find your answer your looking for!
6 0
3 years ago
Which of the following headlines about the French Revolution is not true?
Sever21 [200]

Answer:

d) "Napoleon Declares Himself Holy Roman Emperor"

Explanation:

The French Revolution is defined as a period of the major social upheaval which began in the year 1787 and lasted till year 1799. This revolution completely redefined the the very nature of the political power in France. and also the relationship between the rulers of France and the people they governed.

The 1789 Estates-General was the 1st meeting since year 1614 of the French Estates-General. It is a general assembly which represents the French estates of  realm.

During the French revolution was at peak, the National Assembly issued the Declaration of the rights of the man to the public.

Maximilien Robespierre was considered to be one of the most influential figure and most important statesman during the French Revolution.

Thus all the options (a),(b) and (c) are headlines about the French Revolution, except option (d).

3 0
3 years ago
1. Present the pros and cons of an employer participating in the OSHA Voluntary Protection Program (VPP) or the Star Program. Ba
algol13

Answer:

kohjhfg do gm gm gm tm yl tm yk 9k umm yk to gm I'm y dm b do di Cy do do do do di du t6

3 0
2 years ago
Read 2 more answers
2. Other igneous rock forms from lava that cools quickly on Earth’s surface. Classify the rock as either intrusive or extrusive,
Darya [45]

Answer:

extrusive,

Explanation:

lava exlodes outwards, making it extrusive and not intrusive.

5 0
3 years ago
java Your program class should be called RomanNumerals Write a program that asks the user to enter a number within the range of
Ann [662]

Answer:

// Scanner class is imported to allow program

// receive input

import java.util.Scanner;

// RomanNumerals class is defined

public class RomanNumerals {

   // main method that signify beginning of program execution

   public static void main(String args[]) {

       // Scanner object scan is created

       // it receive input via keyboard

       Scanner scan = new Scanner(System.in);

       // Prompt is display asking the user to enter number

       System.out.println("Enter your number: ");

       // the user input is stored at numberOfOrder

       int number = scan.nextInt();

     

           // switch statement which takes number as argument

           // the switch statement output the correct roman numeral

           // depending on user input

          switch(number){

           case 1:

               System.out.println("I");

               break;

           case 2:

               System.out.println("II");

               break;

           case 3:

               System.out.println("III");

               break;

           case 4:

               System.out.println("IV");

               break;

           case 5:

               System.out.println("V");

               break;

           case 6:

               System.out.println("VI");

               break;

           case 7:

               System.out.println("VII");

               break;

           case 8:

               System.out.println("VIII");

               break;

           case 9:

               System.out.println("IX");

               break;

           case 10:

               System.out.println("X");

               break;

           // this part is executed if user input is not between 1 to 10

           default:

               System.out.println("Error. Number must be between 1 - 10.");

     }

   }

}

Explanation:

The program is well commented. A sample image of program output is attached.

The switch statement takes the user input (number) as argument as it goes through each case block in the switch statement and match with the corresponding case to output the roman version of that number. If the number is greater 10 or less than 1; the default block is executed and it display an error message telling the user that number must be between 1 - 10.

6 0
3 years ago
Read 2 more answers
Other questions:
  • IN JAVA,
    6·1 answer
  • What does it mean when it says technology is A dynamic process
    7·1 answer
  • Acredit report summarizes a person's Acredit score is measure of a person's as a borrower a factor that contributes to a person'
    15·1 answer
  • Which of these are factors of 121?
    13·2 answers
  • A/an_ Oscilloscope uses a cathode ray tube and displays all voltages.
    8·1 answer
  • How would you expect an increase in the austenite grain size to affect the hardenability of a steel alloy? Why?
    13·1 answer
  • Verify if 83 is a term in the arithmetic sequence 11,15,19,23
    15·1 answer
  • the coil polarity in a waste spark system is determined by the direction in which the coil is wound (left hand rule for conventi
    15·1 answer
  • 10.) A solid circular titanium control rod at 7,000 lb axial tension force where stress must not
    15·1 answer
  • The thrust angle is checked by referencing
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!