Kim Ruhol
Kim Ruhol

Reputation: 93

Keep getting Run Time error for a Kattis Problem

I am trying to solve this problem (https://open.kattis.com/problems/anotherbrick).

And when I submit my final code I keep getting a Run Time error. I am not sure what's wrong with my code.

# @Author Gansaikhan Shur
# Another Brick in the Wall 

import sys

# if len(sys.argv) < 2:
#    sys.exit("{}: needs an input file!".format(sys.argv[0]))
#    exit()

sys_input = sys.argv[1]

with open(sys_input, "r") as f:
    data = [line.rstrip('\n') for line in f]
    hw_num = data[0].split()
    len_bricks = data[1].split()
    # Assigning to variables
    height = int(hw_num[0])
    width = int(hw_num[1])
    num_bricks = hw_num[2]

currHeight = 0
for i in range(height):
    currHeight = i+1
    brick_width = 0
    for j in len_bricks:
        brick_width += int(j)
        if currHeight == height and brick_width == width:
            print("YES")
            exit()
        if brick_width == width:
            brick_width = 0
            continue
        elif brick_width > width:
            print("NO")
            exit()
        else:
            continue
    currHeight += 1

Please let me know what I can do to get this code accepted to kattis.com. Thanks

Upvotes: 1

Views: 8357

Answers (1)

Patrick Artner
Patrick Artner

Reputation: 51663

Run time error can mean:

  • you got a error that occures at run time only
  • you got a wrong result (see below the code block - your data gathering was wrong)
  • your code runs too long

Problem 1:

Your code does not increment the height:

 if brick_width == width:
            brick_width = 0
            continue

if you have data like bricks = [1]*10000000 your code will run loooong, because you never increment the height you are at. You need to process the whole list until you get a "NO".

Problem 2:

Your method to read in data for this task is wrong - it is not file based. See the documentation for python on Kattis -you need to read from sys.stdin (code below).


You can streamline it:

def can_he_do_it(h,w,bricks):
    height = 0
    cur_w = 0

    # remove this line for kattis.com
    print("Project: Height: {} Width: {} with {}".format(h,w,bricks)) 

    # process all bricks - not the height or anythin else with range
    # bricks is all you got, place them down one by one, check lenght/height
    # and return False if you are too wide or not high enough
    # return True if you are high enough
    for brick in bricks:
        cur_w += brick        # add to current width and check

        if cur_w > w:         # too wide
            return False
        elif cur_w == w:      # exaclty correct, next row
            height += 1
            cur_w = 0

        if height == h:       # reach target heigth
            return True

    return False              # too few materials


def result(b):
     print("YES" if b else "NO") 

# replace with your number-read-code
result( can_he_do_it(2, 10, [5,5,5,5,5,5,]) )
result( can_he_do_it(2, 10, [5,5,5,3,5,5,]) )
result( can_he_do_it(2, 10, [5,5,5,]) ) 

Output:

Project: Height: 2 Width: 10 with [5, 5, 5, 5, 5, 5]
YES

Project: Height: 2 Width: 10 with [5, 5, 5, 3, 5, 5]
NO

Project: Height: 2 Width: 10 with [5, 5, 5]
NO

Use:

import sys 

data = []
for i in sys.stdin:
    data.append(i)

data = [line.rstrip('\n') for line in data if line]
height, width, *_ = map(int,data[0].split())  # read 2 values, ignore rest, cast to int
bricks = list(map(int, data[1].split()))      # use all bricks, cast to int

# replace with your number-read-code
can_he_do_it(height, width, bricks)

to get:

result

Upvotes: 2

Related Questions