Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

08 Consider the following Python code. The first half reads a list of words from a file. The second processes that list to produce another

image text in transcribed
image text in transcribed
image text in transcribed
08 Consider the following Python code. The first half reads a list of words from a file. The second processes that list to produce another list of all the unique words. 01 f = open("GreatBook. txt","1") 02 fileContents f.read() 03 wordList fileContents.split() 04 f.close() 05 unique Words = [] 06 for entry in wordlist: 07 word entry.rstrip(".?!") if word not in unique Words: 09 unique Words. insert(0, word) 10 uniqueWords. sort (a) Suppose that running the code produces an error message from line 6: for entry in wordlist: NameError: name 'wordlist' is not defined Is this a syntax, runtime, or logic error? Justify your answer. From now on, suppose that the mistake on line 6 is corrected, and that the first two words in wordList are "Why?" and "What!". (b) At the end of the first iteration of the loop: What is the value assigned to entry? What is the value assigned to word? (c) At the end of the second iteration of the loop what value is assigned to the variable unique Words? (d) Line 9 inserts a word into a list and Line 10 puts the list data into order. What would go wrong if we tried to combine them into the following single line: uniqueWords. insert(0,word). sort() the first has complexity 0(4M) and the second O(N). What are the names of the corresponding complexity classes? Which algorithm will scale better? (6 marks) Wharbi (2 marks) Hide blocks (5 marks) (e) The chaining of list methods suggested in Q3d fails, but similar chaining would work for string methods. Explain the key difference between the data types and their methods that accounts for this. (1) In the functional programming paradigm the list methods discussed above are considered "impure". Describe one different way a function may be impure. (e) Consider the following pseudo-code used by a web retailer to identify suitable products to advertise to a customer: 01 FOR each product on offer DO 02 FOR each identified interest of the customer DO 03 IF product fits interest THEN 04 show advert for product 05 CONTINUE to next product Suppose that there are n interests of the customer and twenty products on offer at any one time. What is the worst case Big-O complexity of the code? To which complexity class does the algorithm belong? (h) The web retailer must decide which of their N products to put on offer in the future. Two coders each propose an algorithm to model future sales

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Accounting questions