Skip to content
This repository has been archived by the owner on Jul 10, 2022. It is now read-only.

Commit

Permalink
Implements all methods
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelhgf committed Nov 21, 2019
1 parent 325ddcf commit 5f9f654
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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

0 comments on commit 5f9f654

Please sign in to comment.