diff options
author | Nao Pross <np@0hm.ch> | 2023-10-04 18:23:57 +0200 |
---|---|---|
committer | Nao Pross <np@0hm.ch> | 2023-10-04 18:23:57 +0200 |
commit | fea034905fa9c1efb1494d280cb9bb4115eb7bd7 (patch) | |
tree | 0897109acbf18aa97a37a2d80fb8c60ec9df197a /src | |
parent | Pass TestSimpleIntro (diff) | |
download | act4e-fea034905fa9c1efb1494d280cb9bb4115eb7bd7.tar.gz act4e-fea034905fa9c1efb1494d280cb9bb4115eb7bd7.zip |
Pass TestFiniteSetRepresentation
Diffstat (limited to 'src')
-rw-r--r-- | src/act4e_solutions/sets_representation.py | 39 |
1 files changed, 36 insertions, 3 deletions
diff --git a/src/act4e_solutions/sets_representation.py b/src/act4e_solutions/sets_representation.py index 7f3e818..64d958a 100644 --- a/src/act4e_solutions/sets_representation.py +++ b/src/act4e_solutions/sets_representation.py @@ -1,11 +1,44 @@ -from typing import Any +from typing import Any, Collection, Iterator, List, TypeVar, cast import act4e_interfaces as I +E = TypeVar("E") + +class MyFiniteSet(I.FiniteSet[E]): + _elements: List[E] + def __init__(self, elements: Collection[E]): + self._elements = list(elements) + + def size(self) -> int: + return len(self._elements) + + def contains(self, x: E) -> bool: + return any(map(lambda y: y == x, self._elements)) + + def elements(self) -> Iterator[E]: + return iter(self._elements) + + def save(self, h: I.IOHelper, x: E) -> I.ConcreteRepr: + return cast(I.ConcreteRepr, x) + + def load(self, h: I.IOHelper, o: I.ConcreteRepr): + return cast(E, o) + class SolFiniteSetRepresentation(I.FiniteSetRepresentation): def load(self, h: I.IOHelper, data: I.FiniteSet_desc) -> I.FiniteSet[Any]: - raise NotImplementedError() + if not isinstance(data, dict): + raise I.InvalidFormat() + + if not "elements" in data: + raise I.InvalidFormat() + + if not isinstance(data["elements"], list): + raise I.InvalidFormat() + + elements = data["elements"] + return MyFiniteSet(elements) def save(self, h: I.IOHelper, f: I.FiniteSet[Any]) -> I.FiniteSet_desc: - raise NotImplementedError() + all_elements = [f.save(h, e) for e in f.elements()] + return {"elements": all_elements} |