Python Set – Practice Program 1

Set is an unordered, mutable, iterable collection datatype that can store heterogeneous elements. Set does not allow to store duplicate elements in it. Set in Python supports mathematical operations like union, intersection, difference, and symmetric difference, subset and super-set.

In previous articles, we learned about various functions and mathematical operations that we can perform on Sets in Python. In this article we will work on practice programs related to Python Sets.

Program – Problem Statement

We will be given a list of sets. List will contain multiple sets and sets will contain number elements. From all the sets given in list, there can be some duplicate sets. We need to find the first duplicate set from all the sets in the list. Output should be a duplicate element set in list elements.

Duplicate sets are those sets which has exactly same number of elements and exactly same elements in them

Test Case 1
Input: input_list = [{4, 9, 6, 1}, {6, 4, 1, 9}, {1, 3, 4, 3}, {1, 4, 3}, {7, 8, 9}]
Output: {6, 4, 1, 9}

Test case explanation:
List with set elements will be given as an input. We will iterate through the list from left index position. First element in list is {4, 9, 6, 1}. We will compare number of elements and value of elements in this set with other sets. {4, 9, 6, 1} will have same number of elements and exactly same elements with another set {6, 4, 1, 9}. Comparison with other sets ({1, 3, 4, 3}, {1, 4, 3}, {7, 8, 9}) will either fail with number of elements not equal or all elements are not present. From this we got the set which has duplicate set in the list. Duplicate set should be {4, 9, 6, 1}.

Program – Solution

Solution Program Steps

  1. List input_list with set elements will be given as an input.
  2. We will iterate through the input_list from left index position (index 0) and will take the first_set. For iteration we can either use for loop OR while loop.
  3. Inside the first iterator, we will have another iterator starting from next index position which will give us second_set.
  4. Now from both the sets (first_set and second_set), first we will check if number of elements in both sets are equal or not.
  5. If number of elements are not equal in both sets, then move to the next index position and take next set first_set.
  6. If number of elements are equal in both sets, then we will check for the difference between these two sets (first_set and second_set). OR we can loop through the elements to compare every element of first_set is present in second_set.
  7. If the difference set is empty, then it means second_set is duplicate of first_set else go for next iteration and take new set as first_set.
  8. Print the value of duplicate set.

Solution Code 1 – for loop and difference() method

input_list = [{14, 9, 16}, {2, 4, 1, 7}, {1, 14, 3}, {16, 14, 9}]
duplicate_set = set()

list_length = len(input_list)
for index in range(list_length):                # iterate through list elements get first_set
    for n_index in range(index + 1, list_length):         # iterate through other element in list, get second_set
        if len(input_list[index]) == len(input_list[n_index]):
            difference_set = input_list[index].difference(input_list[n_index])      # using difference() method
            if len(difference_set) == 0:
                duplicate_set = input_list[index]
                break
    if len(duplicate_set) > 0:                # check duplicate_set is empty or not
        break

print(duplicate_set)

Program Output{16, 9, 14}

Test Cases

Test Case 1
Input: input_list = [{4, 9, 6, 1}, {6, 4, 1, 9}, {1, 3, 4, 3}, {1, 4, 3}, {7, 8, 9}]
Output: {6, 4, 1, 9}

Test Case 2
Input: input_list = [{8, 9, 6, 3}, {6, 4, 10, 19}, {6, 9, 8, 3}, {1, 4, 3}]
Output: {6, 9, 8, 3}

Test Case 3
Input: input_list = [{14, 9, 16}, {2, 4, 1, 7}, {1, 14, 3}, {16, 14, 9}]
Output: {16, 9, 14}

Test Case 4
Input: input_list = [{24, 19, 26}, {12, 14, 41, 17}, {18, 314, 53}, {156, 184, 79}]
Output: set()

Solution Code 2 – while loop and difference() method

input_list = [{8, 9, 6, 3}, {6, 4, 10, 19}, {6, 9, 8, 3}, {1, 4, 3}]
duplicate_set = set()

list_length = len(input_list)
index = 0
while index < list_length:            # iterate through list elements get first_set
    n_index = index + 1
    while n_index < list_length:                # iterate through other element in list, get second_set
        if len(input_list[index]) == len(input_list[n_index]):
            difference_set = input_list[index].difference(input_list[n_index])        # using difference() method
            if len(difference_set) == 0:
                duplicate_set = input_list[index]
                break
        n_index += 1
    if len(duplicate_set) > 0:          # check duplicate_set is empty or not
        break
    index += 1

print(duplicate_set)

Program Output{8, 9, 3, 6}

Solution Code 2 – for loop and in or not in operator

input_list = [{24, 19, 26}, {12, 14, 41, 17}, {18, 314, 53}, {156, 184, 79}]
duplicate_set = set()

list_length = len(input_list)
for index in range(list_length):            # iterate through list elements get first_set
    for n_index in range(index + 1, list_length):         # iterate through other element in list, get second_set
        if len(input_list[index]) == len(input_list[n_index]):
            different_element_count = 0
            for element in input_list[index]:                   # iterate through the elements of set
                if element not in input_list[n_index]:       # compare if element of set  is present in second_set
                    different_element_count += 1
                    break;
            if different_element_count == 0:
                duplicate_set = input_list[index]
                break
    if len(duplicate_set) > 0:            # check duplicate_set is empty or not
        break

print(duplicate_set)

Program Outputset(). It means no duplicate set found.

Summary

In this article, we write a Python program for a problem statement related to Python Set and following points were covered.

Code – Github Repository

All code snippets and programs for this article and for Python tutorial, can be accessed from Github repository – Comments and Docstring in Python.

Python Topics


Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *