Question

In: Computer Science

In python this structure can be represented by a set of tuples, where each tuple has...

In python this structure can be represented by a set of tuples, where each tuple has two elements. The following two lines would build the set given above and the print it. >>> L = [(’dog’,’white’), (’cat’,’black’),(’mouse’,’black’)] >>> f = set(L) >>> print(f) {(’cat’, ’black’), (’dog’, ’white’), (’mouse’, ’black’)} In the example, first we store the tuples into a list, and then we create a set with those tuples. There are obviously countless other ways to initialize f and get the same result. The following example builds a similar structure, but has a problem: 1 >>> L = L = [(’dog’,’white’), (’cat’,’black’),(’dog’,’black’)] >>> f = set(L) >>> print(f) {(’dog’, ’black’), (’cat’, ’black’), (’dog’, ’white’)} The problem is that in this case f is not the graph of a function because there are two entries where dog is the first element, and this is contrary to the definition of a function (every element in the domain must be associated with exactly one element in the codomain.)

With the above assumptions we then represents functions by giving their graph. Write two functions in python that satisfy the following specifications:

1. Write a function is_a_graph(A,B,f)

that receives three parameters. The first is the domain, the second is the codomain, and the third is a subset of A × B. The function returns True if f represents the graph of a function f : A → B and False otherwise.

2. Write a function is_surjective(A,B,f)

that receives three parameters. The first is the domain, the second is the codomain, and the third is the graph of a function represented as above. The function returns True if f is the graph of a surjective function and False otherwise. You can assume that in this case f is the graph of a function, i.e., you can ignore the case when f is not the graph of a function.

Suggestions: to solve this exercise it is necessary to analyze all elements in f. To this end, you can re-use the code provided in the file functionsexamples.py that is linked from the lab assignment. Therein we give the code for a function is injective that can be modified to answer the questions given above.

___________________________use this code__in python_________________________________________________________________________________________________

# creates the domain
A = set(['dog','cat','fish','frog'])
# creates the codomain
B = set([1,2,3,4,5,6])

# creates the graph of a function
f = set([('dog',1),('cat',1),('frog',5),('fish',4)])
# This corresponds to the following function
# f(dog) = 1
# f(cat) = 1
# f(frog) = 5
# f(fish) = 4

# prints domain and codomain as sets
print('Domain A')
print(A)
print('Codomain B')
print(B)

# prints the graph of the function as a set
print('Graph of the function')
print(f)

# iterates over all the tuples in f and prints them to the screen one per row
for element in f:
print(element)
  
# prints all the first elements in the tuples in f; observe that [0] accesses the first element in the tuple
for element in f:
print(element[0])
  
  
# prints all the second elements in the tuples in f; observe that [1] accesses the second element in the tuple
for element in f:
print(element[1])
  


def is_injective(A,B,f):
"""
Determines if f is the graph of an injective function.
We assume that f is a valid graph.

Note: in this case parameters are not necessary and not even used.
However they are included for uniformity with the questions you have to
answer in the lab.
Parameters
----------
A: set
A is the domain of the function
B: set
B is the codomain of the function
f : set of tuples
f is the graph of a function.

Returns
-------
bool
True if f is the graph of an injective function and False if it is not.

"""
for element in f: # analyze all elements in the graph
a = element[0] # first element in the tuple (not needed -- just for clarity)
b = element[1] # second element: b = f(a)
for other in f: # consider other elements in the graph
if other != element:
bprime = other[1] # Same as above
if b == bprime: # if two elements have the same image, the function is not injective
return False
# if this point is reached, all elements have been analyzed and it has not
# found two elements with the same image; so the function is injective
return True
  

check1 = is_injective(A,B,f)
if check1 == True:
print('Function f is injective')
else:
print('Function f is not injective')

# change f to make it injective
f = set([('dog',1),('cat',2),('frog',5),('fish',4)])

check2 = is_injective(A,B,f)
if check2 == True:
print('Function f is injective')
else:
print('Function f is not injective')

Solutions

Expert Solution

#determines if f is a valid graph
def is_a_graph(A,B,f):
for element in f:
#if first element of tuple is not in domain and/or second element of tuple is not in codomain
#it means the f is not a graph
#thus, return false

if(element[0] not in A or element[1] not in B):
return False
#if every element of f is ok, it is a graph
return True

#determines if f is a surjective function
def is_surjective(A,B,f):
y = []
#create a list with every codomain in f
for element in f:
y.append(element[1])
#if every element of actual codomain is not in codomain in f,
#it means the function is not surjective

for codomains in B:
if codomains not in y:
return False
return True

As the function simply returns True and False, I am not showing any sample execution. But the functions work.

In case of any doubt, drop a comment and I'll surely get back to you.

Please give a like if you're satisfied with the answer. Thank you.


Related Solutions

#Write a function called find_max_sales. find_max_sales will #have one parameter: a list of tuples. Each tuple...
#Write a function called find_max_sales. find_max_sales will #have one parameter: a list of tuples. Each tuple in the #list will have two items: a string and an integer. The #string will represent the name of a movie, and the integer #will represent that movie's total ticket sales (in millions #of dollars). # #The function should return the movie from the list that #had the most sales. Return only the movie name, not the #full tuple. #Below are some lines of...
Assume someone has a secret 4-tuple (w, x, y, z) where each number can be from...
Assume someone has a secret 4-tuple (w, x, y, z) where each number can be from 1 to 1000 inclusive. Your code is supposed to find what these integers are by using a loop and comparing the number (part of Brute Force). One approach is to write four nested for-loops to check for the number. This is O(n 4 ). Your task is to determine a random 4-tuple. Is your solution better than O(n^4 )? 1 import random as rn...
PYTHON Question - An acrostic is a form of writing where a set letter of each...
PYTHON Question - An acrostic is a form of writing where a set letter of each line spells out a word or message. Often acrostics (especially poems) use the first letter to spell out their messages, but other “columns” in the text may be used. For this lab, we’ll create an acrostic reader. Given the poem and dream data, write a function called print_acrostic() that takes a list that contains two elements: a string containing the poem/dream, and an integer...
Describe how tuples can be useful with loops over lists and dictionaries, and give Python code...
Describe how tuples can be useful with loops over lists and dictionaries, and give Python code examples. Create your own code examples. Do not copy them from the textbook or any other source. Your descriptions and examples should include the following: the zip function, the enumerate function, and the items method.
Cu3Au undergoes a phase transition. At elevated temperatures, it has the structure of Cu, where each...
Cu3Au undergoes a phase transition. At elevated temperatures, it has the structure of Cu, where each position is filled randomly with Cu or gold atoms (i.e. at each position, there is a 1/4 probability that the atom is Au, and a 3/4 probability that the atom is Cu). At lower temperatures, the thermodynamically stable phase is a cubic one with space group Pm3m (#221), with a= 3.74Å. Cu is at (0, 0.5, 0.5) and Au is at (0,0,0). a.Show unit...
Assume that you have a set of jobs where each has only a processing time that...
Assume that you have a set of jobs where each has only a processing time that you need to schedule on a single machine. Explain how you should schedule the jobs to minimize the sum of completion (finish) times. Write a proof that your schedule is optimal.
The global demand for cocoa can be represented with the following equation: P=50 - 0.25Q, where...
The global demand for cocoa can be represented with the following equation: P=50 - 0.25Q, where P is the price (dollars per 100 lbs.), and Q is quantity. Furthermore, assume that cocoa can be produced at a constant marginal and average cost of $10 per unit of Q. Cocoa producers have formed a cartel, aimed at realizing the monopoly price for cocoa. Given the demand equation and marginal cost specified above, what is the monopoly price and quantity?
The global demand for cocoa can be represented with the following equation: P=50 - 0.25Q, where...
The global demand for cocoa can be represented with the following equation: P=50 - 0.25Q, where P is the price (dollars per 100 lbs.), and Q is quantity. Furthermore, assume that cocoa can be produced at a constant marginal and average cost of $10 per unit of Q. Cocoa producers have formed a cartel, aimed at realizing the monopoly price for cocoa. Given the demand equation and marginal cost specified above, what is the monopoly price and quantity? The monopoly...
I. The demand curve for chocolate can be represented by P = 1,500-10Q, where P is...
I. The demand curve for chocolate can be represented by P = 1,500-10Q, where P is the price per ton (in U.S. dollars), and Q is expressed in tons per year. The private marginal cost of production in the chocolate industry is given by 20Q. But chocolate production causes a nauseating odor to sweep over the city in the vicinity of chocolate factories, so the social marginal cost of production, taking the odor into account, is 30Q a. (3 points)...
Create a C structure which stores information about a student. Each student should be represented by...
Create a C structure which stores information about a student. Each student should be represented by a student ID (integer), first name, last name, day, month and year of birth, and program code (string). Write a C program which creates an array of 100 of these structures, then prompts the user to enter data from the keyboard to fill the array. If an ID of 0 is entered, data entry should finish, and the list of students should be printed...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT