Nov-04-2017, 12:12 PM
(This post was last modified: Nov-04-2017, 12:12 PM by DontHurtMe.)
Hello, I've created a simple Python program that does work but goes over the time limits as it runs too slow. I don't know a lot about how to make code run faster, but I hope somebody can help me.
____O
____O
____O
O_O_O
OOO_O
n = int(input()) # N = how many input numbers printer = [None] * n for i in range(0, n): printer[i] = int(input()) # Fill the empty array with input values printing = False prints = 0 top = max(printer) i = 0 while (i < top): i += 1 for num in printer: if (num >= i and not printing): prints += 1 printing = True elif (num < i and printing): printing = False printing = False print(prints)What this program does is that it takes one value for every column. The value is how many "blocks" high is that column. Now there is a 3D printer that will print all the columns row by row (left-to-right). When it meets a gap (a place it should not print), it will turn off and turn on again once it has to print. It also has to turn off when going to the next row. The output is the number of times it had to turn on. Sorry if my explanation was not clear, not sure if it's even needed, as I just have to make it run faster. Here's an example input/output.
Output:Input:
5
2
1
2
0
4
Output:
7
That example would look like this, where O is filled block and _ is empty:____O
____O
____O
O_O_O
OOO_O