Home > Software engineering >  Error during output in Design Parking system question
Error during output in Design Parking system question

Time:10-04

I am solving this question on LeetCode. I have the following code:

class ParkingSystem:

    def __init__(self, big: int, medium: int, small: int):
        self.big=big
        self.medium =medium
        self.small =small

    def addCar(self, carType: int) -> bool:
        if carType == 1 and self.big>0:
            self.big -=1
            return 'true'
        elif carType ==2 and self.medium>0:
            self.medium -=1
            return 'true'
        elif carType ==3 and self.small>0:
            self.small -=1
            return 'true'
        else:
            return 'false'

I am unable to understand, why I am getting the wrong output?
Input

["ParkingSystem", "addCar", "addCar", "addCar", "addCar"]
[[1, 1, 0], [1], [2], [3], [1]]

Output

[null, true, true, false, false]

my output: Output

[null,true,true,true,true]

Please help. Thanks in advance.

CodePudding user response:

Your function will return a boolean value because you have put a boolean pointer at the function definition.

Change this:

def addCar(self, carType: int) -> bool:

To this:

def addCar(self, carType: int) -> str:

Of course, you can also change the return string to actual booleans too.

CodePudding user response:

class ParkingSystem:

    def __init__(self, big: int, medium: int, small: int):
        self.big=big
        self.medium =medium
        self.small =small

    def addCar(self, carType: int) -> bool:
        if carType == 1 and self.big>0:
            self.big -=1
            return True
        elif carType ==2 and self.medium>0:
            self.medium -=1
            return True
        elif carType ==3 and self.small>0:
            self.small -=1
            return True
        else:
            return False

Return the boolean value either True or False.

  • Related