Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

ef read _ input _ file ( input _ file ) : Reads the input file and stores the data in a

ef read_input_file(input_file):
"""
Reads the input file and stores the data in a dictionary.
Args:
input_file (str): The name of the input file.
Returns:
dict: A dictionary where keys are the number of seasons and values are lists of TV shows.
"""
data_dict ={} # Initialize an empty dictionary to store the data
with open(input_file, 'r') as file: # Open the input file in read mode
lines = file.readlines() # Read all lines from the file
for i in range(0, len(lines),2): # Iterate over pairs of lines
seasons = int(lines[i].strip()) # Parse the number of seasons
show = lines[i+1].strip() # Parse the TV show
if seasons in data_dict: # Check if the number of seasons already exists in the dictionary
data_dict[seasons].append(show) # Append the show to the existing list of shows
else:
data_dict[seasons]=[show] # Create a new key-value pair
return data_dict # Return the populated dictionary
def write_output_keys(output_file, data_dict):
"""
Writes the sorted dictionary keys and associated TV shows to an output file.
Args:
output_file (str): The name of the output file.
data_dict (dict): The dictionary containing the TV show data.
"""
with open(output_file, 'w') as file: # Open the output file in write mode
for key in sorted(data_dict.keys()): # Iterate over sorted keys of the dictionary
shows ="; ".join(data_dict[key]) # Join the shows associated with the key
file.write(f"{key}: {shows}
") # Write the key-value pair to the output file
def write_output_titles(output_file, data_dict):
"""
Writes the sorted TV show titles to an output file.
Args:
output_file (str): The name of the output file.
data_dict (dict): The dictionary containing the TV show data.
"""
with open(output_file, 'w') as file: # Open the output file in write mode
for key in sorted(data_dict.keys()): # Iterate over sorted keys of the dictionary
for show in sorted(data_dict[key]): # Iterate over sorted shows associated with the key
file.write(f"{show}") # Write the show title
file.write("
") # Write a new line
def main():
"""
Main function to orchestrate the program execution.
"""
input_file = input("Enter the name of the input file: ") # Prompt user for input file name
output_keys_file = "output_keys.txt" # Set output file name for sorted keys
output_titles_file = "output_titles.txt" # Set output file name for sorted titles
# Read input file and store data in a dictionary
data_dict = read_input_file(input_file)
# Write sorted data to output files
write_output_keys(output_keys_file, data_dict)
write_output_titles(output_titles_file, data_dict)
# Print success message
print(f"Results have been written to {output_keys_file} and {output_titles_file}")
if __name__=="__main__":
main() # Call the main function to execute the program

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2014 Integration Services

Authors: Brian Knight, Devin Knight

1st Edition

1118850904, 9781118850909

Students also viewed these Databases questions

Question

How do you resist?

Answered: 1 week ago