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
Determine if the following errors are systematic or random. Justify your response. (a) Effect of temperature on the circuitry of
k0ka [10]

Answer:

a) temperature: random error

b) parallax: systematic error

c) using incorrect value: systematic error

Explanation:

Systematic errors are associated with faulty calibration or reading of the equipments used and they could be avoided refining your method.

4 0
3 years ago
A sinusoidal wave of frequency 420 Hz has a speed of 310 m/s. (a) How far apart are two points that differ in phase by π/8 rad?
Olin [163]

Answer:

a) Two points that differ in phase by π/8 rad are 0.0461 m apart.

b) The phase difference between two displacements at a certain point at times 1.6 ms apart is 4π/3.

Explanation:

f = 420 Hz, v = 310 m/s, λ = wavelength = ?

v = fλ

λ = v/f = 310/420 = 0.738 m

T = periodic time of the wave = 1/420 = 0.00238 s = 0.0024 s = 2.4 ms

a) Two points that differ in phase by π/8 rad

In terms of the wavelength of the wave, this is equivalent to [(π/8)/2π] fraction of a wavelength,

[(π/8)/2π] = 1/16 of a wavelength = (1/16) × 0.738 = 0.0461 m

b) two displacements at times 1.6 ms apart.

In terms of periodic time, 1.6ms is (1.6/2.4) fraction of the periodic time.

1.6/2.4 = 2/3.

This means those two points are 2/3 fraction of a periodic time away from each other.

1 complete wave = 2π rad

Points 2/3 fraction of a wave from each other will have a phase difference of 2/3 × 2π = 4π/3.

8 0
3 years ago
In javaWrite a program that simulates flipping a coin to make decisions. The input is how many decisions are needed, and the out
Pavel [41]

Answer:

// Program is written in Java Programming Language

// Comments are used for explanatory purpose

import java.util.*;

public class FlipCoin

{

public static void main(String[] args)

{

// Declare Scanner

Scanner input = new Scanner (System.in);

int flips;

// Prompt to enter number of toss or flips

System.out.print("Number of Flips: ");

flips = input.nextInt();

if (flips > 0)

{

HeadsOrTails();

}

}

}

public static String HeadsOrTails(Random rand)

{

// Simulate the coin tosses.

for (int count = 0; count < flips; count++)

{

rand = new Random();

if (rand.nextInt(2) == 0) {

System.out.println("Tails"); }

else {

System.out.println("Heads"); }

rand = 0;

}

}

7 0
3 years ago
Thermal energy is...
Yuki888 [10]
B because thermal has to do with temperature and it’s the amount of kinetic and potential energy in and object
4 0
2 years ago
Read 2 more answers
Question 1: Final Results = What are the values of the resistances such that the gain = -100, Rin = 1 MI2. Don't use resistances
lidiya [134]

Answer:

Explanation:

In a study of algebra, you will encounter many families of equations, or groups of

equations that share common characteristics. Of interest to us here is the family of

linear equations in one variable, a study that lays the foundation for understanding

more advanced families. In addition to solving linear equations, we’ll use the skills we

develop to solve for a specified variable in a formula, a practice widely used in science,

business, industry, and research.

A. Solving Linear Equations Using Properties of Equality

An equation is a statement that two expressions are

equal. From the expressions and

we can form the equation

which is a linear equation in one variable. To solve

an equation, we attempt to find a specific input or xvalue that will make the equation true, meaning the

left-hand expression will be equal to the right. Using

Table 1.1, we find that is a

true equation when x is replaced by 2, and is a false

equation otherwise. Replacement values that make

the equation true are called solutions or roots of the equation.

4 0
2 years ago
Other questions:
  • For the SR-latch below high levels of Set and Reset result in Q= 1 and 0, respectively. The next state is unknown when both inpu
    12·1 answer
  • Block A is released from rest and slides down the frictionless ramp to the loop. The maximum height h of the loop is the same as
    6·1 answer
  • A company that produces footballs uses a proprietary mixture of ideal gases to inflate their footballs. If the temperature of 23
    11·1 answer
  • A heat recovery system​ (HRS) is used to conserve heat from the surroundings and supply it to the Mars Rover. The HRS fluid loop
    12·1 answer
  • Fill in the blank to correctly complete the statement below.
    6·1 answer
  • Thermodynamics fill in the blanks The swimming pool at the local YMCA holds roughly 749511.5 L (749511.5 kg) of water and is kep
    6·1 answer
  • Problematic substances mechanical engeneering???
    6·1 answer
  • The pressure at the bottom of an 18 ft deep storage tank for gasoline is how much greater than at the top? Express your answer i
    15·1 answer
  • A type of adjustable square that can be used to set, test, and transfer angles is called a
    5·1 answer
  • Engineers please help im not good when it comes to drawing​
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!