Skip to content

Weird bug on "Construct the Rectangle" problem #36

@weebao

Description

@weebao

Log:

Traceback (most recent call last): File "/code/tmp8dl2laag.py", line 60, in run_tests passed = 
compare_results(result, test['expected']) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
File "/code/tmp8dl2laag.py", line 25, in compare_results expected = eval(expected); 
return result[0]*result[1] == expected[0]*expected[1] and result[0] >= result[1] 
    and abs(result[0]-result[1]) <= abs(expected[0]-expected[1]) ~~~~~~^^^ IndexError: list index out of range

Reproduction:
Go to "Construct the Rectangle" and execute the following code:

class Solution:
    def constructRectangle(self, area: int) -> List[int]:
        min_diff = float("inf")
        res = []
        L_cache = set()
        for W in range(1, area):
            if W in L_cache:
                break
            if area % W != 0:
                continue
            L = area / W
            L_cache.add(L)
            if L - W < min_diff:
                min_diff = L - W
                res = [L, W]
        return res

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions