check the pdf
Department of Computer Science 176 Thorvaldson Building 110 Science Place, Saskatoon, SK, S7N 5C9, Canada Telephine: (306) 966-4886, Facimile: (306) 966-4884 CMPT 141 Winter 2019 Introduction to Computer Science Assignment 5 Dictionaries and File I/O Date Due: February 28, 2019, 6:00pm Total Marks: 23 General Instructions • This assignment is individual work. You may discuss questions and problems with anyone, but the work you hand in for this assignment must be your own work. • For this assignment, you will hand in one single �le, called a5.py. Put your name and student number at the top of the document. This assists the markers in their work. Failure to follow these conventions will result in needless e�ort by the markers, and a deduction of grades for you. Do not submit folders, zip documents, even if you think it will help. • Programs must be written in Python 3. • Assignments must be submitted to Moodle. There is a link on the course webpage that shows you how to do this. • Moodle will not let you submit work after the assignment deadline. It is advisable to hand in each answer that you are happywith as you go. You can always revise and resubmit asmany times as you like before the deadline; only your most recent submission will be graded. • Questions are annotated use descriptors like "easy" "moderate" and "tricky". All students should be able to obtain perfect grades on "easy" problems. Most students should obtain perfect grades on "moderate" problems. The problemsmarked "tricky" may require signi�cantly more time, and only the top students should expect to get perfect grades on these. We use these annotations to help students be aware of the di�erences, and also to help students allocate their time wisely. Partial credit will be given as appropriate, so hand in all attempts. Yousef Abdelhadi Yousef Abdelhadi Department of Computer Science 176 Thorvaldson Building 110 Science Place, Saskatoon, SK, S7N 5C9, Canada Telephine: (306) 966-4886, Facimile: (306) 966-4884 CMPT 141 Winter 2019 Introduction to Computer Science Overview Pokemon are fantastic creatures that people can catch and train. Pokemon are classi�ed into di�erent "types", such as grass, �re or electric. To catch di�erent types of pokemon, you might have to travel all over the world! For this assignment, you will build a program that will help organize information about where to go to catch the di�erent pokemon. This assignment is divided into 5 di�erent "questions", but in the end, your work from all of the questions will be combined to complete the overall program. The �rst four questions will each ask you to de�ne a function that will perform a speci�c task. Make sure that the inputs (parameters) and outputs (return values) of your functions match the problem description exactly, or else the whole thing won’t �t together properly! The following is a brief overview of how the questions for this assignment will �t together: • Question 1: Read data about the pokemon from a text �le and organize it as a database. You must fully complete this question before moving on. All of the other questions will depend on the database that you create here. • Question 2: Build a list of all the unique continents where you can �nd pokemon. • Question 3: Build a list of all the pokemon that can be found on a particular continent. • Question 4: Count how many of each pokemon type occur within a given list of pokemon names. • Question 5: For each continent, display the pokemon type counts. Individually, none of these questions are particularly di�cult, but if youmake amistake anywhere along the way, then the last part won’t work. Make sure to perform simple testing, such as printing out lists or dictionaries, as you go to make sure that everything looks right. One last note of caution: we have given you an input �le for this assignment, but be aware that we might test your programon a di�erent input �le. Therefore, youmustmake sure your code is fully general; don’t count on knowing exactly how many pokemon there are, or on knowing exactly what the names of the pokemon types or locations might be. What to Hand In When you’re done, you’ll hand in one document, a5.py, with ALL your work in it. Make sure you put identi�ca- tion information at the top of your program! Page 2 Yousef Abdelhadi Yousef Abdelhadi Yousef Abdelhadi Department of Computer Science 176 Thorvaldson Building 110 Science Place, Saskatoon, SK, S7N 5C9, Canada Telephine: (306) 966-4886, Facimile: (306) 966-4884 CMPT 141 Winter 2019 Introduction to Computer Science Question 1 (6 points): Purpose: To practice the concept of dictionary as a database Degree of Di�culty: Moderate. Before starting this question, �rst, download the data �le pokemonLocations.txt from the class Moodle. Make sure to put it in the same folder as your a5.py python code. Write a function called read_pokedata() that takes as parameter(s): • a string indicating the name of a pokemon location data �le This function should return a database (i.e. a dictionary-of-dictionaries) that stores all of the Pokemon data in a format that we will describe further below. You can review section 11.1.9 of the text for a refresher on what databases look like using dictionaries. Input File Format The data �le for this question looks like this: bulbasaur,grass,South America ivysaur,grass,Asia,Antarctica Each line of the �le contains all of the data for a single pokemon. The �rst item on the line is always the pokemon’s name; names are guaranteed to be unique. The second item is always the pokemon’s type, which in the example above, is the type grass.1 Following that are one or more continents where that pokemon can be found. So Bulbasaur can only be found in South America, but Ivysaur can be found in both Asia and Antarctica. Note that the continent names CAN contain spaces. All of the data items on a each line are separated by commas. Database Format Your function should open the �le given as a parameter and read the data from it into a database. The keys for this database will be pokemon names, and the values will be records (i.e another dictionary) for the matching pokemon. First, the read_pokedata() function should create an empty dictionary to be the overall database. Then, for each pokemon from the input �le, create one record (i.e. one dictionary) with the following �elds: • The pokemon’s name, as a string. • The pokemon’s type, as a string. • The locations where the pokemon can be found, as a list of strings. This record should then be added to the database, using the pokemon’s name as a key. Once all of the records have been added, the function should return the database dictionary. 1Yes, we are aware that in fact Pokemon can have more than one type. Page 3 Yousef Abdelhadi Yousef Abdelhadi Yousef Abdelhadi Department of Computer Science 176 Thorvaldson Building 110 Science Place, Saskatoon, SK, S7N 5C9, Canada Telephine: (306) 966-4886, Facimile: (306) 966-4884 CMPT 141 Winter 2019 Introduction to Computer Science Evaluation • 1 mark: Function correctly uses �le name parameter to open the data �le • 1 mark: The function returns a database dictionary. • 2 marks: The record for each pokemon is correctly constructed • 1 mark: Each record is correctly added to the database dictionary • 1 mark: For a good function docstring Page 4 Yousef Abdelhadi Yousef Abdelhadi Department of Computer Science 176 Thorvaldson Building 110 Science Place, Saskatoon, SK, S7N 5C9, Canada Telephine: (306) 966-4886, Facimile: (306) 966-4884 CMPT 141 Winter 2019 Introduction to Computer Science Question 2 (4 points): Purpose: To practice iterating over nested compound data Degree of Di�culty: Easy. Write a function called find_continents() which takes as parameter(s): • A dictionary in the format of the pokemon database as described in Q1 This function should construct and return a list of all the di�erent continents where it is possible to �nd pokemon. Each entry in the list should be unique; in other words, don’t add the same continent twice even if you can �nd multiple kinds of pokemon there. For the input �le you were given, the resulting list should be (though not necessarily in this order):� � [’Oceania ’, ’Asia’, ’South America ’, ’North America ’, ’Africa ’, ’Antarctica ’, ’Europe ’]� � Also, note that this functionmust not change the pokemon database in any way. Evaluation • 1 mark: The function parameters and return value are the correct data type • 1 mark: All of the continents are in the returned list • 1 mark: The list entries are all unique • 1 mark: For a good function docstring Page 5 Yousef Abdelhadi Yousef Abdelhadi Department of Computer Science 176 Thorvaldson Building 110 Science Place, Saskatoon, SK, S7N 5C9, Canada Telephine: (306) 966-4886, Facimile: (306) 966-4884 CMPT 141 Winter 2019 Introduction to Computer Science Question 3 (4 points): Purpose: To practice iterating over nested compound data and accessing di�erent dictionary �elds Degree of Di�culty: Easy. Write a function called pokemon_in_continent() which takes as parameter(s): • A dictionary in the format of the pokemon database as described in Q1 • The name of a continent, as a string The function should construct and return a list containing the names of all of the pokemon that can be found on the given continent. For example, for the input �le you were given, if the continent is "Africa", the resulting list should be (though not necessarily in this order):� � [’gyarados ’, ’nidorina ’, ’magmar ’, ’jigglypuff ’, ’machoke ’, ’machamp ’, ’dragonair ’, ’slowbro ’, ’machop ’, ’venomoth ’, ’tentacruel ’, ’parasect ’, ’omanyte ’, ’poliwrath ’, ’muk’, ’hitmonlee ’, ’vaporeon ’, ’tangela ’, ’gengar ’, ’nidoranmale ’, ’venusaur ’, ’onix’, ’nidoking ’, ’golem’, ’aerodactyl ’, ’tauros ’, ’metapod ’, ’electrode ’, ’lapras ’, ’psyduck ’, ’blastoise ’, ’nidoqueen ’, ’alakazam ’, ’cubone ’, ’magikarp ’, ’kabutops ’, ’kakuna ’, ’poliwag ’, ’marowak ’, ’rhydon ’, ’kangaskhan ’, ’zubat’, ’dodrio ’, ’persian ’, ’pikachu ’, ’meowth ’, ’drowzee ’, ’gastly ’]� � Also, note that this functionmust not change the pokemon database in any way. Evaluation • 1 mark: The function parameters and return value are the correct data type • 2 marks: All of the pokemon on the given continent are found and placed in the list • 1 mark: For a good function docstring Page 6 Yousef Abdelhadi Yousef Abdelhadi Department of Computer Science 176 Thorvaldson Building 110 Science Place, Saskatoon, SK, S7N 5C9, Canada Telephine: (306) 966-4886, Facimile: (306) 966-4884 CMPT 141 Winter 2019 Introduction to Computer Science Question 4 (4 points): Purpose: To practice summarizing data from a dictionary Degree of Di�culty: