Answers for "python All()"

0

all in python

from dataclasses import dataclass
from enum import Enum
from typing import List


class ParkingSpotStatus(str, Enum):
    FREE = "FREE"
    OCCUPIED = "OCCUPIED"


@dataclass
class ParkingSpot:
    number: int
    status: ParkingSpotStatus


# With for and if
@dataclass
class Garage:
    parking_spots: List[ParkingSpot]

    def is_full(self):
        full = True
        for spot in self.parking_spots:
            if spot.status == ParkingSpotStatus.FREE:
                full = False
                break
        return full


garage = Garage(parking_spots=[ParkingSpot(number=1, status=ParkingSpotStatus.OCCUPIED)])
print(garage.is_full())


# With all
@dataclass
class Garage:
    parking_spots: List[ParkingSpot]

    def is_full(self):
        return all(spot.status == ParkingSpotStatus.OCCUPIED for spot in self.parking_spots)


garage = Garage(parking_spots=[ParkingSpot(number=1, status=ParkingSpotStatus.OCCUPIED)])
print(garage.is_full())
Posted by: Guest on September-13-2021

Python Answers by Framework

Browse Popular Code Answers by Language