need help with python programming problem challenge use Pyth

need help with python programming problem challenge:

use Python lists to represent these. For example, suppose you have a set A = {1, 2, 3}, a set B = {4, 5}, and a function f : A --> B, f = {(1, 4), (2, 5), (3, 4)}. This would be represented in Python as: A = [1, 2, 3] B = [4, 5] f = [ [1, 4], [2, 5], [3, 4] ]

1. Write a function isFunction(A, B, f) that determines whether the relation f is a function with the domain A and target B. A, B, and f are expressed using lists, as above. For example, calling isFunction(A, B, f) with the above variable values should return True.

2. Write a function image(f) that returns the image of the function f. The elements of the image should be sorted in ascending order. For example, calling image(f) with the above variable values should return the list [4, 5]. Python tip: You can sort a list like this: someList = sorted(someList)

3. Write a function isOneToOne(A, B, f) that determines whether the function f : A --> B is one-to-one. For example, calling isOneToOne(A, B, f) with the above variable values should return False.

4. Write a function isOnto(A, B, f) that determines whether the function f : A --> B is onto. For example, calling isOnto(A, B, f) with the above variable values should return True.

5. Write a function inverse(A, B, f) that returns the inverse of the function f : A --> B. If no inverse exists, return None (this is the Python equivalent of Java’s null). For example, calling inverse(A, B, f) with the above variable values should return None. But if you were to define your variables like this: A = [1, 2, 3] B = [4, 5, 6] f = [ [1, 4], [2, 5], [3, 6] ] then calling inverse(A, B, f) should return something like [ [4, 1], [5, 2], [6, 3] ]

Solution

def isFunction(A, B, f):
domain_dict = {}
for a in A:
domain_dict[a] = 0
for [a, b] in f:
if not a in A:
return False
else:
domain_dict[a] += 1
if b not in B:
return False
for a in domain_dict.keys():
if domain_dict[a] == 0:
return False
  
return True

def image(f):
imag = []
for (a,b) in f:
if not b in imag:
imag.append(b)
imag.sort()
return imag

def isOneToOne(A, B, f):
range_dict = {}
for b in B:
range_dict[b] = 0
domain_dict = {}
for a in A:
domain_dict[a] = 0
  
for (a,b) in f:
domain_dict[a] += 1
if range_dict[b] == 1:
return False
else:
range_dict[b] = 1
for a in domain_dict.keys():
if domain_dict[a] == 0:
return False
return True

def isOnto(A, B, f):
range_dict = {}
for b in B:
range_dict[b] = 0
  
for (a,b) in f:
range_dict[b] += 1
for b in range_dict.keys():
if range_dict[b] == 0:
return False
  
return True
  
  
def inverse(A, B, f):
if (not isOneToOne(A, B, f)) or (not isOnto(A, B, f)):
return None
  
inv = []
for (a, b) in f:
inv.append([b,a])
return inv
  
A = [1, 2, 3]
B = [4, 5]
f = [[1,4], [2,5], [3,4]]

print isFunction(A, B, f)
print image(f)
print isOneToOne(A, B, f)
print isOnto(A, B, f)
print inverse(A, [4,5,6], [ [1, 4], [2, 5], [3, 6] ])

# link for the case if indentation mess up. https://goo.gl/bbP9Np

need help with python programming problem challenge: use Python lists to represent these. For example, suppose you have a set A = {1, 2, 3}, a set B = {4, 5}, a
need help with python programming problem challenge: use Python lists to represent these. For example, suppose you have a set A = {1, 2, 3}, a set B = {4, 5}, a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site