Question: 5. [10] In this question, you will write a program to solve the general form of a well-known brain-teaser You are given two initially empty

 5. [10] In this question, you will write a program to

5. [10] In this question, you will write a program to solve the general form of a well-known brain-teaser You are given two initially empty buckets A and B, with capacities of m litres and n litres, respectively. Your goal is to measure exactly k litres of water using these two buckets. Assume that m, n and k are positive integers and that k S max(m, n). You want to achieve this goal by performing a sequence of moves until one of the buckets has exactly k litres of water. Each move can be one of the following Fill a bucket until it's full Empty a bucket Use the water in one bucket to fill the other until one of the buckets is full or empty Your job is to design and implement a function bucket flip (m, n, k), which takes m, n and k as inputs and returns the minimum number of moves needed to solve this puzzle. If it is impossible to measure k litres of water using the two buckets, the function returns -1 The header of your function is as follows 1 def bucket flip(m, n, k): 2 3 Returns the minimum number of moves needed to measure k liters of water using two buckets of sizes m and n 6 Precondition: m, n, k are positive integers and k >bucket flip(3, 7, 5) 8 10 Requirements: . Your code must be written in Python 3, and filename must be bucket.py as required by MarkUs It is OK to have import statements in your file Submission: Your submission will include the following parts . The Python file bucket.py which implements the bucket flip) function. The TA will test your code using the following manner import bucket # importing your bucket.py assertEqual(8, bucket.bucket flip(3, 7, 5)) In your PDF file, provide a brief high-level description of your algorithm. A short paragraph should suffice In your PDF file, express the worst-case runtime of your algorithm in terms of m and n Note: To make sure your program is correct, you need to create your own test cases for verification. For debugging purposes, it will be useful to print out the sequence of moves that are taken in the optimal solution, which you should be able to do conveniently

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 Databases Questions!