Reputation: 85
Here's the word problem: It takes exactly 2 minutes and 7 seconds to produce an item. Unfortunately, after 143 items are produced, the fabricator must cool off for 5 minutes and 13 seconds before it can continue. Write a program that will calculate the amount of time required to manufacture a given number of items.
The test number is 1340 items.
numItems = 1340
produceitem = 2 * 60 + 7 #2 minutes and 7 seconds
cooldown = 5 * 60 + 13 #5 minutes and 13 seconds
items_before_delay = 143
productiontime = 0
if numItems <= 143:
productiontime = produceitem * numItems
if numItems > 143:
productiontime = (produceitems * numItems) - (numItems / items_before_delay * cooldown)
print str(productiontime) + "seconds"
The output for the test number is supposed to be 172997 seconds but my program outputs it as 167363 seconds.
Can anyone let me know what I can do to improve this?
Upvotes: 2
Views: 74
Reputation: 365975
You're subtracting the cooldown time, instead of adding it. That's it.
So, change this:
productiontime = (produceitems * numItems) - (numItems / items_before_delay * cooldown)
… to this:
productiontime = (produceitems * numItems) + (numItems / items_before_delay * cooldown)
However, while we're here:
produceitem
, but used produceitems
. If this worked at all, it's probably because you got lucky in the interactive interpreter, having already defined produceitems
as well.items_before_delay
, don't use the number 143 directly, use items_before_delay
.if a <= b:
then if a > b:
; just change the second one to else:
.if
at all. If numItems <= 143
, (numitems / items_before_delay * cooldown)
will be 0, so the second version will still give the right answer.//
for truncating integer division than /
. That means your code still works in Python 3.x, or if someone does a __future__
statement, etc.—and, more importantly, it means humans can read and understand your code without having to guess whether it was for 2.x or 3.x.items_before_delay
follows PEP8 recommendations, but numItems
does not.productiontime
before setting it.172997seconds
without a space.So:
num_items = 1340
produce_item = 2 * 60 + 7 #2 minutes and 7 seconds
cooldown = 5 * 60 + 13 #5 minutes and 13 seconds
items_before_delay = 143
total_cooldown = num_items // items_before_delay * cooldown
production_time = (produce_item * num_items) + total_cooldown
print '{} seconds'.format(production_time)
Upvotes: 2