Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

As the diameter of each tyre is written on the sidewall (which faces upwards in the stack of tyres), the only way for Jo to

As the diameter of each tyre is written on the sidewall (which faces upwards in the stack of tyres), the only way for Jo to find if a particular diameter tyre is in a given stack of tyres is to take off the tyre above it, and then inspect the resulting new top tyre. This process is repeated until the tyre needed is found in the given stack, a tyre larger than the one being looked for is found, or the stack is empty. Jo has to rebuild the stack afterwards. As Jo's tyre shop is very tightly packed, there is not much room to move tyres around. Jo leaves two empty stack spaces, MOVEA and MOVEB, which can be used to build temporary stacks of tyres while a particular stack is being searched. Note that Jo is not removing the tyre.

Write an operation to implement the definition below to find whether a tyre of tyre diameter is within stack name within tyre shop. You should make use of a recursive technique and may create additional functions or operations if you wish.

Operation: tyre in stack
Inputs/Outputs: tyre shop, a TyreShop
Inputs: tyre diameter, an integer, stack name, a string
Preconditions: stack name exists within tyre shop, MOVEA and MOVEB exist within tyre shop and are empty stacks.
Output: found a Boolean
Postconditions: tyre found is true if tyre diameter is found within stack name, else false; MOVEA and MOVEB are empty; stack stack name has the same contents and order as it did prior to operation call.

You can use the tests below to help check if your code is working.
NOTE that failing the tests is a clear indication your code is faulty, but passing the tests is NOT a guarantee it is correct.
Your tutor may run additional tests.

Note
This is a challenging question designed to encourage you to explore recursive solutions to problems. While you are encouraged to tackle the problem recursively, some marks are available if you complete the question in a non-recursive manner. You might like to research the "Towers of Hanoi" when tackling this question.

 

 

 

 

#answer here

 


#Tests
tyreshop.show()

test_table = [
   ['Exists at top',tyreshop,190,'A',True],
   ['Exists in middle',tyreshop,200,'A',True],
   ['Does not exist',tyreshop,7,'A',False],
]
test(tyre_in_stack, test_table)  #Ensure the function name in this line matches your function.
tyreshop.show()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

To implement the operation tyreinstack recursively we can follow these steps Check if the stack is empty If so return False because the tyre cannot be ... 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_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Elementary Statistics

Authors: Robert R. Johnson, Patricia J. Kuby

11th Edition

978-053873350, 9781133169321, 538733500, 1133169325, 978-0538733502

More Books

Students explore these related Algorithms questions