Suppose there is a circle. There are petrol pumps on that circle. Petrol pumps are numbered to (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump.Initially, you have a tank of infinite capacity carrying no petrol. You can start the tour at any of the petrol pumps. Calculate the first point from where the truck will be able to complete the circle. Consider that the truck will stop at each of the petrol pumps. The truck will move one kilometer for each litre of the petrol.Input FormatThe first line will contain the value of .The next lines will contain a pair of integers each, i.e. the amount of petrol that petrol pump will give and the distance between that petrol pump and the next petrol pump.Constraints:Output FormatAn integer which will be the smallest index of the petrol pump from which we can start the tour.Sample Input31 510 33 4Sample Output1ExplanationWe can start the tour from the second petrol pump.Contest ends in 2 hoursSubmissions: 4Max Score: 10Difficulty: HardRate This Challenge: More Python 31#!/bin/python323import math4import os5import random6import re7import sys89#10# Complete the 'truckTour' function below.11#12# The function is expected to return an INTEGER.13# The function accepts 2D_INTEGER_ARRAY petrolpumps as parameter.14#1516def truckTour(petrolpumps):17 # Write your code here1819if __name__ == '__main__':20 fptr = open(os.environ['OUTPUT_PATH'], 'w')2122 n = int(input().strip())2324 petrolpumps = []2526 for _ in range(n):27 petrolpumps.append(list(map(int, input().rstrip().split())))2829 result = truckTour(petrolpumps)3031 fptr.write(str(result) + '\n')3233 fptr.close()34
Question
Suppose there is a circle. There are petrol pumps on that circle. Petrol pumps are numbered to (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump.Initially, you have a tank of infinite capacity carrying no petrol. You can start the tour at any of the petrol pumps. Calculate the first point from where the truck will be able to complete the circle. Consider that the truck will stop at each of the petrol pumps. The truck will move one kilometer for each litre of the petrol.Input FormatThe first line will contain the value of .The next lines will contain a pair of integers each, i.e. the amount of petrol that petrol pump will give and the distance between that petrol pump and the next petrol pump.Constraints:Output FormatAn integer which will be the smallest index of the petrol pump from which we can start the tour.Sample Input31 510 33 4Sample Output1ExplanationWe can start the tour from the second petrol pump.Contest ends in 2 hoursSubmissions: 4Max Score: 10Difficulty: HardRate This Challenge: More Python 31#!/bin/python323import math4import os5import random6import re7import sys89#10# Complete the 'truckTour' function below.11#12# The function is expected to return an INTEGER.13# The function accepts 2D_INTEGER_ARRAY petrolpumps as parameter.14#1516def truckTour(petrolpumps):17 # Write your code here1819if name == 'main':20 fptr = open(os.environ['OUTPUT_PATH'], 'w')2122 n = int(input().strip())2324 petrolpumps = []2526 for _ in range(n):27 petrolpumps.append(list(map(int, input().rstrip().split())))2829 result = truckTour(petrolpumps)3031 fptr.write(str(result) + '\n')3233 fptr.close()34
Solution
The problem you're trying to solve is about finding the starting point of a circular tour that can be completed with the given conditions. Here's a step-by-step solution:
- Initialize total_petrol and total_distance to 0 and start to 0.
- Create a loop from 0 to n (the number of petrol pumps). For each petrol pump, add the amount of petrol to total_petrol and the distance to the next pump to total_distance.
- If total_petrol < total_distance, this means we can't reach the next pump from the current starting point, so we need to change the starting point. Set start to i+1 (the next pump) and reset total_petrol and total_distance to 0 because we're starting anew.
- After the loop, if we've gone through all the pumps and total_petrol >= total_distance, this means we can complete the tour, so return start. This is the first pump we can start the tour from.
Here's the Python code for the truckTour function:
def truckTour(petrolpumps):
total_petrol = 0
total_distance = 0
start = 0
for i in range(len(petrolpumps)):
total_petrol += petrolpumps[i][0]
total_distance += petrolpumps[i][1]
if total_petrol < total_distance:
total_petrol = 0
total_distance = 0
start = i+1
return start
This function takes a 2D array of petrol pumps, where each pump is represented by a pair of integers: the amount of petrol it gives and the distance to the next pump. It returns the smallest index of the petrol pump from which we can start the tour.
Similar Questions
The x-t table for a car running a race on a horizontal straight road is given. There are checkpoints at x = 1 km and x = 3 km. Each time the car reaches the checkpoints, it stops for refuelling. Assuming the car runs straight to the given positions without any extra turning or motion, calculate the number of times the car is refilled.
Eight trucks, A, B, C, D, E, F, G and H, are parked in a circle, with all of them facing the centre. Truck E is second to the left of Truck A and is next to Truck C and Truck G. Truck D is not next to Truck A. Truck G is next to Truck F. Truck B is between Truck D and Truck H.
Two cars C1 and C2 start their journey from two different points at 4 kmph and 3 kmph respectively and meet after 5 hours at a point where their paths make an angle of 135°. After the cars met, they interchange their paths. Each car first travels to the other car's starting point (i.e. C1 travels to C2's starting point along the path C2 was travelling on and C2 travels to C1's starting point along the path C1 was travelling on ) and then travels along the straight line path (i.e. the shortest distance) joining the starting points of the two cars, finally reaching it's original starting point. Find the approximate time interval between the return of C1 and C2 to their starting positions.5-6 hours11-12 hours3-4 hours8-9 hours
Answer each of the following questions independently.Two cars C1 and C2 start their journey from two different points at 4 kmph and 3 kmph respectively and meet after 5 hours at a point where their paths make an angle of 135°. After the cars met, they interchange their paths. Each car first travels to the other car's starting point (i.e. C1 travels to C2's starting point along the path C2 was travelling on and C2 travels to C1's starting point along the path C1 was travelling on ) and then travels along the straight line path (i.e. the shortest distance) joining the starting points of the two cars, finally reaching it's original starting point. Find the approximate time interval between the return of C1 and C2 to their starting positions.
Answer each of the following questions independently.Two cars C1 and C2 start their journey from two different points at 4 kmph and 3 kmph respectively and meet after 5 hours at a point where their paths make an angle of 135°. After the cars met, they interchange their paths. Each car first travels to the other car's starting point (i.e. C1 travels to C2's starting point along the path C2 was travelling on and C2 travels to C1's starting point along the path C1 was travelling on ) and then travels along the straight line path (i.e. the shortest distance) joining the starting points of the two cars, finally reaching it's original starting point. Find the approximate time interval between the return of C1 and C2 to their starting positions.5-6 hours11-12 hours8-9 hours3-4 hours
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.