Question

In this part, you will use a file containing various beasts that were featured in different...

In this part, you will use a file containing various beasts that were featured in different titles in the Harry Potter series of books and movies. We give you a simple data file called titles.txt to test your program. Each line of the file contains a title (movie or book), followed by all the beasts featured in this title. For example, the following line: Harry Potter and the Goblet of Fire|Hippocampus|Merpeople|Niffler mentions that the title Harry Potter and the Goblet of Fire featured three beasts: Hippocampus, Merpeople, and Niffler. You can assume that the data is valid and that splitting on the '|' string will correctly parse the lines in titles.txt. Your program must do the following: Read the name of a title from the user. The user will enter any part of the title (in any case, upper or lower). Find the first title in the list that contains the input string. For example, the user may enter FIRE, which will match the above line. If no match is found, print the message. If a match is found, then do the following:

– Print all the beasts that were featured in this title in lexicographical order.

– Print all the other titles that have at least one beast in common with this title in lexicographical order.

– Print all the beasts that were featured only in this title (i.e., no other title has these beasts), again in lexicographical order. Consider which set operation will help with the last two operations . . .

Once you have finished processing the input (match or no match), ask for another input, until the user inputs stop.

How to write this problem in python?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find below code in python3.

 def main(): file1 = open('myfile.txt', 'r') Lines = file1.readlines() map1 = {} for l in Lines: spl = l.split('|') map1[spl[0]]=set(spl[1:]) title = input("Enter name of title: ") res = [val for key, val in map1.items() if title.lower() in key.lower()] set1 = res[0] print("All the beasts that were featured in this title in lexicographical order.") print(sorted(set1)) print("\nAll the other titles that have at least one beast in common with this title in lexicographical order.") res2 = [key for key, val in map1.items() if len(val.intersection(set1))>0] print(sorted(res2)) print("\nAll the beasts that were featured only in this title.") allbeast = set([a for key, val in map1.items() for a in val ]) otherbeasts = allbeast.difference(set1) print(set1.difference(otherbeasts)) main()
Add a comment
Know the answer?
Add Answer to:
In this part, you will use a file containing various beasts that were featured in different...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Exceptions Suppose a library is processing an input file containing the titles of books in...

    Java Exceptions Suppose a library is processing an input file containing the titles of books in order to remove duplicates. Write a program that reads all of the titles from an input file called bookTitles.inp and writes them to an output file called uniqueTitles.out. When complete, the output file should contain all unique titles found in the input file. Create the input file using Notepad or another text editor, with one title per line. Make sure you have a number...

  • Creat a C Program that Reads words from a file called words, which contains one word...

    Creat a C Program that Reads words from a file called words, which contains one word per line.Each word has maximum length to M.The number of words in the file is equal to N. Incert each word to Binary Search Tree with node name dictionary.Each node of the tree must contain one word.The left child must contain a word that it is lexicographically smaller while the right child contains a lexicographically bigger one. 1) When you finish reading the file,...

  • Write a PYTHON program that asks the user for the name of the file. The program...

    Write a PYTHON program that asks the user for the name of the file. The program should write the contents of this input file to an output file. In the output file, each line should be preceded with a line number followed by a colon. The output file will have the same name as the input filename, preceded by “ln” (for linenumbers). Be sure to use Try/except to catch all exceptions. For example, when prompted, if the user specifies “sampleprogram.py”...

  • Write a program **(IN C)** that displays all the phone numbers in a file that match the area code...

    Write a program **(IN C)** that displays all the phone numbers in a file that match the area code that the user is searching for. The program prompts the user to enter the phone number and the name of a file. The program writes the matching phone numbers to the output file. For example, Enter the file name: phone_numbers.txt Enter the area code: 813 Output: encoded words are written to file: 813_phone_numbers.txt The program reads the content of the file...

  • Capitalization JAVA In this program, you will read a file line-by-line. For each line of data...

    Capitalization JAVA In this program, you will read a file line-by-line. For each line of data (a string), you will process the words (or tokens) of that line one at a time. Your program will capitalize each word and print them to the screen separated by a single space. You will then print a single linefeed (i.e., newline character) after processing each line – thus your program will maintain the same line breaks as the input file. Your program should...

  • Given the attached data file, write a payload class Book that represents a book. The fields...

    Given the attached data file, write a payload class Book that represents a book. The fields are id, title, publisher, type, price and paperback. Most should be String fields, but price should be a double and paperback should be a boolean. The one constructor should take a Scanner parameter opened to the file. It should input each of the fields using the Scanner. Note that for the paperback field, you should input a String, and set the field to true...

  • Task 1: Write a Python program that takes as input from the user the name of a file containing po...

    python code: Task 1: Write a Python program that takes as input from the user the name of a file containing postcode/location information. Each line in the file consists of the postcode followed by a tab followed by a comma-separated list of locations that have that postcode. For example, the file Small.txt contains: 3015 Newport,South Kingsville,Spotswood 3016 Williamstown 3018 Altona,Seaholme 3019 3021 Albanvale,Kealba,Kings Park,St Albans Braybrook, Robinson Your program should create a list of postcode/location pairs with each pair stored...

  • Basic C program Problem: In this assignment, you have to read a text file (in.txt) that...

    Basic C program Problem: In this assignment, you have to read a text file (in.txt) that contains a set of point coordinates (x,y). The first line of the file contains the number of points (N) and then each line of the file contains x and y values that are separated by space. The value of x and y are an integer. They can be both negative and positive numbers. You have to sort those points in x-axis major order and...

  • Finish FormatJavaProgram.java that prompts the user for a file name and assumes that the file contains...

    Finish FormatJavaProgram.java that prompts the user for a file name and assumes that the file contains a Java program. Your program should read the file (e.g., InputFile.java) and output its contents properly indented to ProgramName_Formatted.java (e.g., InputFile_Formatted.java). (Note: It will be up to the user to change the name appropriately for compilation later.) When you see a left-brace character ({) in the file, increase your indentation level by NUM_SPACES spaces. When you see a right-brace character (}), decrease your indentation...

  • Part 1 The purpose of this part of the assignment is to give you practice in...

    Part 1 The purpose of this part of the assignment is to give you practice in creating a class. You will develop a program that creates a class for a book. The main program will simply test this class. The class will have the following data members: A string for the name of the author A string for the book title A long integer for the ISBN The class will have the following member functions (details about each one are...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT