Starting from:

$25

Files and Sets solution

Overview You are not expected to use dictionaries to answer this part, but you can. It is your call. Overall, you may find it conceptually easier not to use dictionaries and read the input file a few times to construct your answer. You must use sets. It will become quite obvious why as you start working on the program. This program brings together many little things you have learnt in different parts of this class. But, we will not tell you how to do the whole thing. My solution is a total of 65 lines including white lines for readability. So, it is not a long program if you plan it properly. I recommend constructing it in little pieces.

Problem: Villains and stories You are given a file called DrWhoVillains.tsv. It is a TAB separated file containing villains in a science fiction show called Dr. Who. The show is actually known for its extremely low budget and silly creature effects. Look at the picture of a Dalek (one of the main villains) above to see this for yourself. Each line of this file is a different villain, containing the following information (separated by TABs): 1. Villain: name of the villain (e.g. Dalek)
2. Year first: the first year this villain was introduced
3. Year last: the last year this villain was shown
4. Doc. no.: the list of doctor ids with this villain, seperated by commas
5. Doctor actor: the list of actors for each doctor above
6. Episodes: number of episodes featuring this villain
7. Stories, total: number of stories featuring this villain, each story may span many episodes
8. Motivation (invasion earth, kill humans, etc): the description of the main motivation of this villain
9. Story titles: the titles of the stores that the villain was involved with, a list of strings separated by commas. Your program must do the following: • Read through the file to find the villain names and the (set of) stories for each villain. • Find the top 10 most popular villains: those with top 10 highest number of stories listed in the file and display them to the user, numbered 1 to 10, in the decreasing order of popularity. Then, ask the user for a number.
• If the user enters -1, the program exits.
• If the user enters something other than a number between 1 to 10, or -1, the program displays the top 10 list again and asks for another input.
• If the user enters a valid option, numbered between 1 to 10, then you find the corresponding villain and display the following: – The number of stories with this villain – The names of all other villains that were in a story with this chosen villain, in alphabetical order. – The names of all story lines that featured only this villain, and no other villain. If none, you should display there is no such story line. Once you are done displaying this, you will print again the top 10 and ask for the next input, until user enters -1. One thing you want to do is to display the names of matching villains and stories in a reasonable looking paragraph. You do not have to match the output in the submission server exactly. You should however try to break the output into multiple lines of similar (but not necessarily identical) length. We will not take points off for breaking lines mid-word. There are many ways to do this. For example, what we did in HW#4 part 3 is one way. My solution involves a module called textwrap which wraps text into a list of multiple lines. Try the following to see if you can figure out how to use this module. import textwrap x = '+'*200 textwrap.wrap(x,40) A final requirement: your program must following the following structure (in preparation for a life after Python): ## all imports import textwrap ##if you use it ## all functions first def function1(x): return x + 1 def function2(y): return y -1 ## all your main code after this if statement if __name__ == '__main__': z = 10 print function1(z) print function2(z) This code has three main sections: import statements come first, followed by all function definitions. This is what we have been enforcing so far. In addition, there is no code that uses these functions or defines variables above the line: if __name__ == '__main__':. All your main code lies underneath this if statement. Of course, you add comments to make this even more readable, especially describing what each function does. Almost all programming languages require a separation of functions and the main code. Think of functions helping the main code. The main code is typically quite small and it serves as a traffic police, deciding on which functions to call and when. It will help tremendously in this homework if you can start thinking of what the main code should do first. Please show your code to your TAs and mentors to get advice on structuring it right. Then, slowly add complexity and refine the main function by adding different functions. Note: If you structure your code this way, whenever you import this module, none of the code under the if __name__ == '__main__': line will be executed. This means, you can reuse the functions in this program in future homeworks. Deliverables – Final Check Here is a summary of all the requirements for this homework. Double check this before you submit: • Your program must use sets, that will also help you quite a lot.
• Submit a single file called hw6.py that assumes the existence of a text file called DrWhoVillains.tsv.
• The main loop of the program should be able to handle incorrect entry, repeated calls to display the top 10 villains, and exit when -1 entered.
• Print all input that you read immediately.
• The outputs containing multiple items like villains or stories should be a reasonable looking paragraph, wrapping long lines into multiple lines. But the output does not have to be a perfect match for the submission server. Lines do not have to be exactly the same length either.
• Your program should print the villain names and story names in alphabetical order.
• Your program should have the above structure. We will penalize incorrect program structure. Expected output Below you can see the expected functioning of this program. 1. Daleks 2. Cybermen 3. Master (the) 4. Dalek Supreme (Supreme Dalek), inc Progenitor 5. Sontarans 6. Ice Warriors (inc benevolent appearences) 7. Davros 8. Silurians (exc Sea Devils) 9. Kovarian, Madame 10. Cyber-leader Please enter a number between 1 and 10, or -1 to end Enter a villain == stop stop 1. Daleks 2. Cybermen 3. Master (the) 4. Dalek Supreme (Supreme Dalek), inc Progenitor 5. Sontarans 6. Ice Warriors (inc benevolent appearences) 7. Davros 8. Silurians (exc Sea Devils) 9. Kovarian, Madame 10. Cyber-leader Please enter a number between 1 and 10, or -1 to end Enter a villain == 5 5 Sontarans in 9 stories, with the following other villains: ================================================== Alliance - onscreen - Daleks, Cybermen, Sontarans & Autons, (cameos Silurians, Sycorax, Judoon, Hoix, Roboforms, microcameos Blowfish, Uvodni, Weevils), Atraxi, Borusa, Chessene of the Franzine Grig, Cybermen, Dalek Supreme (Supreme Dalek), inc Progenitor, Daleks, Dastari, Joinson, Gray, Steve, Harris, Private Carl, Headless Monks, Irongron, Kovarian, Madame, Manton, Colonel, Martha Jones- Clone, Master (the), Naismith, Joshua, Rassilon, Rattigan, Luke, Red Carnivorous Maw, Shockeye of the Quawncing Grig, Silurians (exc Sea Devils), Staal, Stike, Vardan, Varl, Varla The stories that only features Sontarans are: ================================================== The Sontaran Experiment 1. Daleks 2. Cybermen 3. Master (the) 4. Dalek Supreme (Supreme Dalek), inc Progenitor 5. Sontarans 6. Ice Warriors (inc benevolent appearences) 7. Davros 8. Silurians (exc Sea Devils) 9. Kovarian, Madame 10. Cyber-leader Please enter a number between 1 and 10, or -1 to end Enter a villain == 7 7 Davros in 6 stories, with the following other villains: ================================================== Dalek Supreme (Supreme Dalek), inc Progenitor, Daleks, Lytton, Commander Gustav, Movellans, Nyder, Sharrel There are no stories with only this villain ================================================== 1. Daleks 2. Cybermen 3. Master (the) 4. Dalek Supreme (Supreme Dalek), inc Progenitor 5. Sontarans 6. Ice Warriors (inc benevolent appearences) 7. Davros 8. Silurians (exc Sea Devils) 9. Kovarian, Madame 10. Cyber-leader Please enter a number between 1 and 10, or -1 to end Enter a villain == -1 -1 Exiting

More products