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
  hi need help to make this code work correctly atulkul1985 5 701 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 626 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Cleaning my code to make it more efficient BSDevo 13 1,275 Sep-27-2023, 10:39 PM
Last Post: BSDevo
  how to make bot that sends instagram auto password reset code kraixx 2 1,282 Mar-04-2023, 09:59 PM
Last Post: jefsummers
  Make code non-blocking? Extra 0 1,099 Dec-03-2022, 10:07 PM
Last Post: Extra
  faster code for my code kucingkembar 19 3,111 Aug-09-2022, 09:48 AM
Last Post: DPaul
  Make the code shorter quest 2 1,476 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 1,751 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
  Pyspark - my code works but I want to make it better Kevin 1 1,745 Dec-01-2021, 05:04 AM
Last Post: Kevin
  List index out of range error when attempting to make a basic shift code djwilson0495 4 2,934 Aug-16-2020, 08:56 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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