Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Can you please help me with Python Code? Thanks a lot. 1. Dessert Shop Part 9: Combine Like Items You may have noticed in the
Can you please help me with Python Code? Thanks a lot.
1. Dessert Shop Part 9: Combine Like Items You may have noticed in the previous few receipts that some items are duplicated. We want to combine items that seem to be the same into one line item on the receipt. For example, suppose the order contains two (2) items of Gummy Bears that both cost $0.25/lb, but one is for 0.5lbs and the other is for 1.25lbs-it makes sense to combine those into a single item of: Gummy Bears: 1.75 lbs e $0.25/1b This would only make sense if both the item name and price per pound were the same. A store may sell one kind of Gummy Bear (blue) at $0.35/lb and another kind of Gummy Bear (green) at $0.25/lb. These would be different items and should not be combined. Only identical Cookie items or identical Candy items can be combined. Trying to package two Ice Cream Sundaes into a single plastic boat wouldn't work, for example. Continuing our exploration of what typing information can do for us: Python is a strongly-typed and dynamically-typed. This means that type errors are caught, but they are checked at runtime. For large complex code bases, type hints can help programmers communicate the types of variables that are being passed around a system. Just like Classes and Objects help manage complexity, type hints help manage complexity. To achieve this, we will define a runtime-checkable mixin Protocol class (interface) called which combinable classes of desserts will implement. Each such class will define what it means to check if they can be combined and how two items of its type are to be combined. It is tempting to implement this by redefining on combinable Dessertltem objects, but this is not a good design idea: 1. We already defined all relational operators in Part 8 to do something else, including 2. The idea of combining to "same" or "similar" items, there's no concept of ordering, less than, greater than, or sorting, so it would not make sense to try to define that concept that way. Combinable Protocol You will define this protocol class in the file - Define the protocol class Combinable - Mark the protocol as run-time checkable with decorator. - Define predicate method can_combine(self, other: "Combinable")->bool. It has no body. - Define method combine(self, other: "Combinable") "Combinable" . It has no body. - Mark both methods as abstract. A precondition of calling combine successfully is that would return for the two items. The Combinable Protocol would be called a pure Interface in other languages. It specifies methods, but no default implementations. In some places you see the type "Combinable" in quotes. This is necessary when you need to use the type being defined within its definition. Note: Using Protocol classes assumes we are using Python 3.8+. As of December 2022, Codio is still using Python 3.6.8 by default. If that is still the case in Spring 2023,then define the class as a pure abstract class and use it as a mix-in with multiple inheritance. Implement the Combinable protocol. Do not use direct inheritance. - can_combine(self, other: "Candy") -> return True only if other is an instance of Candy other has the same name and same price per pound otherwise False - combine(self, other: "Candy") -> "Candy" -> add the weight of other to the weight of self destructively Changes to Cookie class Implement the Combinable protocol. Do not use direct inheritance. - can_combine(self, other: "Cookie") -> "Cookie" -> return True only if other is an instance of Cookie other has the same name and same price per dozen otherwise False - combine(self, other: "Cookie") -> "Cookie" -> add the quantity of other to the quantity of self destructively Python offers several ways to search a collection or list of items and potentially combine them, from very basic to using what most people consider intermediate or advanced techniques. Do what makes sense to you, as long as it is your own code. One way is to add the following mutually-exclusive checks to the code that adds an item to the order: 1. if the new item is not Combinable or can_combine() returns False for all items in the order: i. add the new item to the order 2. if new item is Combinable: i. find the first item for which can_combine() returns True ii. combine the new item with the existing item A 2 nd way is to combine like items after the order is created but before it is printed, using the same constraints listed above. This usually works best if you produce a copy of the order with items combined, rather than destructively modifying order items. lest cases Add some new tests to test Combinable functionality. New Candy Tests Add one test for can_combine() that should correctly return True if both are candies. Add one test for can_combine() that should correctly return False if the other item is not a Candy. Add one test for combine() that should correctly combine two Candy items. Add one test for combine() that should correctly not combine two items where one is a Candy and one is not. We expect this to fail. New Cookie Tests Add one test for can_combine() that should correctly return True if both are cookies. Add one test for can_combine() that should correctly return False if the other item is not a Cookie. Add one test for combine() that should correctly combine two Cookie items. Add one test for combine() that should correctly not combine two items where one is a Cookie and one is not. We expect this to fail
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started