Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are these set operations, and why do they give different results?

I had seen this test question on Pluralsight:

Given these sets:

x = {'a', 'b', 'c', 'd'}
y = {'c', 'e', 'f'}
z = {'a', 'g', 'h', 'i'}

What is the value of x | y ^ z?

The expected answer is:

{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}

Combines the sets (automatically discarding duplicates), and orders them from lowest to greatest.

My questions are:

  • What is this expression called?
  • Why do I get 3 different results from 3 different Python versions?

Result on Python 3.7.5 on Ubuntu 18.04:

{'c', 'h', 'f', 'd', 'b', 'i', 'g', 'a', 'e'}

Result on Python 2.17.17rc1 on Ubuntu 18.04:

set(['a', 'c', 'b', 'e', 'd', 'g', 'f', 'i', 'h'])

Result on Python 3.7.2 on Windows 10:

{'a', 'd', 'h', 'f', 'b', 'g', 'e', 'c', 'i'}

Here is a repl of the same code I'm using for this: https://repl.it/repls/RudeMoralWorkplace

I'd like to understand what happens behind the scenes with these expressions so I can debunk why I get different results.

like image 873
CodeSpent Avatar asked Feb 06 '20 16:02

CodeSpent


People also ask

What are the different operations of a set?

There are four main set operations which include set union, set intersection, set complement, and set difference.

What is set operation explain different set operations with example?

2 Set Operations. The union of two sets is a set containing all elements that are in A or in B (possibly both). For example, {1,2}∪{2,3}={1,2,3}. Thus, we can write x∈(A∪B) if and only if (x∈A) or (x∈B).


1 Answers

The set operations you have mentioned are:

^ - symmetric difference (XOR):

Return a new set with elements in either the set or other but not both.

Example: {'1', '2', '3'} ^ {'2', '3', '4'} = {'1', '4'}

| - union (OR):

Return a new set with elements from the set and all others.

Example: {'1', '2', '3'} | {'2', '3', '4'} = {'1', '2', '3', '4'}

There are also other set operations in python:

& - intersection (AND):

Return a new set with elements common to the set and all others.

Example: {'1', '2', '3'} & {'2', '3', '4'} = {'2', '3'}

- - difference:

Return a new set with elements in the set that are not in the others.

Example: {'1', '2', '3'} - {'2', '3', '4'} = {'1'}

The order of precedence for these operations is -, &, ^, |, so in your example, we first apply ^:

>>> y^z
{'a', 'c', 'e', 'f', 'g', 'h', 'i'}

And then |:

>>> x|{'a', 'c', 'e', 'f', 'g', 'h', 'i'}
{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}

The different outputs you describe are actually the same set, as sets are not ordered.

>>> {'c', 'h', 'f', 'd', 'b', 'i', 'g', 'a', 'e'} == {'a', 'd', 'h', 'f', 'b', 'g', 'e', 'c', 'i'}
True

Any order shown in the string representation of a set is an implementation detail and should not be relied upon as it will vary unpredictably, as you have found.

like image 111
CDJB Avatar answered Oct 25 '22 07:10

CDJB