Question: PYTHON. I WILL RATE! (e) We can combine assignment statements, for-loops, and if statements to perform a wide range of tasks with lists. Suppose we
PYTHON. I WILL RATE!
(e) We can combine assignment statements, for-loops, and if statements to perform a wide range of tasks with lists. Suppose we have a bookstore with each book defined as follows: Book = namedtuple('Book', 'author title genre year price instock'), where "genre" is the category of book (e.g., cookbook or mystery or sports), "year" is the year of publication, and "instock" is the number of copies of that book we have available to sell. Make up a half-dozen Book structures and combine them into a list called book_store_inventory.
(e.1) Write a sequence of statements that prints the title of each book in book_store_inventory, one per line.
(e.2) Write a sequence of statements that prints the title of each book in book_store_inventory, one per line, in alphabetical order; do this without changing the original order of book_store_inventory.
(e.3) Write a sequence of statements that raises the price of each book in book_store_inventory by 10%; this does change the value of book_store_inventory.
(e.4) Write a sequence of statements that prints the title of each book in book_store_inventory whose genre is Technology.
(e.5) If we ask how many books there are in our list book_store_inventory, there are two possible answers: One is just the number of items in the list, each representing a different author/title combination (book publishers call this the number of "titles"); the other is the number of individual, physical books in the store's inventory (i.e., the sum of all the instock figures of all the "titles" in the list).
Write a sequence of statements that creates a new list containing the Books ("titles") in book_store_inventory that were published before 2000 and a second new list of Books published in 2000 or later. Then write a sequence of statements that prints one of these phrases: More titles before 2000 or More titles 2000 or later. Finally, add to whichever message you print the number of titles in each category, in a form like this: More titles 2000 or later (345 vs. 189). [Hint: To create a new list in this way, start with an empty list. Then, each time you find a new Book that belongs on the list, add that Book to the list. When you've gone through the original list, your new list will have the Books you want. Second hint: There's a simple predefined function that will tell you the number of Books on a (newly created) list. Third hint: Remember that we're writing general code, code that will work for a list of 10 books or of 10,000. When instructions say, to print something "in the form (345 vs. 189)", don't think, "That can't make sense; we've only created six books." Think, "Oh, right, this code might have to deal with more than 500 books."]
(e.6) The value of the inventory of a particular book is the price of that book times the number of copies we have in stock. Write a function called inventory_value that takes a Book as its argument and returns the value of our inventory of that book. Then write a function called top_value that takes a list of Books as its argument and returns the Book object (from the list) that has the highest-value inventory. Finally, write a sequence of statements that prints a line in this form: The highest-inventory-value book is War and Peace by Tolstoy, Leo at a value of $ 595.00.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
