Solve classic problems by classic solutions (with explanation)
This is a textbook problem. \n\nSolution 1 pick up two forks at the same time\nfaster than 42.16% and less memory than 100% Python 3 code\npython\nclass DiningP
This is a textbook problem. \n\nSolution 1 pick up two forks at the same time\nfaster than 42.16% and less memory than 100% Python 3 code\n```python\nclass DiningPhilosophers:\n    def __init__(self):\n        self._cv = Condition()\n        self._free = [True for _ in range(5)]\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        with self._cv:\n            while (not self._free[philosopher]) or (not self._free[(philosopher + 1) % 4]):\n                self._cv.wait()\n            self._free[philosopher], self._free[(philosopher + 1) % 5] = False, False\n        pickLeftFork()\n        pickRightFork()\n        eat()\n        putLeftFork()\n        putRightFork()\n        with self._cv:\n            self._free[philosopher], self._free[(philosopher + 1) % 5] = True, True\n            self._cv.notify_all()\n```\n\nSolution 2 odd-numbered philosopher pick up left fork first while even-numbered philosopher pickup right fork first\nfaster than 70.27% and less memory than 100% Python 3 code\n```python\nclass DiningPhilosophers:\n    def __init__(self):\n        self._locks = [Lock() for _ in range(5)]\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        order = (philosopher, (philosopher + 1) % 5) if philosopher % 2 else ((philosopher + 1) % 5, philosopher)\n        for i in order:\n            self._locks[i].acquire()\n        pickLeftFork()\n        pickRightFork()\n        eat()\n        putLeftFork()\n        putRightFork()\n        for i in order:\n            self._locks[i].release()\n```
[]