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

Commit

Permalink
Add final code snippets and project tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodolfoams committed Nov 7, 2019
1 parent 15761dc commit b305f09
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import unittest

from BinaryRelationUtils import BinaryRelationUtils
from SameFirstLetterBinaryRelation import SameFirstLetterBinaryRelation
from SameParityBinaryRelation import SameParityBinaryRelation
from GreaterThanBinaryRelation import GreaterThanBinaryRelation


class BinaryRelationTest(unittest.TestCase):

def setUp(self):
self.integers_set = set(range(300))
self.names_set = {"Alice", "Andre", "Bob", "Bruno",
"Charles", "Carlos", "Camila", "Rodolfo", "Zeus"}
self.utils = BinaryRelationUtils()
self.parity_relation = SameParityBinaryRelation()
self.first_letter_relation = SameFirstLetterBinaryRelation()
self.greater_than_relation = GreaterThanBinaryRelation()

def testReflexivityVerification(self):
self.assertTrue(self.utils.verify_reflexivity(
self.parity_relation, self.integers_set))
self.assertTrue(self.utils.verify_reflexivity(
self.first_letter_relation, self.names_set))
self.assertFalse(self.utils.verify_reflexivity(
self.greater_than_relation, self.integers_set))

def testSymmetryVerification(self):
self.assertTrue(self.utils.verify_symmetry(
self.parity_relation, self.integers_set))
self.assertTrue(self.utils.verify_symmetry(
self.first_letter_relation, self.names_set))
self.assertFalse(self.utils.verify_symmetry(
self.greater_than_relation, self.integers_set))

def testTransitivityVerification(self):
self.assertTrue(self.utils.verify_transitivity(
self.parity_relation, self.integers_set))
self.assertTrue(self.utils.verify_transitivity(
self.first_letter_relation, self.names_set))
self.assertTrue(self.utils.verify_transitivity(
self.greater_than_relation, self.integers_set))

def testAntiSymmetryVerification(self):
self.assertFalse(self.utils.verify_antisymmetry(
self.parity_relation, self.integers_set))
self.assertFalse(self.utils.verify_antisymmetry(
self.first_letter_relation, self.names_set))
self.assertTrue(self.utils.verify_antisymmetry(
self.greater_than_relation, self.integers_set))

def testEquivalencyVerification(self):
self.assertTrue(self.utils.verify_equivalency(
self.parity_relation, self.integers_set))
self.assertTrue(self.utils.verify_equivalency(
self.first_letter_relation, self.names_set))
self.assertFalse(self.utils.verify_equivalency(
self.greater_than_relation, self.integers_set))

def testSamples(self):
self.assertTrue(self.parity_relation.contains_ordered_pair(100, 500))
self.assertTrue(self.parity_relation.contains_ordered_pair(101, 501))
self.assertFalse(self.parity_relation.contains_ordered_pair(100, 501))
self.assertTrue(
self.first_letter_relation.contains_ordered_pair("Alice", "Andre"))
self.assertFalse(
self.first_letter_relation.contains_ordered_pair("Alice", "Bruno"))
self.assertTrue(
self.greater_than_relation.contains_ordered_pair(500, 100))
self.assertFalse(
self.greater_than_relation.contains_ordered_pair(100, 500))

def testPartitioning(self):
self.assertSetEqual(len(self.utils.get_partitioning(
self.parity_relation, self.integers_set)), 2)
self.assertSetEqual(len(self.utils.get_partitioning(
self.first_letter_relation, self.names_set)), 5)
self.assertSetEqual(self.utils.get_partitioning(self.first_letter_relation, self.names_set), {{"Alice", "Andre"}, {"Bob", "Bruno"},
{"Charles", "Carlos", "Camila"}, {"Rodolfo"}, {"Zeus"}})


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ 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.
pass

@staticmethod
Expand All @@ -32,6 +33,7 @@ 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.
pass

@staticmethod
Expand All @@ -47,6 +49,7 @@ 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.
pass

@staticmethod
Expand All @@ -62,6 +65,7 @@ def verify_antisymmetry(binary_relation, input_set):
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.
pass

@staticmethod
Expand All @@ -77,6 +81,7 @@ def verify_equivalency(binary_relation, input_set):
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.
pass

@staticmethod
Expand All @@ -91,4 +96,5 @@ 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.
"""
pass
# TODO: Replace line below with actual code.
return {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#coding: utf-8

from BinaryRelation import BinaryRelation


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.
"""
pass

def relation(self, S):
"""
This method returns a set of pairs in SxS (a.k.a. S²) that belong to the binary relation.
Arguments:
S - The input set.
Return a set of pairs in SxS (a.k.a. S²) that belong to the binary relation.
"""
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#coding: utf-8

from BinaryRelation import BinaryRelation


class SameFirstLetterBinaryRelation(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 both elements of a given ordered pair have the same first letter.
Arguments:
x - The first element of the ordered pair.
y - The second element of the ordered pair.
Return True if the ordered pair belongs to the binary relation, otherwise, return False.
"""
pass

def relation(self, S):
"""
This method returns a set of pairs in SxS (a.k.a. S²) that belong to the binary relation.
Arguments:
S - The input set.
Return a set of pairs in SxS (a.k.a. S²) that belong to the binary relation.
"""
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#coding: utf-8

from BinaryRelation import BinaryRelation


class SameParityBinaryRelation(BinaryRelation):
"""
A Binary Relation which elements in an ordered pair have the same parity (divisibility by 2).
"""

def contains_ordered_pair(self, x, y):
"""
This method returns a boolean value indicating if both elements of a given ordered pair have the same parity.
Arguments:
x - The first element of the ordered pair.
y - The second element of the ordered pair.
Return True if the ordered pair belongs to the binary relation, otherwise, return False.
"""
pass

def relation(self, S):
"""
This method returns a set of pairs in SxS (a.k.a. S²) that belong to the binary relation.
Arguments:
S - The input set.
Return a set of pairs in SxS (a.k.a. S²) that belong to the binary relation.
"""
pass

0 comments on commit b305f09

Please sign in to comment.