Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need some help to fix my Python Codes: My codes: def inverse(f): my_inverted_dict = dict() for key, value in inverse.items(): my_inverted_dict.setdefault(value, list()).append(key) Error: AttributeError: 'function'

Need some help to fix my Python Codes:

image text in transcribed

My codes:

def inverse(f): my_inverted_dict = dict() for key, value in inverse.items(): my_inverted_dict.setdefault(value, list()).append(key)

Error: AttributeError: 'function' object has no attribute 'items'

Problem 3: Compute the "inverse" of a dictionary A dictionary f can be thought of a mapping from keys to values, that is, as a one-to-many partial function f:KHV from a set K of keys to a set V of values. The domain Dom(s) CK of f is the set of keys of the dictionary f. The function f has an inverse iff (if and only if) it is one-to-one, that is, if every element of K is mapped to at most one element of V. The inverse function f- of f is defined by f-'(y) = x for every x such that x e Dom(f) and y = f(x). Write a function inverse that takes as input a dictionary f, and returns the inverse of f. If f is not one-to-one, you should raise the valueError exception. Hints: You may want to accumulate the values (the elements to which keys are mapped) into a set, to check whether f is one-to-one or not. You can raise the valueError exception via raise ValueError(). To form the result dictionary, you need all values to be hashable. Do not worry about this; Python will raise an exception if a type is not hashable

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Current Trends In Database Technology Edbt 2006 Edbt 2006 Workshops Phd Datax Iidb Iiha Icsnw Qlqp Pim Parma And Reactivity On The Web Munich Germany March 2006 Revised Selected Papers Lncs 4254

Authors: Torsten Grust ,Hagen Hopfner ,Arantza Illarramendi ,Stefan Jablonski ,Marco Mesiti ,Sascha Muller ,Paula-Lavinia Patranjan ,Kai-Uwe Sattler ,Myra Spiliopoulou ,Jef Wijsen

2006th Edition

3540467882, 978-3540467885

More Books

Students also viewed these Databases questions

Question

What are some characteristics of the Social Age?

Answered: 1 week ago