diff --git a/equivalence-relation/projeto-logica-unifacisa/BinaryRelationUtils.py b/equivalence-relation/projeto-logica-unifacisa/BinaryRelationUtils.py index 2e40135..e54f6bf 100644 --- a/equivalence-relation/projeto-logica-unifacisa/BinaryRelationUtils.py +++ b/equivalence-relation/projeto-logica-unifacisa/BinaryRelationUtils.py @@ -17,7 +17,11 @@ def verify_reflexivity(binary_relation, input_set): Return True if the binary relation in the given input set is reflexive or False if it is not. """ - # TODO: Replace line below with actual code. + for x in input_set: + if (x, x) not in binary_relation.relation(input_set): + return False + + return True pass @staticmethod @@ -33,7 +37,13 @@ def verify_symmetry(binary_relation, input_set): Return True if the binary relation in the given input set is symmetric or False if it is not. """ - # TODO: Replace line below with actual code. + relation = binary_relation.relation(input_set) + for x in input_set: + for y in input_set: + if(x, y) in relation and (y, x) not in relation: + return False + + return True pass @staticmethod @@ -49,7 +59,15 @@ def verify_transitivity(binary_relation, input_set): Return True if the binary relation in the given input set is transitive or False if it is not. """ - # TODO: Replace line below with actual code. + relation = binary_relation.relation(input_set) + + for x in input_set: + for y in input_set: + for z in input_set: + if (x, y) in relation and (y, z) in relation and (x, z) not in relation: + return False + + return True pass @staticmethod @@ -62,10 +80,16 @@ def verify_antisymmetry(binary_relation, input_set): input_set - A set closed under the binary relation. - Return True if the binary relation in the given input set is + Return True if the binary relation in the given input set is antisymmetric or False if it is not. """ - # TODO: Replace line below with actual code. + relation = binary_relation.relation(input_set) + + for x, y in relation: + if (y, x) in relation and x != y: + return False + + return True pass @staticmethod @@ -78,10 +102,10 @@ def verify_equivalency(binary_relation, input_set): input_set - A set closed under the binary relation. - Return True if the binary relation in the given input set is + Return True if the binary relation in the given input set is an equivalence relation or False if it is not. """ - # TODO: Replace line below with actual code. + return BinaryRelationUtils.verify_reflexivity(binary_relation, input_set) and BinaryRelationUtils.verify_symmetry(binary_relation, input_set) and BinaryRelationUtils.verify_transitivity(binary_relation, input_set) pass @staticmethod @@ -98,5 +122,20 @@ def get_partitioning(binary_relation, input_set): Return None if the binary relation is not an equivalence relation or a partitioning of the input set (e.g.: [{1, 3, 5, ...}, {2, 4, 6, ...}]) if it is an equivalence relation. """ - # TODO: Replace line below with actual code. - return [] + return_list = [] + + relation = binary_relation.relation(input_set) + + if BinaryRelationUtils.verify_equivalency(binary_relation, input_set): + for x in input_set: + partition = set() + for y in input_set: + if (x, y) in relation: + partition.add(y) + + if partition not in return_list: + return_list.append(partition) + + return return_list + else: + return None diff --git a/equivalence-relation/projeto-logica-unifacisa/GreaterThanBinaryRelation.py b/equivalence-relation/projeto-logica-unifacisa/GreaterThanBinaryRelation.py index f4ac0f4..c54e3db 100644 --- a/equivalence-relation/projeto-logica-unifacisa/GreaterThanBinaryRelation.py +++ b/equivalence-relation/projeto-logica-unifacisa/GreaterThanBinaryRelation.py @@ -7,15 +7,7 @@ class GreaterThanBinaryRelation(BinaryRelation): """A Binary Relation which elements in an ordered pair share the same first letter.""" def contains_ordered_pair(self, x, y): - """ - This method returns a boolean value indicating if the first element of a given ordered pair is greater than the second one. - - Arguments: - x - The first element of the ordered pair. - y - The second element of the ordered pair. - - Return True if the first element of the ordered pair is greater than the second element, otherwise, return False. - """ + return x > y pass def relation(self, S): @@ -27,4 +19,6 @@ def relation(self, S): Return a set of pairs in SxS (a.k.a. S²) that belong to the binary relation. """ + + return set([(x, y) for x in S for y in S if self.contains_ordered_pair(x, y)]) pass diff --git a/equivalence-relation/projeto-logica-unifacisa/SameFirstLetterBinaryRelation.py b/equivalence-relation/projeto-logica-unifacisa/SameFirstLetterBinaryRelation.py index 636da69..6440321 100644 --- a/equivalence-relation/projeto-logica-unifacisa/SameFirstLetterBinaryRelation.py +++ b/equivalence-relation/projeto-logica-unifacisa/SameFirstLetterBinaryRelation.py @@ -16,6 +16,7 @@ def contains_ordered_pair(self, x, y): Return True if the ordered pair belongs to the binary relation, otherwise, return False. """ + return x[0] == y[0] pass def relation(self, S): @@ -27,4 +28,5 @@ def relation(self, S): Return a set of pairs in SxS (a.k.a. S²) that belong to the binary relation. """ + return set([(x, y) for x in S for y in S if self.contains_ordered_pair(x, y)]) pass diff --git a/equivalence-relation/projeto-logica-unifacisa/SameParityBinaryRelation.py b/equivalence-relation/projeto-logica-unifacisa/SameParityBinaryRelation.py index 8a1f0f4..722e400 100644 --- a/equivalence-relation/projeto-logica-unifacisa/SameParityBinaryRelation.py +++ b/equivalence-relation/projeto-logica-unifacisa/SameParityBinaryRelation.py @@ -18,6 +18,13 @@ def contains_ordered_pair(self, x, y): Return True if the ordered pair belongs to the binary relation, otherwise, return False. """ + + if x % 2 == 0 and y % 2 == 0: + return True + elif x % 2 != 0 and y % 2 != 0: + return True + else: + return False pass def relation(self, S): @@ -29,4 +36,5 @@ def relation(self, S): Return a set of pairs in SxS (a.k.a. S²) that belong to the binary relation. """ + return set([(x, y) for x in S for y in S if self.contains_ordered_pair(x, y)]) pass