# The given code starts by initializing some sets and functions f , g , and h .

## Question:

The given code starts by initializing some sets and functions f$,$ g$,$ and h$.$

Block $1$ 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 $2$ 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 inverse$($f$,$ Bin$4$Even, Bin$3,\text{}\text{'}011\text{'})$ should return the string $\text{'}0011\text{'}.$

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$3\text{}=\text{}\{\text{'}000\text{'},\text{}\text{'}001\text{'},\text{}\text{'}010\text{'},\text{}\text{'}011\text{'},\text{}\text{'}100\text{'},\text{}\text{'}101\text{'},\text{}\text{'}110\text{'},\text{}\text{'}111\text{'}\}$

Bin$4$Odd $=\text{}\{\text{'}0001\text{'},\text{}\text{'}0010\text{'},\text{}\text{'}0100\text{'},\text{}\text{'}0111\text{'},\text{}\text{'}1000\text{'},\text{}\text{'}1011\text{'},\text{}\text{'}1101\text{'},\text{}\text{'}1110\text{'}\}$ # The number of $1\text{'}$s is odd

Bin$4$Even $=\text{}\{\text{'}0000\text{'},\text{}\text{'}0011\text{'},\text{}\text{'}0101\text{'},\text{}\text{'}0110\text{'},\text{}\text{'}1001\text{'},\text{}\text{'}1010\text{'},\text{}\text{'}1100\text{'},\text{}\text{'}1111\text{'}\}$ # The number of $1\text{'}$s is even

def f$($input$)$: # Removes the second bit from the string.

out $=\text{}\text{'}\text{'}$

index $=\text{}0$

for c in input:

if $($index $!=\text{}1)$:

out $+=$ c

index $+=\text{}1$

return$($out$)$

def g$($input$)$: # Adds a $0$ to the end of the string

return$($input $+\text{}\text{'}0\text{'})$

def h$($input$)$: # Adds a $0$ to the end of the string if the number of $1\text{'}$s is even and adds a $1$ if the number of $1\text{'}$s is odd

count $=$ input.count$(\text{'}1\text{'})$

if $($count $\%\text{}2\text{}==\text{}0)$:

return$($input $+\text{}\text{'}0\text{'})$

else:

return$($input $+\text{}\text{'}1\text{'})$

# Block $1$:

# 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 $0$ or more than one domain element.

# To make sure your output matches the test cases use the line:

# print$(\text{'}$There are $\text{'}\text{}+$ str$($count$)\text{}+\text{}\text{'}$ inputs from the domain that map to $\text{'}\text{}+$ y $+\text{}\text{'}$ in the target'$)$

def hasInverse$($func$,$ D$,$ T$)$:

hasInv $=$ True

for y in sorted$($T$)$: # 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 $=\text{}0$ # count will store the number of domain elements seen so far that map to y$.$

for x in D:

if $($func$($x$)\text{}==$ y$)$:

count $=$ count $+\text{}1$ # An element is found that reaches this particular y$.$ Increment the count.

if $($count $!=\text{}1)$: # 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

print$(\text{'}$There are $\text{'}\text{}+$ str$($count$)\text{}+\text{}\text{'}$ inputs from the domain that map to $\text{'}\text{}+$ y $+\text{}\text{'}$ in the target'$)$

return$($hasInv$)$

# The following lines determine whether functions have a valid inverse for different domains and targets

# If you have written your code correctly in Block $1,$ these lines should output the correct text

if $($hasInverse$($f$,$ Bin$4$Odd, Bin$3))$:

print$(\text{'}$f: Bin$4$Odd $->$ Bin$3$ has an inverse'$)$

else:

print$(\text{'}$f: Bin$4$Odd $->$ Bin$3$ does not have an inverse'$)$

print$(\text{'}\text{}\text{'})$

if $($hasInverse$($f$,$ Bin$4$Even, Bin$3))$:

print$(\text{'}$f: Bin$4$Even $->$ Bin$3$ has an inverse'$)$

else:

print$(\text{'}$f: Bin$4$Even $->$ Bin$3$ does not have an inverse'$)$

print$(\text{'}\text{}\text{'})$

if $($hasInverse$($g$,$ Bin$3,$ Bin$4$Even$))$:

print$(\text{'}$g: Bin$3\text{}-$ Bin$4$Even has an inverse'$)$

else:

print$(\text{'}$g: Bin$3\text{}-$ Bin$4$Even does not have an inverse'$)$

print$(\text{'}\text{}\text{'})$

if $($hasInverse$($h$,$ Bin$3,$ Bin$4$Even$))$:

print$(\text{'}$h: Bin$3\text{}-$ Bin$4$Even has an inverse'$)$

else:

print$(\text{'}$h: Bin$3\text{}-$ Bin$4$Even does not have an inverse'$)$

print$(\text{'}\text{}\text{'})$

# Block $2$:

# This function should return the correct value for the inverse of func on the given input

def inverse$($func$,$ D$,$ T$,$ input$)$:

if $($hasInverse$($func$,$ D$,$ T$)\text{}==$ False$)$:

return$(\text{'}$ERROR: The function does not have an inverse'$)$

if $(($input in T$)\text{}==$ False$)$:

return$(\text{'}$ERROR: 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$(-1)$

# The following lines compute the inverse of functions on different inputs

# If you have written your code correctly in Block $2,$ these lines should output the correct text

print$(\text{'}$The inverse of f:Bin$4$Odd$->$Bin$3$ on input $011$ is $\text{'}\text{}+$ inverse$($f$,$ Bin$4$Odd, Bin$3,\text{}\text{'}011\text{'}))$

print$(\text{'}\text{}\text{'})$

print$(\text{'}$The inverse of f:Bin$4$Even$->$Bin$3$ on input $001$ is $\text{'}\text{}+$ inverse$($f$,$ Bin$4$Even, Bin$3,\text{}\text{'}001\text{'}))$

print$(\text{'}\text{}\text{'})$

print$(\text{'}$The inverse of h:Bin$3->$Bin$4$Even on input $1001$ is $\text{'}\text{}+$ inverse$($h$,$ Bin$3,$ Bin$4$Even, $\text{'}1001\text{'}))$

**Related Book For**

## Income Tax Fundamentals 2013

ISBN: 9781285586618

31st Edition

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill