Car Fueling Algorithm in Python The Next CEO of Stack OverflowCalling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonWhat is the best algorithm for an overridden System.Object.GetHashCode?How can I safely create a nested directory in Python?How to get the current time in PythonHow can I make a time delay in Python?Does Python have a string 'contains' substring method?What is the optimal algorithm for the game 2048?

How to compactly explain secondary and tertiary characters without resorting to stereotypes?

Ising model simulation

How to show a landlord what we have in savings?

Traveling with my 5 year old daughter (as the father) without the mother from Germany to Mexico

Does Germany produce more waste than the US?

Oldie but Goldie

Find the majority element, which appears more than half the time

Compensation for working overtime on Saturdays

Another proof that dividing by 0 does not exist -- is it right?

Can this transistor (2N2222) take 6 V on emitter-base? Am I reading the datasheet incorrectly?

Is it "common practice in Fourier transform spectroscopy to multiply the measured interferogram by an apodizing function"? If so, why?

How do I secure a TV wall mount?

Does int main() need a declaration on C++?

What does this strange code stamp on my passport mean?

How does a dynamic QR code work?

How should I connect my cat5 cable to connectors having an orange-green line?

Direct Implications Between USA and UK in Event of No-Deal Brexit

My boss doesn't want me to have a side project

Could a dragon use hot air to help it take off?

How dangerous is XSS

Finitely generated matrix groups whose eigenvalues are all algebraic

How to implement Comparable so it is consistent with identity-equality

How can I replace x-axis labels with pre-determined symbols?

Would a grinding machine be a simple and workable propulsion system for an interplanetary spacecraft?



Car Fueling Algorithm in Python



The Next CEO of Stack OverflowCalling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonWhat is the best algorithm for an overridden System.Object.GetHashCode?How can I safely create a nested directory in Python?How to get the current time in PythonHow can I make a time delay in Python?Does Python have a string 'contains' substring method?What is the optimal algorithm for the game 2048?










-1















The problem is the following :



  • You are going to travel to another city that is located d miles away
    from your home city.

  • You can travel at most m miles on a full tank and you start with a
    full tank.

  • Along your way, there are gas stations at distances stop1,
    stop2,..., stopn from your home city.

What is the minimum number of refills needed?



So far I have come up with the following:



def min_refills(distance, tank, stops):
i = 0 # counter for the current stop
distance_driven = 0
count_stops = 0
stops.append(100000000000) # using this to avoid an out of index error for the second while loop
while tank + distance_driven < distance: # check if goal state is achieved
if tank + distance_driven < stops[i + 1]:
return -1 # returned if a distance is unreachable
else:
while tank + distance_driven >= stops[i]: # greedy assumption to "full-up" at the furthest reachable gas station
i = i + 1
i = i - 1
distance_driven = stops[i]
count_stops = count_stops + 1
return count_stops


This is my implementation of a greedy algorithm.



The Input is read from the command line in the following format:




950 400 4 200 375 550 750




where the first the first number refers to d (distance), the second refers to t (the tank), the third to n (the number of gas stations on the way) and the other numbers show the locations of n gas stations.



The output will return an integer with the minimum refills needed to reach the distance:




2




In the example the distance between the cities is 950, the car can travel at most 400 miles on a full tank. It suffices to make two refills: at points 375 and 750. This is the minimum number of refills as with a single refill one would only be able to travel at most 800 miles.



Maybe the real question is how can I test my algortihm for mistakes ?










share|improve this question
























  • This question might not be a fit here; cstheory.stackexchange.com might be better. However, it could be a better fit here if you stated what the problem with your algorithm is.

    – wallyk
    Mar 21 at 20:18







  • 3





    So what's wrong with your code?

    – Blorgbeard
    Mar 21 at 20:21











  • Sorry I forgot the most important part. For some inputs the algorithm outputs a wrong solution. The inputs come in the form of integer in the following order: distance, tank, stops

    – Nils
    Mar 21 at 20:42












  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described.

    – Prune
    Mar 21 at 21:09











  • Can you edit your post and add the specific inputs, expected output and actual output?

    – Blorgbeard
    Mar 21 at 22:53















-1















The problem is the following :



  • You are going to travel to another city that is located d miles away
    from your home city.

  • You can travel at most m miles on a full tank and you start with a
    full tank.

  • Along your way, there are gas stations at distances stop1,
    stop2,..., stopn from your home city.

What is the minimum number of refills needed?



So far I have come up with the following:



def min_refills(distance, tank, stops):
i = 0 # counter for the current stop
distance_driven = 0
count_stops = 0
stops.append(100000000000) # using this to avoid an out of index error for the second while loop
while tank + distance_driven < distance: # check if goal state is achieved
if tank + distance_driven < stops[i + 1]:
return -1 # returned if a distance is unreachable
else:
while tank + distance_driven >= stops[i]: # greedy assumption to "full-up" at the furthest reachable gas station
i = i + 1
i = i - 1
distance_driven = stops[i]
count_stops = count_stops + 1
return count_stops


This is my implementation of a greedy algorithm.



The Input is read from the command line in the following format:




950 400 4 200 375 550 750




where the first the first number refers to d (distance), the second refers to t (the tank), the third to n (the number of gas stations on the way) and the other numbers show the locations of n gas stations.



The output will return an integer with the minimum refills needed to reach the distance:




2




In the example the distance between the cities is 950, the car can travel at most 400 miles on a full tank. It suffices to make two refills: at points 375 and 750. This is the minimum number of refills as with a single refill one would only be able to travel at most 800 miles.



Maybe the real question is how can I test my algortihm for mistakes ?










share|improve this question
























  • This question might not be a fit here; cstheory.stackexchange.com might be better. However, it could be a better fit here if you stated what the problem with your algorithm is.

    – wallyk
    Mar 21 at 20:18







  • 3





    So what's wrong with your code?

    – Blorgbeard
    Mar 21 at 20:21











  • Sorry I forgot the most important part. For some inputs the algorithm outputs a wrong solution. The inputs come in the form of integer in the following order: distance, tank, stops

    – Nils
    Mar 21 at 20:42












  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described.

    – Prune
    Mar 21 at 21:09











  • Can you edit your post and add the specific inputs, expected output and actual output?

    – Blorgbeard
    Mar 21 at 22:53













-1












-1








-1








The problem is the following :



  • You are going to travel to another city that is located d miles away
    from your home city.

  • You can travel at most m miles on a full tank and you start with a
    full tank.

  • Along your way, there are gas stations at distances stop1,
    stop2,..., stopn from your home city.

What is the minimum number of refills needed?



So far I have come up with the following:



def min_refills(distance, tank, stops):
i = 0 # counter for the current stop
distance_driven = 0
count_stops = 0
stops.append(100000000000) # using this to avoid an out of index error for the second while loop
while tank + distance_driven < distance: # check if goal state is achieved
if tank + distance_driven < stops[i + 1]:
return -1 # returned if a distance is unreachable
else:
while tank + distance_driven >= stops[i]: # greedy assumption to "full-up" at the furthest reachable gas station
i = i + 1
i = i - 1
distance_driven = stops[i]
count_stops = count_stops + 1
return count_stops


This is my implementation of a greedy algorithm.



The Input is read from the command line in the following format:




950 400 4 200 375 550 750




where the first the first number refers to d (distance), the second refers to t (the tank), the third to n (the number of gas stations on the way) and the other numbers show the locations of n gas stations.



The output will return an integer with the minimum refills needed to reach the distance:




2




In the example the distance between the cities is 950, the car can travel at most 400 miles on a full tank. It suffices to make two refills: at points 375 and 750. This is the minimum number of refills as with a single refill one would only be able to travel at most 800 miles.



Maybe the real question is how can I test my algortihm for mistakes ?










share|improve this question
















The problem is the following :



  • You are going to travel to another city that is located d miles away
    from your home city.

  • You can travel at most m miles on a full tank and you start with a
    full tank.

  • Along your way, there are gas stations at distances stop1,
    stop2,..., stopn from your home city.

What is the minimum number of refills needed?



So far I have come up with the following:



def min_refills(distance, tank, stops):
i = 0 # counter for the current stop
distance_driven = 0
count_stops = 0
stops.append(100000000000) # using this to avoid an out of index error for the second while loop
while tank + distance_driven < distance: # check if goal state is achieved
if tank + distance_driven < stops[i + 1]:
return -1 # returned if a distance is unreachable
else:
while tank + distance_driven >= stops[i]: # greedy assumption to "full-up" at the furthest reachable gas station
i = i + 1
i = i - 1
distance_driven = stops[i]
count_stops = count_stops + 1
return count_stops


This is my implementation of a greedy algorithm.



The Input is read from the command line in the following format:




950 400 4 200 375 550 750




where the first the first number refers to d (distance), the second refers to t (the tank), the third to n (the number of gas stations on the way) and the other numbers show the locations of n gas stations.



The output will return an integer with the minimum refills needed to reach the distance:




2




In the example the distance between the cities is 950, the car can travel at most 400 miles on a full tank. It suffices to make two refills: at points 375 and 750. This is the minimum number of refills as with a single refill one would only be able to travel at most 800 miles.



Maybe the real question is how can I test my algortihm for mistakes ?







python algorithm greedy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 23 at 9:24







Nils

















asked Mar 21 at 20:07









NilsNils

14




14












  • This question might not be a fit here; cstheory.stackexchange.com might be better. However, it could be a better fit here if you stated what the problem with your algorithm is.

    – wallyk
    Mar 21 at 20:18







  • 3





    So what's wrong with your code?

    – Blorgbeard
    Mar 21 at 20:21











  • Sorry I forgot the most important part. For some inputs the algorithm outputs a wrong solution. The inputs come in the form of integer in the following order: distance, tank, stops

    – Nils
    Mar 21 at 20:42












  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described.

    – Prune
    Mar 21 at 21:09











  • Can you edit your post and add the specific inputs, expected output and actual output?

    – Blorgbeard
    Mar 21 at 22:53

















  • This question might not be a fit here; cstheory.stackexchange.com might be better. However, it could be a better fit here if you stated what the problem with your algorithm is.

    – wallyk
    Mar 21 at 20:18







  • 3





    So what's wrong with your code?

    – Blorgbeard
    Mar 21 at 20:21











  • Sorry I forgot the most important part. For some inputs the algorithm outputs a wrong solution. The inputs come in the form of integer in the following order: distance, tank, stops

    – Nils
    Mar 21 at 20:42












  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described.

    – Prune
    Mar 21 at 21:09











  • Can you edit your post and add the specific inputs, expected output and actual output?

    – Blorgbeard
    Mar 21 at 22:53
















This question might not be a fit here; cstheory.stackexchange.com might be better. However, it could be a better fit here if you stated what the problem with your algorithm is.

– wallyk
Mar 21 at 20:18






This question might not be a fit here; cstheory.stackexchange.com might be better. However, it could be a better fit here if you stated what the problem with your algorithm is.

– wallyk
Mar 21 at 20:18





3




3





So what's wrong with your code?

– Blorgbeard
Mar 21 at 20:21





So what's wrong with your code?

– Blorgbeard
Mar 21 at 20:21













Sorry I forgot the most important part. For some inputs the algorithm outputs a wrong solution. The inputs come in the form of integer in the following order: distance, tank, stops

– Nils
Mar 21 at 20:42






Sorry I forgot the most important part. For some inputs the algorithm outputs a wrong solution. The inputs come in the form of integer in the following order: distance, tank, stops

– Nils
Mar 21 at 20:42














Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described.

– Prune
Mar 21 at 21:09





Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described.

– Prune
Mar 21 at 21:09













Can you edit your post and add the specific inputs, expected output and actual output?

– Blorgbeard
Mar 21 at 22:53





Can you edit your post and add the specific inputs, expected output and actual output?

– Blorgbeard
Mar 21 at 22:53












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55288505%2fcar-fueling-algorithm-in-python%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55288505%2fcar-fueling-algorithm-in-python%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript