From fea034905fa9c1efb1494d280cb9bb4115eb7bd7 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Wed, 4 Oct 2023 18:23:57 +0200 Subject: Pass TestFiniteSetRepresentation --- src/act4e_solutions/sets_representation.py | 39 +++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) (limited to 'src') 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} -- cgit v1.2.1