Ask Python Expert

Coding examples

Following are some  attempts at defining a function isSubset,  which  takes  two  arguments, a and  b, and  returns True if a is a subset  of b, assuming that  a and  b are represented as lists of elements.

Here is one answer to the problem which  is in the Pythonic functional style.

 

def isSubset(a, b):

return reduce(operator.and_, [x in b for x in a])

 

This is short  and  direct.   The expression x in b tests  to see if the  item  x is in the  structure b.  So, [x in b for x in a] is a list of Booleans,  one for each components in a, indicating whether that component is in b. Finally,  we replace that  list using  the and operator11 , which  will have  the value True if all of the elements of the list are False, and True otherwise.

An alternative is to do it recursively:

 

def isSubset(a, b):

if a == []:

return True

else:

return a[0] in b and isSubset(a[1:], b)

 

The base case of the recursion is that a is the empty list; in that type, it's define that a is a subset  of b. Otherwise, we may give  our answer in terms  of isSubset,  but asking  a simpler question.  So, we call that a is a subset  of b if the ?rst element of a is a member of b and the set of rest of the elements of a is a subset  of b.

 

We can go even farther in the recursive solution for compressing:

def isSubset(a, b):

return a == None or a[0] in b and isSubset(a[1:], b)

 

Here, we are taking  advantage of the fact that in Python (and most other languages), the or Boolean operator has the "early  out"  charters tics. That is, if we are calculating e1 or e2, we start  by evaluating e1, and  if it is True,  then  we know  the  output  of the  expression has  to be True,  and  therefore we give without calculating e2.  So, or can perform as a kind  of conditional operator. Some person would search this program too abstruse (shorter isn't always better),  and  some  would take  it very beautiful.

 

Here is another good solution, this time in the imperative style:

 

def isSubset(a, b):

for x in a:

if not x in b:

return False return True

 

It works  by going through the component in a, in order.  If any of those components is not in b, then we can immediately quit and give False. Otherwise, if we have taken through all of the components in a, and each of them  has been in b, then a really is a subset  of b and we may give True.

Here is another imperative program:

 

def isSubset(a, b): result = True for x in a:

result = result and x in b return result

 

This procedure starts  by initializing a result  to True,  which  is the  check component for and (it plays  the same  role as 0 for add).  Then  it tends all the way  through the structure a, and  ands the last result  with  x in b. This calculation is as the computation done  in the functional version, but rather than  preparing the entire  list of Booleans  and  then  replacing, we inter leave the individual membership tests with combining them  into a result.

 

Python, Programming

  • Category:- Python
  • Reference No.:- M9506011

Have any Question?


Related Questions in Python

Part i the assignment filesone of the most important

Part I: The Assignment Files One of the most important outcomes of this assignment is that you understand the importance of testing. This assignment will follow an iterative development cycle. That means you will write a ...

Homework -this homework will have both a short written and

Homework - This homework will have, both a short written and coding assignment. The problems that are supposed to be written are clearly marked. 1) (Written) Make heuristics Describe two heuristics for the slide problem ...

Tasksdemonstrate data scraping of a social network of

Tasks Demonstrate data scraping of a social network of choice. Develop technical documentation, including the development of the code & detailing the results. Provide a report on the findings, that includes research into ...

Assignment1 utilising python 3 build the following

Assignment 1. Utilising Python 3 Build the following regression models: - Decision Tree - Gradient Boosted Tree - Linear regression 2. Select a dataset (other than the example dataset given in section 3) and apply the De ...

Python programming assignment -you first need an abstract

Python Programming Assignment - You first need an abstract base class, called, Account which has the following attributes and methods: accountID: This attribute holds the ID assigned the account , if not provided set to ...

Learning outcomes lo3 - research develop and document a

Learning Outcomes LO3 - Research, develop, and document a basic security policy, and analyse, record, and resolve all security incidents LO4 - Identify and assess the threats to, and vulnerabilities of networks Assessmen ...

Question research pythons dictionary data type dictdiscuss

Question : Research Python's dictionary data type (dict). Discuss its interface and usage. Include examples. Discuss practical applications of dictionaries.

Questionwhat is a python development frameworkgive 3

Question What is a python development framework? Give 3 examples python development framework used today. and explain which development framework is used in which industry.

Below zero - ice cream storethe local ice-cream store needs

Below Zero - ice cream store The local ice-cream store needs a new ordering system to improve customer service by streamlining the ordering process. The manager of the store has found that many orders are incorrect and s ...

The second task in this assignment is to create a python

The second task in this assignment is to create a Python program called pancakes.py that will determine the final order of a stack of pancakes after a series of flips.(PYTHON 3) Problem Task In this problem, your input w ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As