Limiting the number of philosophers to dine
We only allow n-1 philosophers to dine, and the last philosopher has to wait until one of them finishes, thus always allowing us to avoid any deadlock state.\n\
We only allow n-1 philosophers to dine, and the last philosopher has to wait until one of them finishes, thus always allowing us to avoid any deadlock state.\n```\nfrom threading import Lock\nfrom threading import Semaphore\n\nclass DiningPhilosophers:\n    \n    def __init__(self):\n        self.spoons = [Lock() for i in range(5)]\n        self.sem = Semaphore(4)\n\n    # call the functions directly to execute, for example, eat()\n    def wantsToEat(self,\n                   philosopher: int,\n                   pickLeftFork: \'Callable[[], None]\',\n                   pickRightFork: \'Callable[[], None]\',\n                   eat: \'Callable[[], None]\',\n                   putLeftFork: \'Callable[[], None]\',\n                   putRightFork: \'Callable[[], None]\') -> None:\n        \n        left = philosopher\n        right = (philosopher + 1) % 5\n        \n        \n        with self.sem:\n            with self.spoons[left]:\n                with self.spoons[right]:\n                    pickLeftFork()\n                    pickRightFork()\n                    eat()\n                    putLeftFork()\n                    putRightFork()\n```
['Python']