[Python] | Left-right for even; right-left for odd philosophers
Intuition\nCreate a Lock for each fork. Each philosopher tries to acquire 2 forks adjacent to him, before eating\n\nIf all philosophers take the fork to the lef
# Intuition\nCreate a Lock for each fork. Each philosopher tries to acquire 2 forks adjacent to him, before eating\n\nIf all philosophers take the fork to the left first, there can be a deadlock, when all of them got only 1 fork.\n\n# Approach\nTo resolve the deadlock, make even philosopers take left first, then right. And for odd ones - vice versa.\n\n# Code\n```\nfrom threading import Lock\n\nclass DiningPhilosophers:\n    def __init__(self):\n        self.forks = [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        \n        left_fork, right_fork = self.forks[philosopher], self.forks[philosopher - 1]\n        \n        #for even philosopers pick first left then right, for odd - first right then left\n        if philosopher%2: \n            first_fork, second_fork = right_fork, left_fork\n        else:\n            first_fork, second_fork = left_fork, right_fork\n\n        with first_fork, second_fork:\n            pickLeftFork()\n            pickRightFork()\n            eat()\n            putRightFork()\n            putLeftFork()\n```
['Python3']