Python Forum
How do I make this code run faster?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I make this code run faster?
#1
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.

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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Make code run faster: point within polygon lookups Bennygib 2 369 Apr-19-2025, 09:33 AM
Last Post: Larz60+
  How can I make this code more efficient and process faster? steven_ximen 0 399 Dec-17-2024, 04:27 PM
Last Post: steven_ximen
  hi need help to make this code work correctly atulkul1985 5 1,968 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 1,554 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Cleaning my code to make it more efficient BSDevo 13 3,698 Sep-27-2023, 10:39 PM
Last Post: BSDevo
  how to make bot that sends instagram auto password reset code kraixx 2 2,658 Mar-04-2023, 09:59 PM
Last Post: jefsummers
  Make code non-blocking? Extra 0 2,046 Dec-03-2022, 10:07 PM
Last Post: Extra
  faster code for my code kucingkembar 19 6,533 Aug-09-2022, 09:48 AM
Last Post: DPaul
  Make the code shorter quest 2 2,238 Mar-14-2022, 04:28 PM
Last Post: deanhystad
  How would you (as an python expert) make this code more efficient/simple coder_sw99 3 2,621 Feb-21-2022, 10:52 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020