182k views
0 votes
We wish to explore the differences between sets and bags, and in particular the effect of operators on these ADTs.

In Python the main operators that can be used on sets and bags are as follows (Y and N indicate whether a given operator is available for that ADT).

Operator Python sets bags
Intersection & Y Y
Union | Y Y
Difference - Y Y
Sum + N Y
Where an operator can be used for both sets and bags the effect may be slightly different in some cases. The Sum operator for bags in Python, implemented using the Counter class, adds the counts of corresponding elements in the two bags. See here for details and examples.

The following code defines a simple test function and several sets whose members are individual characters, derived from character strings.

Run this code to set up the test function and initialise the sets.

def one_test(name, actual, expected, details:bool=False) -> None:
"""Report if test passed or failed.
Final argument is optional- if True, then details of failed test are displayed
"""
if actual == expected:
print(name, 'OK')
else:
print(name, 'FAILED')
if (details):
print(' got', actual, '\\ instead of', expected)

# Examples of using the one_test function, with and without details.
# If the "details" argument is True then a FAILED test gives information
# about the expected and actual results of the test.
one_test('1 and 1 makes 2', 1 + 1, 2)
one_test('1 and 1 makes 2', 1 + 1, 2, details = True)
one_test('2 and 2 makes 4', 2 + 2, 5)
one_test('2 and 2 makes 4', 2 + 2, 5, details = True)


PARLIAMENT = 'parliament'
ARIAN = 'arian'
PARLIAMENTARIAN ='parliamentarian'

set_P = set(PARLIAMENT)
set_A = set(ARIAN)
set_PA = set(PARLIAMENTARIAN)

Q1(a)(i) (1 mark)

Run the following test to explore use of set operators.

one_test('set_PA - set_P = set_A : ', set_PA - set_P, set_A)

Given the way these sets were defined, you might have expected this test to pass. Explain briefly below why it fails. We want a general explanation, not a list of the elements of each set, for example.

Q1(a)(ii) (2 marks)

The tests below will also fail (run them to confirm this). Edit the code below to change one operator in each test so that the tests pass. You should also change the operator in the test title (the first argument of the one_test function so that it matches your change to the test).

one_test('set_P - set_A = set_PA: ', set_P - set_A, set_PA)
one_test('set_PA - set_A = set_A : ', set_PA - set_A, set_A)

Explain below why each of your changed tests works. We want a general explanation, not a list of the elements of each set.

1 Answer

4 votes

Final answer:

The difference operator '-' removes elements from the first set that are in the second, which may not match a third set. In Python sets, the corrected tests utilize the union operator '|' to form a complete set without duplicates, and the intersection operator '&' to find common elements between two sets.

Step-by-step explanation:

When we work with sets in Python, the difference operator '-' subtracts elements from one set that are found in another set. However, since sets don’t have duplicate elements, when you perform set_PA - set_P, you remove all the characters found in set_P from set_PA, resulting in a set that does not necessarily equal set_A, because set_A could contain characters that are still present in set_PA after the removal. To correct the provided test cases, we need to substitute the incorrect set difference operator with the correct set operator that achieves the intended result.

Corrected Tests

  • To make set_P - set_A = set_PA pass, we can change the operator to the union operator ('|') as in set_P | set_A = set_PA. The union operator combines all the elements from both sets without duplicates, which correctly forms set_PA when combining set_P and set_A.

  • For the test set_PA - set_A = set_A, changing to the intersection operator ('&') as in set_PA & set_A = set_A will make it pass. The intersection operator returns only the elements common to both sets, which, in this case, should match set_A as all elements of set_A are within set_PA.

User Sfzhang
by
7.2k points