Some simple python programming questions.

Anonymous
timer Asked: Mar 29th, 2019
account_balance_wallet $35

Question Description

I've attached the documents containing the sample code and the questions to go along with it. If you need anything else to answer the question then I'm sure I can provide it! Thanks :)

Unformatted Attachment Preview

TMA02 – Python Question 1 (30 marks) The TMA questions and guidance page has all the files mentioned in this question. Imagine you are part of a team developing a crossword app that helps the user, e.g. by listing all 3-letter English words ending in ‘t’. For such a feature, it is better to store dictionary words sorted by length instead of alphabetically. The team lead asks you to write a function that takes a non-empty list of non-empty strings (e.g. ['hi', 'this', 'is', 'Jane']) and returns a new list with the same strings in increasing length. Words of the same length must appear in the original order (that’s called a stable sorting). For this example, the result is ['hi', 'is', 'this', 'Jane']. You are asked to use this bucket sort algorithm: 1. Compute the maximum length L of the words in the list. For the example, L is 4. 2. Create a list with L buckets, each bucket being initially the empty list. For the example, this step creates [[], [], [], []]. 3. Put each word in the right bucket: words of length 1 (like ‘a’ and ‘I’) go in the first bucket, words of length 2 go in the second bucket, etc. For the example, the result of this step is [[], ['hi', 'is'], [], ['this', 'Jane']]. The first and third buckets remain empty. 4. Compute the sorted list by first adding the words in bucket 1, then those in bucket 2, etc. For the example, the result of this step is ['hi', 'is', 'this', 'Jane']. • a.This part is about Python’s list operations (Python activity 1.5). Step 2 has been implemented for you in file TMA02_Q1.py. Implement the rest of the algorithm by replacing the pass statements by your code (about 12 lines). You can only use the Python list operations given in the Companion’s appendix. Copy the completed bucket_sorted function into your Solution Document. We advise you add one step at a time and test it with file TMA02_Q1_tests.py. You should add your own tests. Your tutor will use a different test file, so make sure your code solves the general problem, not just the problem instances (tests) we provide. (12 marks) • b.Explain how your implementation guarantees the sort to be stable, i.e. keeps the original order of words of the same length. (2 marks) • c.This part is about algorithmic complexity (Unit 2 Section 4.1 and theCompanion). State and explain the worst-case Big-O complexity of your bucket sort implementation in terms of the number of words n, i.e. the length of the input list. The unit of computation is the assignment. Use the complexities in the Companion’s appendix. Explain also why the problem size is just the number of words and not dependent on the number of buckets L too. (5 marks) • d.This part is about heap sort (Unit 3 Section 4.2) and the professional skill of comparing alternative approaches. A team member suggests it would be easier to just take heap sort and in the percolation process replace every comparison of strings in the heap by the comparison of their lengths, i.e. x < y becomes len(x) < len(y). What is the worst-case complexity of the modified algorithm? Explain whether you would follow the team member’s suggestion. (3 marks) • e.This part is about recursion (Unit 3 Section 3.2 and Companion section ‘Recursion’). Rewrite function empty_buckets in file TMA02_Q1.py so that it is recursive. Add comments to indicate each of the base case and the reduction, recursive and inductive steps, as done in the Companion. Copy your completed function into your Solution Document. Use again file TMA02_Q1_tests.py to check your code. Don’t forget to add your own tests. (8 marks) Question 2 (20 marks) The TMA questions and guidance page has all the files mentioned in this question. A bag is an unordered collection that may contain duplicate items. File TMA02_Q2a.pyimplements a Bag ADT with the following operations: • Bag() creates a new empty bag • add(self, item) adds one copy of item to the bag self • count(self, item) returns how often item occurs in bag self • size(self) counts the total number of copies in bag self • clear(self, item) removes all copies of item from bag self • ordered(self) returns the content of bag self as a sequence of pairs (count, item), in decreasing order of count This part is about Python’s list operations (Python activity 1.5 and Companion) and relates to the professional skill of reengineering code to improve its performance. • a.This part is about Python’s list operations (Python activity 1.5 and Companion) and relates to the professional skill of reengineering code to improve its performance. File TMA02_Q2a.py represents a bag as an unsorted list, with as many copies of each item as there are in the bag. Completely rewrite the clear method, so that it has O(n)worst-case complexity. Only use the list operations in the Companion’s appendix. Check your code with TMA02_Q2_tests.py. We recommend you add your own tests. Your tutor will use a different test file. Copy the rewritten method to your Solution Document. Briefly justify why it has worst-case O(n) complexity. (6 marks) • b.This part is about Python dictionaries (Companion section ‘Hash tables’). Reimplement the Bag ADT using Python dictionaries, by completing file TMA02_Q2b.py. You can only use the operations listed in the Companion’s appendix. Change the import statement in TMA02_Q2_tests.py to test your code. Copy the methods into your Solution Document. (10 marks) • c.This part relates to the professional skill of choosing appropriate data structures. File TMA02_Q2c.py creates a bag with the words in Shakespeare’s Hamlet and prints the 50 most frequent words and their count. (Doing this for several texts can indicate the most common English words, e.g. to teach beginners.) Running the code, you will note that the most common words are mainly stop words(articles, prepositions, etc.) and the play’s characters (Hamlet, Horatio, etc.). After reading the code, describe briefly how you would change it, and what data structure you would use, to prevent stop words and character names from being added to the bag. Justify your approach. Assume there are 18 character names in the play (lines 70-77 of hamlet.txt) and about 120 stop words (taken from this list). You can use any data structure from Units 1–5 of M269 and its operations. Assume the complexities listed in the Companion’s appendix. Note you are only asked to describe your changes, not to write or submit any code. (4 marks) TMA02 – Python – Resource Tma02_q1.py """Bucket sort. For M269 18J TMA02 Question 1. Student version 1: 5/8/18 """ def empty_buckets(n): """Return a list with n empty lists. Assume n is a positive integer. """ buckets = [] for bucket in range(n): buckets.append([]) return buckets def bucket_sorted(words): """Return a new list with the same words, but by increasing length. Assume words is a non-empty list of non-empty strings. """ # Compute the maximum length L of all words. pass # Create a list of L empty lists (buckets). buckets = empty_buckets(L) # Put each word in the bucket corresponding to its length. pass # Put all buckets together into a single list of words. pass Tma02_q1_tests.py """Tests for bucket sort. For M269 18J TMA02 Question 1. Student version 1: 5/8/18 """ from TMA02_Q1 import empty_buckets, bucket_sorted failed = 0 ran = 0 def test(name, actual, expected): """Report if test passed or failed.""" global ran, failed if actual == expected: print(name, 'OK') else: print(name, 'FAILED: got', actual, 'instead of', expected) failed += 1 ran += 1 # Tests test('3 empty buckets', empty_buckets(3), [[], [], []]) print() before = ['hi', 'this', 'is', 'Jane'] after = ['hi', 'is', 'this', 'Jane'] test('some buckets empty', bucket_sorted(before), after) words = ['if', 'to'] test('single bucket', bucket_sorted(words), words) # Report print() print(ran, "tests:", ran-failed, "OK", failed, "FAILED") if failed == 0: print("Well done, you passed our tests!") print("Now add your own. Think of boundary values.") print() TMA02_Q2_a.py """An implementation of the Bag ADT, using unsorted lists. For M269 18J TMA02 Question 2. Student version 1: 6/8/18 """ class Bag: def __init__(self): """Create a new empty bag.""" self.items = [] def add(self, item): """Add one copy of item to the bag. Multiple copies are allowed.""" self.items.append(item) def count(self, item): """Return the number of copies of item in the bag. Return zero if the item doesn't occur in the bag. """ counter = 0 for an_item in self.items: if an_item == item: counter += 1 return counter def clear(self, item): """Remove all copies of item from the bag. Do nothing if the item doesn't occur in the bag. """ index = 0 while index < len(self.items): if self.items[index] == item: self.items.pop(index) else: index += 1 def size(self): """Return the total number of copies of all items in the bag.""" return len(self.items) def ordered(self): """Return the items by decreasing number of copies. Return a list of (count, item) pairs. """ result = set() for item in self.items: result.add((self.count(item), item)) return sorted(result, reverse=True) TMA02_q2b.py """An implementation of the Bag ADT, using a dictionary. For M269 18J TMA02 Question 2. Student version 1: 7/8/18 """ class Bag: def __init__(self): """Create a new empty bag.""" def add(self, item): """Add one copy of item to the bag. Multiple copies are allowed.""" def count(self, item): """Return the number of copies of item in the bag. Return zero if the item doesn't occur in the bag. """ def clear(self, item): """Remove all copies of item from the bag. Do nothing if the item doesn't occur in the bag. """ def size(self): """Return the total number of copies of all items in the bag.""" def ordered(self): """Return the items by decreasing number of copies. Return a list of (count, item) pairs. """ TMA02_q2c.py """Finding common English words. For M269 18J TMA02 Question 2. Student version 1: 30/8/18 """ # If you have done Question 2b, you may replace 2a by 2b # to test your dictionary implementation returns the same most frequent words from TMA02_Q2a import Bag def valid(word): """Return True if word should be added to the bag, otherwise False. word is a string """ return word != '' def bag_of_words(filename): """Return the words occurring in filename as a bag-of-words. filename is a string with the name of a text file """ words = Bag() # open the file in read-only mode with open(filename, 'r') as file: # go through the file line by line for line in file: # transform punctuation into space line = line.replace('(', ' ') line = line.replace('[', ' ') line = line.replace('{', ' ') line = line.replace(')', ' ') line = line.replace(']', ' ') line = line.replace('}', ' ') line = line.replace('.', ' ') line = line.replace(',', ' ') line = line.replace(';', ' ') line = line.replace(':', ' ') line = line.replace('_', ' ') # use space to separate the words in a line for word in line.split(): # remove quote marks and other characters word = word.strip("'\"!?+-*/#") # put in lowercase word = word.lower() if valid(word): words.add(word) return words print("Collecting words in Shakespeare's Hamlet...") all_words = bag_of_words('hamlet.txt') print("Done") print('Sorting the words by decreasing frequency...') frequency = all_words.ordered() print("Done") top = 50 print("The", top, "most frequent words are:") for (count, word) in frequency[:top]: print(count, word) TMA02_Q2_Tests """Tests for the Bag ADT For M269 18J TMA02 Question 2. Student version 1: 30/8/18 """ # change Q2a to Q2b in the next line to test your dictionary implementation from TMA02_Q2a import Bag failed = 0 ran = 0 def test(name, actual, expected): """Report if test passed or failed.""" global ran, failed if actual == expected: print(name, 'OK') else: print(name, 'FAILED: got', actual, 'instead of', expected) failed += 1 ran += 1 words = Bag() words.add('once') words.add('twice') words.add('twice') test('size', words.size(), 3) test('non-existing item', words.count('none'), 0) test('multiple copies', words.count('twice'), 2) test('ordered', words.ordered(), [(2, 'twice'), (1, 'once')]) words.clear('what?') test('clear non-existing', words.size(), 3) words.clear('once') test('clear item', words.size(), 2) test('once gone', words.count('once'), 0) test('twice remains', words.count('twice'), 2) test('ordered after clearing', words.ordered(), [(2, 'twice')]) print() print('Ran', ran, 'tests:', ran - failed, 'OK,', failed, 'FAILED') if failed == 0: print('You passed all our tests. Well done!') print('Now add YOUR tests. Think of boundary values.') ...
Purchase answer to see full attachment

Tutor Answer

mutindadennis35
School: UIUC

Hey buddy 😃have finished check the attached fi...

flag Report DMCA
Review

Anonymous
Excellent job

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors