P1.Write a Mathematica function reflexiveQ[set, relation]that takes a set and a
ID: 3585937 • Letter: P
Question
P1.Write a Mathematica function reflexiveQ[set, relation]that takes a set and a relation on that set as input. The function returns True if the relation is reflexive, and returns False otherwise. Use the For function and the MemberQ function
Sample output:
In[1]:= reflexiveQ[{1, 2}, {{1, 2}, {1, 1}, {2, 1}, {2, 2}}]
Out[1]:= True
In[2]:= reflexiveQ[{1, 2, 3}, {{1, 1}, {2, 2}, {3, 2}}]
Out[2]:= False
P2.
How many times does “CU” appears in a string? Write a Mathematica function countCU [charlist] that takes a list of single characters and returns the number of times the letter “C” appears followed immedi-ately by “U”. Submit your code, along with the output for the strings "CUandC" and "THE CULTIVATION OF CUTE CUDDLY CUCUMBER CUBES IS CURRENTLY AN OCCUPATIONAL HAZARD"
Sample output:
In[1]:= Characters["CUandC"]
Out[1]:= {C, U, a, n, d, C}
In[2]:= countCU[Characters["CUandC"]]
Out[2]:= 1
In[3]:= countCU[Characters["THE CULTIVATION OF CUTE CUDDLY CUCUMBER CUBES
IS CURRENTLY AN OCCUPATIONAL HAZARD"]]
Out[3]:= 8.
Explanation / Answer
P1.
The pseudo code for the problem can be written as:
set flag to true
for each index i
set a = A[i]
set b = B[i]
set flag to false
for each index j
if A[j] is b
if B[j] is a
set flag to true
break
if flag is false
return false
break
if flag is true
return true
The code is as follows:
is_reflexive(int set[],int relation[])
{
int flag;
int set[],relation[];
int l = set.size();
for(int i=0;i<l;i++)
{
int a = set[i];
int b = relation[i];
flag = 0;
for(int j=0;j<relation.size();j++)
{
if(set[j]==b)
{
if(relation[j] == a)
{
flag = 1;
break;
}
}
}
if(flag == 0)
{
return false;
}
}
if(flag==1)
{
return true;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.