2

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.

quamrana
  • 37,849
  • 12
  • 53
  • 71
Kumar
  • 173
  • 1
  • 12
  • I can't reproduce your output. I get: `[, 'true', 'true', 'false', 'false']`. btw I would expect that you should be returning either: `True`, or `False`. – quamrana Oct 03 '21 at 16:14
  • Related: https://stackoverflow.com/questions/68946065/why-does-my-code-output-true-when-ran-on-leetcode-but-false-when-ran-on-my-o/68946151#68946151 – LuckElixir Oct 03 '21 at 16:20

2 Answers2

1

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.

LuckElixir
  • 306
  • 3
  • 14
1
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.