patternpythonModerate
Determining if a relation is reflexive
Viewed 0 times
determiningrelationreflexive
Problem
A binary relation \$R\$ on a set \$A\$ is called reflexive if and only if \$R(a, a)\$ for every element \$a \in A\$.
I want to know if there can be any improvements made on the function below to make it more efficient.
Test case:
I want to know if there can be any improvements made on the function below to make it more efficient.
def reflexive(R):
'''
@param R : set containing homogenous elements
'''
result = []
a = []
y = []
for a1, a2 in R:
if (a1 == a2):
result.append((a1,a2))
y.append(a1)
if (a1 not in a):
a.append(a1) #contains list of a0
if (set(a) == set(y)):
print("Reflexive")
return (result, True)
print("Not Reflexive")
return ([] , False)Test case:
R2 = reflexive([(1, 3), (1, 5), (3, 1), (3, 5), (7, 5)])
Not Reflexive
[]Solution
Regardless of whether the function is actually correct or not, there are some issues that could be improved:
-
I would expect a function that is supposed to determine whether a relation \$R\$ over a set \$A\$ is reflexive to take two arguments:
Providing only the set \$\{(a, b) | a, b \in A, R(a,b)\}\$ and deducing \$A\$ from it is insufficient, because there could be elements in \$A\$ which are not related to any other element. This could lead to a false positive result.
-
I would expect the function to only return a boolean value, and to have no side effects like printing its result. Leave that to the caller.
-
The docstring of the function does not explain well enough what it expects as its argument, and does not explain at all what the function does.
-
-
Instead of creating
-
Parentheses are not needed around conditions in
(also technically not around the returned tuples)
-
This comment confuses me: What is
You better just remove the comment.
-
I would expect a function that is supposed to determine whether a relation \$R\$ over a set \$A\$ is reflexive to take two arguments:
- The relation \$R\$,
- and the set \$A\$.
Providing only the set \$\{(a, b) | a, b \in A, R(a,b)\}\$ and deducing \$A\$ from it is insufficient, because there could be elements in \$A\$ which are not related to any other element. This could lead to a false positive result.
-
I would expect the function to only return a boolean value, and to have no side effects like printing its result. Leave that to the caller.
-
The docstring of the function does not explain well enough what it expects as its argument, and does not explain at all what the function does.
-
a and y are poorly named. What is their meaning?-
Instead of creating
a and y a as lists to which is appended (needlessly checking if elements are already contained), only to convert them to sets at the end, why don't you just create them as sets in the first place and add to them?-
Parentheses are not needed around conditions in
if statements:if a1 == a2:
if a1 not in a:
if set(a) == set(y):(also technically not around the returned tuples)
return result, True
return [], False-
This comment confuses me: What is
a0?if (a1 not in a):
a.append(a1) #contains list of a0You better just remove the comment.
Code Snippets
if a1 == a2:
if a1 not in a:
if set(a) == set(y):return result, True
return [], Falseif (a1 not in a):
a.append(a1) #contains list of a0Context
StackExchange Code Review Q#152460, answer score: 10
Revisions (0)
No revisions yet.