diff options
-rw-r--r-- | Homeworks.md | 4 | ||||
-rw-r--r-- | src/act4e_solutions/posets_construction.py | 29 | ||||
-rw-r--r-- | src/act4e_solutions/posets_representation.py | 53 | ||||
-rw-r--r-- | src/act4e_solutions/sets_power.py | 4 |
4 files changed, 82 insertions, 8 deletions
diff --git a/Homeworks.md b/Homeworks.md index e74ac23..faec500 100644 --- a/Homeworks.md +++ b/Homeworks.md @@ -38,8 +38,8 @@ $ make docker-check - [X] TestFiniteGroupMorphismRepresentation - [X] TestFiniteSemigroupMorphismsChecks -- [ ] TestFinitePosetRepresentation -- [ ] TestFinitePosetConstructionPower +- [>] TestFinitePosetRepresentation +- [X] TestFinitePosetConstructionPower - [ ] TestFinitePosetSubsetProperties - [ ] TestFinitePosetMeasurement - [ ] TestFinitePosetConstructionProduct diff --git a/src/act4e_solutions/posets_construction.py b/src/act4e_solutions/posets_construction.py index ae24f75..bba5b62 100644 --- a/src/act4e_solutions/posets_construction.py +++ b/src/act4e_solutions/posets_construction.py @@ -1,10 +1,37 @@ from typing import Any, TypeVar +from .sets_properties import SolFiniteSetProperties +from .sets_power import MyFiniteSetOfFiniteSubsets, SolFiniteMakePowerSet +from .posets_representation import MyFinitePoset + import act4e_interfaces as I X = TypeVar("X") +C = TypeVar("C") # Type of elements of set +E = TypeVar("E") + +class MyFinitePosetOfFiniteSubsets(I.FinitePosetOfFiniteSubsets[C, E]): + _subsets: I.FiniteSetOfFiniteSubsets[C, E] + def __init__(self, subsets): + self._subsets = subsets + self._prop = SolFiniteSetProperties() + + def carrier(self) -> I.FiniteSetOfFiniteSubsets[C, E]: + return self._subsets + + def holds(self, a: I.FiniteSet[C], b: I.FiniteSet[C]): + if self._prop.equal(a, b): + return True + + if self._subsets.contains(a) and self._subsets.contains(b): + if self._prop.is_subset(a, b): + return True + + return False class SolFinitePosetConstructionPower(I.FinitePosetConstructionPower): def powerposet(self, s: I.FiniteSet[X]) -> I.FinitePosetOfFiniteSubsets[X, Any]: - raise NotImplementedError() + setpow = SolFiniteMakePowerSet() + subsets = setpow.powerset(s) + return MyFinitePosetOfFiniteSubsets(subsets) diff --git a/src/act4e_solutions/posets_representation.py b/src/act4e_solutions/posets_representation.py index c43da82..ed3eaf9 100644 --- a/src/act4e_solutions/posets_representation.py +++ b/src/act4e_solutions/posets_representation.py @@ -1,11 +1,58 @@ -from typing import Any +from typing import TypeVar, Any, List, Tuple + +from .sets_representation import SolFiniteSetRepresentation import act4e_interfaces as I +X = TypeVar("X") + +class MyFinitePoset(I.FinitePoset[X]): + _carrier: I.FiniteSet[X] + # This could be an adjacency list / matrix but I'm too lazy today, + # and python is sloooow so I doubt it would make a difference + _values: List[Tuple[X,X]] + + def __init__(self, carrier: I.FiniteSet[X], values: List[Tuple[X,X]]): + self._carrier = carrier + self._values = values + + def carrier(self) -> I.FiniteSet[X]: + return self._carrier + + def holds(self, a: X, b: X) -> bool: + for v in self._values: + if self.carrier().equal(v[0], a) and self.carrier().equal(v[1], b): + return True + return False + + class SolFinitePosetRepresentation(I.FinitePosetRepresentation): def load(self, h: I.IOHelper, s: I.FinitePoset_desc) -> I.FinitePoset[Any]: - raise NotImplementedError() + if "poset_product" in s.keys(): + raise NotImplementedError + + if "poset_sum" in s.keys(): + raise NotImplementedError + + if not all(k in s.keys() for k in ["carrier", "hasse"]): + raise I.InvalidFormat() + + fsr = SolFiniteSetRepresentation() + X = fsr.load(h, s["carrier"]) + + return MyFinitePoset(X, s["hasse"]) def save(self, h: I.IOHelper, p: I.FinitePoset[Any]) -> I.FinitePoset_desc: - raise NotImplementedError() + fsr = SolFiniteSetRepresentation() + d = {"carrier": fsr.save(h, p.carrier()), + "hasse": []} + + for a in p.carrier().elements(): + for b in p.carrier().elements(): + if p.holds(a, b): + d["hasse"].append([a, b]) + + return d + + diff --git a/src/act4e_solutions/sets_power.py b/src/act4e_solutions/sets_power.py index 98d1c69..1e7ec74 100644 --- a/src/act4e_solutions/sets_power.py +++ b/src/act4e_solutions/sets_power.py @@ -12,7 +12,7 @@ C = TypeVar("C") # Type of elements of set E = MyFiniteSet[C] # Type of sets -class MyFiniteSetOfSubsets(I.FiniteSetOfFiniteSubsets[C, E]): +class MyFiniteSetOfFiniteSubsets(I.FiniteSetOfFiniteSubsets[C, E]): # FIXME: _set could be removed _set: I.FiniteSet[C] _subsets: List[I.FiniteSet[C]] @@ -66,5 +66,5 @@ class SolFiniteMakePowerSet(I.FiniteMakePowerSet): for comb in itertools.combinations(s.elements(), i): subsets.append(MyFiniteSet(list(comb))) - return MyFiniteSetOfSubsets(s, subsets) + return MyFiniteSetOfFiniteSubsets(s, subsets) |