Python Fill the Bucket program Codechef solution
Python Fill the Bucket program Codechef solution
Problem
The chef has a bucket with a capacity of 100 litres. It is already filled with X litres of water.
Find the maximum amount of extra water in litres that Chef can fill in the bucket without overflowing.
Input Format
- The first line will contain T, the number of test cases. Then the test cases follow.
- The first and only line of each test case contains two space-separated integers, K and X, as mentioned in the problem.
Output Format
For each test case, output in a single line the amount of extra water in litres that Chef can fill in the bucket without overflowing.
Constraints
- 1≤T≤100
- 1≤X<K≤1000
Sample Input:
2
5 4
15 6
Sample Output:
1
9
Explanation:
Test Case 1: The capacity of the bucket is 5 litres, but it is already filled with 4 litres of water. Adding 1 more litre of water to the bucket fills it to (4+1) = 5 litres. If we try to fill it with more water, it will overflow.
Test Case 2: The capacity of the bucket is 15 litres, but it is already filled with 6 litres of water. Adding 9 more litres of water to the bucket fills it to (6+9) = 15 litres. If we try to fill it with more water, it will overflow.
Solution:
T = int(input(“Enter the number of terms: “)) for i in range(T): k,x = map(int,input(“Enter total water holding capacity of a bucket and the amount of water already filled: “).split()) if k >= x: print(k-x) |
Steps to solve this problem:
- Ask the user to enter the number of terms and store it in variable T.
- In the loop, ask the user to enter the total water holding capacity of a bucket and the amount of water already in the bucket. Now, using the map() function, get iterator objects and store them in k and x, respectively.
- Now check if k is greater than or equal to x, then calculate (k-x) and print the result.