#πŸ”’ Test veifiried but code not passing in online judge

22 messages Β· Page 1 of 1 (latest)

lost pecan
#

import sys

def readstr(): return sys.stdin.readline().strip()
def readfloats(): return list(map(float, readstr().split()))


while(True):

    N, W = readfloats() 
    N = int(N)
    if (N == 0 and W == 0.0000):
        break

    max_h = 0
    sum_width = 0 
    total = 0
    
    for i in range(N):
        h, w = readfloats()

        if sum_width + w > W:
            total += max_h
            sum_width = w
            max_h = h
            
        else: 
            if(h > max_h):
                max_h = h
            sum_width+=w

    total += max_h
    print (f"{total:.4f}")

``` i believe my code is good as it pass the test on the file but it marks wrong answer, is something wrong in my logic ? thanks for reading here the link to the online judge problem : https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=0&problem=1180&mosmsg=Submission+received+with+ID+29872353
stiff kayakBOT
#

@lost pecan

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

wide cosmos
#

What's the while True: for? Are you actually expecting several batches of numbers?

lost pecan
#

Yes as in the problem they said it stops until the line 0 0.0000 appear thats why i put while true

#

There is a test with 5 30.0000 ( 5 books, results is 60) and one with 10 20.0000 (10 books result is 65Γ 

wide cosmos
lost pecan
#

no problem !

wide cosmos
#

Your online judge output looks like a time limit was reached.

#

Try changing this: print (f"{total:.4f}") into this: print (f"{total:.4f}", flush=True) to see if you get any output back from the online judge. (Python output is buffered, so it might not get out before the timeout if the buffer doesn't fill. This flushes the buffer as soon as you print.)

#

Doesn't change your programme behaviour, may provide better indication of what's going on.

#

I don't suppose you know what input it is being tested with?

lost pecan
#

Unfortunately no they only give the sample given in the file i send but i was warned that judges were more harsh on python because of the time

#

i'll test out your solution

#

i still have wrong answer

wide cosmos
#

I expect it to still be wrong. I was hoping the report showed some of your print()s in the output.

#

I don't see anything in the code which is inherently slow.

#

And I don't expect it to spin forever if you somehow miss the end-of-data 0 0.0000 pair - it should just fail if so. So I'm puzzled.

#

You could put in a print() after the N, W = readfloats() line to show N and W; it might show you what size data they're giving you, and maybe show some loss of sync with the input data, though your code looks correct to my eye for that.

stiff kayakBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.