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:

  1. Calling is_burger(['ob']) returns False, because the opening brioche does not have a closing brioche.
  2. Calling is_burger(['ob','or','cr','cb'])returns True, because opening and closing bread contains one burger between them.
  3. Calling is_burger(['ob','or','cr','ob','cb','cb'])returns True, because opening and closing bread contains multiple burgers between them.
  4. 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!