I am working on a question that requires to point out the problem in a function that determines whether a dictionary is invertible (for every value appearing in the dictionary, there is only one key that maps to that value) or not. The question is below:
def is_invertible(adict):
inv_dict = make_inv_dict(adict)
return adict == inv_dict
def make_inv_dict(adict):
if len(adict) > 0:
key, val = adict.popitem()
adict = make_inv_dict(adict)
if val not in adict.values():
adict[key] = val
return adict
else:
return {}
Currently, this returns False
for {'a': 'b', 'b': 'e', 'c': 'f'}
when it is supposed to be True
. I am sure that there is an issue in make_inv_dict
function; is it simply because adict
is not an appropriate variable name in adict = make_inv_dict(adict)
? Or is there another reason why the function returns a wrong outcome?