Production Time Program on Python; Algorithm mistake?

Page 1 of 1

1 Replies - 975 Views - Last Post: 24 February 2013 - 08:05 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=313400&amp;s=1c32ed85d3b4277b90a29b0cf17841e5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 compscihelp

Reputation: 0
• Posts: 1
• Joined: 24-February 13

Production Time Program on Python; Algorithm mistake?

Posted 24 February 2013 - 07:55 PM

So I started Python two months ago and found this problem as practice:

It takes exactly 2 minutes and 7 second 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.
Output the amount of time D days HH:MM:SS

I'm having so much trouble that I literally only have:
```numItems = raw_input("Type in number of items to produce ")
```

I tried doing this whole deal at first where I could make a list ranging from for example:

``` range(1, 1341)
```
, find the 143rd position, add 2 minutes and 7 seconds to a variable called time, and then multiply 143rd position by 2 so I could find the 286th position, add another 2 minutes and 7 seconds to the variable called time, but then this error message comes up and I get really frustrated and erase my program.

I'm in desperate need of help. Please maybe give me a starter so I could go off from there?

Is This A Good Question/Topic? 0

Replies To: Production Time Program on Python; Algorithm mistake?

#2 jon.kiparsky

• Pancakes!

Reputation: 6702
• Posts: 10,949
• Joined: 19-March 11

Re: Production Time Program on Python; Algorithm mistake?

Posted 24 February 2013 - 08:05 PM

You might start in the interpreter by defining some constants like for example

```time_to_produce_item = 2 * 60 + 7
number_of_items_before_delay = 143
cool_down_time = 5 * 60 + 13

```

so you won't have so many magic numbers lying around.

Now you can reason about the flow of the thing.

Don't worry about getting the input now:
```test_quantities = [1, 10, 143, 144, 220]
test_values = [] # get a pencil and work out what the correct times ought to be
```

Come up with some more good test cases if you want. Now you can iterate over these cases and compare the results to the calculated values: you're doing test-driven development!

Okay, so having that on hand, think about the stupidest, least intelligent way to solve this problem. You can change it later, but just geta prototype on the table. The stupidest idea I can come up with is to iterate over the rangs and add some number to an accumulator. (how long does it take to produce this one? did we have to let the machine cool down?)

When you get a function that looks okay, test it with your test_quantities. Does it produce the right answers?

When the answer is yes, improve the function. (eliminate some loops by clever observations, that sort of thing)

This post has been edited by jon.kiparsky: 24 February 2013 - 08:09 PM