diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-12-30 14:29:59 +0100 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-12-30 14:29:59 +0100 |
| commit | 41bdcdb45b0903b974a45094c55462951e05980f (patch) | |
| tree | 3e500338eeaf2e304b898617a712cdbbbc6a7d02 /src/philo | |
| parent | a28ed0f25620c6205516259609ad73d7d70b5ffd (diff) | |
| download | philosophers_test-41bdcdb45b0903b974a45094c55462951e05980f.tar.gz philosophers_test-41bdcdb45b0903b974a45094c55462951e05980f.tar.bz2 philosophers_test-41bdcdb45b0903b974a45094c55462951e05980f.zip | |
Fixing meal_num according to subject
Diffstat (limited to 'src/philo')
| -rw-r--r-- | src/philo/philo.py | 13 | ||||
| -rw-r--r-- | src/philo/table.py | 18 |
2 files changed, 15 insertions, 16 deletions
diff --git a/src/philo/philo.py b/src/philo/philo.py index 3319a0b..3565c41 100644 --- a/src/philo/philo.py +++ b/src/philo/philo.py @@ -6,7 +6,7 @@ # By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/10/01 10:52:56 by cacharle #+# #+# # -# Updated: 2020/10/05 14:03:07 by cacharle ### ########.fr # +# Updated: 2020/12/30 14:29:30 by charles ### ########.fr # # # # ############################################################################ # @@ -25,7 +25,7 @@ class Philo: timeout_sleep: int, meal_num: int ): - self.logs = [] + self.logs = [] self.id = id_ self._timeout_die = timeout_die self._timeout_eat = timeout_eat @@ -56,10 +56,7 @@ class Philo: # check log event number grouped = [(e, list(g)) for e, g in itertools.groupby(self.logs, (lambda x: x.event))] for e, g in grouped[:-1]: - if e is Event.EAT: - if len(g) != self._meal_num: - self._raise("Should eat {} times".format(self._meal_num)) - elif e is Event.FORK: + if e is Event.FORK: if len(g) != 2: self._raise("Should take fork 2 times") elif len(g) != 1: @@ -127,3 +124,7 @@ class Philo: else: return 1 return 0 + + @property + def meal_num_finished(self): + return len([log for log in self.logs if log.event is Event.EAT]) >= self._meal_num diff --git a/src/philo/table.py b/src/philo/table.py index 66eaa9d..804608b 100644 --- a/src/philo/table.py +++ b/src/philo/table.py @@ -6,13 +6,10 @@ # By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/10/01 10:53:29 by cacharle #+# #+# # -# Updated: 2020/10/05 14:03:35 by cacharle ### ########.fr # +# Updated: 2020/12/30 14:26:37 by charles ### ########.fr # # # # ############################################################################ # - -import itertools - from philo import Philo from philo import error from philo.event import Event @@ -31,20 +28,21 @@ class Table: for id_ in range(1, philo_num + 1)] self._logs = [] self._philo_num = philo_num - self.dead = False + self.finished = False + self._meal_num = meal_num def add_log(self, log): """ Add a log to the correct philosopher - Set the dead flag if it's a death log + Set the finished flag if it's a death log """ self._logs.append(log) philo = next(p for p in self._philos if p.id == log.id) philo.logs.append(log) # move - if self.dead: + if self.finished: raise error.Log(self._logs, "Output after death") - if log.event is Event.DIE: - self.dead = True + if log.event is Event.DIE or all(p.meal_num_finished for p in self._philos): + self.finished = True def check(self): """ Check global logs and all philosophers logs for errors @@ -54,7 +52,7 @@ class Table: - Timestamps should be in increasing order """ - if self.dead: + if self.finished: return for p in self._philos: p.check() |
