Question
The given code starts by initializing some sets and functions f , g , and h . Block 1 is a function ( called hasInverse
The given code starts by initializing some sets and functions f g and h
Block is a function called hasInverse that takes as input another function called func and two sets D and T The function hasInverse returns true if the function func with domain D and target T has a valid inverse. Follow the instructions in the comments to print additional information when function func does not have a valid inverse.
The next set of lines call the function hasInverse with combinations of function, domain, and target.
You should complete the code in Block for a function called inverse that returns the value of the inverse of the function on the given input. The function inverse tests whether the function func has a valid inverse and whether the input is in the target. For example, a call to inversef BinEven, Bin should return the string
The next set of lines call the function inverse with various inputs.
Read the comments in the code carefully, as they describe how the given code works in detail.
main.py
# Initializing functions and sets
Bin
BinOdd # The number of s is odd
BinEven # The number of s is even
def finput: # Removes the second bit from the string.
out
index
for c in input:
if index :
out c
index
returnout
def ginput: # Adds a to the end of the string
returninput
def hinput: # Adds a to the end of the string if the number of s is even and adds a if the number of s is odd
count input.count
if count :
returninput
else:
returninput
# Block :
# This function takes as input a function func, a domain set D and a target set T
# A boolean value is returned indicating whether the function func has a well defined inverse
#
# Add one line of code to print out which elements in the target set are mapped to by or more than one domain element.
# To make sure your output matches the test cases use the line:
# printThere are strcount inputs from the domain that map to y in the target'
def hasInversefunc D T:
hasInv True
for y in sortedT: # T is sorted to make sure the output matches. The code should work correctly for any order of T
# This loop counts the number of elements from the domain that func maps to y
count # count will store the number of domain elements seen so far that map to y
for x in D:
if funcx y:
count count # An element is found that reaches this particular y Increment the count.
if count : # If there are zero or more than one element that maps to y then the inverse of func does not extist.
hasInv False # the flag hasInv can never be set back to True
printThere are strcount inputs from the domain that map to y in the target'
returnhasInv
# The following lines determine whether functions have a valid inverse for different domains and targets
# If you have written your code correctly in Block these lines should output the correct text
if hasInversef BinOdd, Bin:
printf: BinOdd Bin has an inverse'
else:
printf: BinOdd Bin does not have an inverse'
if hasInversef BinEven, Bin:
printf: BinEven Bin has an inverse'
else:
printf: BinEven Bin does not have an inverse'
if hasInverseg Bin BinEven:
printg: Bin BinEven has an inverse'
else:
printg: Bin BinEven does not have an inverse'
if hasInverseh Bin BinEven:
printh: Bin BinEven has an inverse'
else:
printh: Bin BinEven does not have an inverse'
# Block :
# This function should return the correct value for the inverse of func on the given input
def inversefunc D T input:
if hasInversefunc D T False:
returnERROR: The function does not have an inverse'
if input in T False:
returnERROR: The input is not in the target set'
# Add your code here to return the correct value for the inverse of func on the given input
return
# The following lines compute the inverse of functions on different inputs
# If you have written your code correctly in Block these lines should output the correct text
printThe inverse of f:BinOddBin on input is inversef BinOdd, Bin
printThe inverse of f:BinEvenBin on input is inversef BinEven, Bin
printThe inverse of h:BinBinEven on input is inverseh Bin BinEven,
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started