Question: How to create the function is_burger(breads)that determines whether a stack of bread is a burger. It is only allowed to use stack operations, i.e: append(
How to create the function is_burger(breads)that determines whether a stack of bread is a burger. It is only allowed to use stack operations, i.e: append( ) at end, pop( ) from end, and the size: len( ). Elements in the list except for the last element cannot be extracted or accessed to.
Input : a non-empty list of strings breadsthat represents the bread stack you are checking; the possible strings
are: 'ob', 'cb', 'ow', 'cw', 'or', 'cr'.
Output : a boolean, Trueif the bread stack is a burger; otherwise False . A bread stack is a burger if each
open piece of bread is followed by a matching closed piece of bread, and there is either nothing between
the opening and closing bread, or there is one or more burgers between the opening and closing bread.
Note: For a bread stack to be a burger it must follow two rules: first, any open piece of bread must be followed
by a matching closing piece of bread; second, any open-close pair may contain a stack of bread between them,
provided the stack is also a burger. There are three different kinds of bread: brioche (open brioche is 'ob' ,
closed is 'cb' ), wholemeal ('ow' , 'cw' ), and rye ('or' , 'cr' ).
Examples:
- Calling is_burger(['ob']) returns False, because the opening brioche does not have a closing brioche.
- Calling is_burger(['ob','or','cr','cb'])returns True, because opening and closing bread contains one burger between them.
- Calling is_burger(['ob','or','cr','ob','cb','cb'])returns True, because opening and closing bread contains multiple burgers between them.
- Calling is_burger(['ob','or','cb','cr'])returns False , because the brioche bun does not contain valid burgers.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
