COMP 1046 OOP Prac 12 Data Structures PDF

Title COMP 1046 OOP Prac 12 Data Structures
Author John Smith
Course Object-Oriented Programming
Institution University of South Australia
Pages 3
File Size 173.4 KB
File Type PDF
Total Downloads 19
Total Views 145

Summary

Download COMP 1046 OOP Prac 12 Data Structures PDF


Description

UniSA STEM

COMP 1046 Object-Oriented Programming Practical 8 (Week 12) Data Structures

Using the computers on campus Internal students are required to attend weekly practical sessions in order to undertake their practical work. Practical sessions are held in computer pools. You will be required to login to a computer in order to use it. Login to the computer using your username and password. Your username and password are your electronic signature-you can use them to access the University's online resources and services. a) Type in your user name in lowercase (this is your UNINET username – e.g. bondj007). b) You will then be asked to enter your UNINET password. The password allocated to you at the beginning of your program is the first four letters of your family name (add x if it's less than four letters) and the day and month of your date of birth. For example, if your surname is Ng and your date of birth is 17 March, your password will be ngxx1703. To protect the privacy of your information it is important that you change your password from the default one described above. Your new password must be 6 to 8 character long and cannot start with a number. To help you in case you forget your password setup a password reset account. If you forget your password try using the password reset facility. If you have not setup your your password reset facility contact the IT Help Desk (Monday to Friday, 8.30am to 9.00pm) by phoning 25000 (internal phones) or +61 8 8302 5000 (externally). Country or interstate callers can phone 1300 558 654 for the cost of a local call. You can get further information or log a service request on line at: www.unisa.edu.au/ITHelpDesk External Students Please ensure that Python, Visual Studio Code (VSC) and the Python Extension for VSC are installed on your home computer so that you are ready for this week’s practical. See the 'Resources' page on the course website for information on installing Python. You must perform this practical individually. Copy all Python code and/or answers in plain English for each Task below into a Word document and submit it electronically via the Website until end of Sunday. Marking criteria for this workshop: • There are three tasks below where the last task is option with bonus marks. • You can use any resource that you like. • You can achieve a maximum of 10 marks (15 including bonus question). The breakdown of marks is described below for each task.

Task 1: Mapping Keys to Multiple Values in a Dictionary (5 marks) Create a dictionary using the defaultdict class that maps keys to more than one value (also called “multidict”). A dictionary is a mapping where each key is mapped to a single value. If you want to map keys to multiple values, you need to store the multiple values in another container such as a list or set. For example, you can specify two dictionaries like this: d={ 'a' : [1, 2, 3], 'b' : [4, 5] } e={ 'a' : {1, 2, 3}, 'b' : {4, 5} } The choice of whether using a list or a set depends on what is needed. Use a list if you want to preserve the insertion order of the items. Use a set if you want to eliminate duplicates (and don’t care about the order). Construct the dictionaries d and e above by using the defaultdict class and adding the values one at a time, using the appropriate method to add a new item to the list or set (append() for list and add() for set). Documentation for defaultdict can be found here: https://docs.python.org/2/library/collections.html#collections.defaultdict A feature of defaultdict is that it automatically initializes the first value so you can simply focus on adding items. The class is part of the collections package and needs to be imported in the first line: from collections import defaultdict You can test the output using the following commands: print(“d=” + str(d.items())) print(“e=” + str(e.items())) It should show the content of both dictionary similar to the above output.

Task 2: Determining the Most Frequently Occurring Items in a Sequence (5 marks) You have a sequence of items, and you’d like to determine the most frequently occurring items in the sequence: words = [ 'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes', 'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the', 'eyes', "don't", 'look', 'around', 'the', 'eyes', 'look', 'into', 'my', 'eyes', "you're", 'under' ] The Counter class in the collections package is designed for just such a problem. It provides a useful most_common() method that will give you the answer. Have a look at the documentation here: https://docs.python.org/2/library/collections.html#collections.Counter Find the top 3 most frequent words in the sequence. Print them out on the screen. Your output should contain the words and their frequency.

OPTIONAL Task 3: Write a customised sort algorithm (bonus 5 marks) Given is the class NumOrStr that holds either a number or a character in its attribute value. class NumOrStr: def __init__(self, value): self.value = value def __lt__(self, object): # IMPLEMENT THE SORTING ALGORITHM HERE def __repr__(self): return f"{self.value}" Also given is a list of NumOrStr objects. n1 = NumOrStr(67) n2 = NumOrStr("k") n3 = NumOrStr(17) n4 = NumOrStr("z") n5 = NumOrStr("t") n6 = NumOrStr(3) n7 = NumOrStr("m") n8 = NumOrStr(14) n9 = NumOrStr(78) l = [n1,n2,n3,n4,n5,n6,n7,n8,n9] l.sort() print(l) produces the output: [67, k, 17, z, t, 3, m, 14, 78] Implement a sort algorithm in __lt__() that sorts first numbers ascending and then characters descending. The output of the list should be: [3, 14, 17, 67, 78, z, t, m, k]

Congratulations! You have created your first object-oriented data structures in Python - well done! Show your results to your supervisor in the class or for external students, submit it electronically  Please make sure you save and keep all of your practical and assignment work. You will need to save your work on a USB or the like. Please ask your supervisor if you are having difficulties doing so.

End of Practical 8 (Week 12)...


Similar Free PDFs