Python Set – Practice Program 2

In Python, 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 two inputs – a list of sets denoted with input_sets and another set to match denoted with match_set. List will contain multiple sets and sets will contain number elements. From all the sets given in list, we need to find the set whose maximum number of elements matches with the elements in match_set. Output should be the set from list (input_sets) with maximum matching elements.

Test Case 1
Input: input_set = [{1, 2, 3}, {1, 4, 5}, {1, 2, 5, 6}] and match_set = {1, 2, 6}
Output: {1, 2, 5, 6}

Test case explanation:
List with set elements will be given as an input_set = [{1, 2, 3}, {1, 4, 5}, {1, 2, 5, 6}] and another set denoted with match_set = {1, 2, 6}. We will iterate through the list from left index position. First element in list is {1, 2, 3}. We will check for the common number of elements in first set {1, 2, 3} and matching set {1, 2, 6}. Common element count can be calculated by using intersection() of both the sets. We will iterate this process for all the sets in the input_set and will return the output whose set will have the maximum matching elements. Will return empty set if no element matches with any set.

Program – Solution

Solution Program Steps

  1. Two parameters will be given as an input. List with set elements will be given as an input_set and another set to match elements with match_set.
  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. For each set (first_set) we got from iteration, we will perform intersection with match_set. We can either use Set built-in intersection() method OR we can use for loop with in operator to calculate count of common elements.
  4. Use len() method to get the number of elements in the intersection set.
  5. Compare if the number of common elements with previous iteration results.
  6. If current iteration has more common element update the matching_count and the matching_set.
  7. Return or print the matching_set elements.

Solution Code 1 – for loop and intersection() method

input_list = [{1, 2, 3}, {1, 4, 5}, {1, 2, 5, 6}]
match_set = {1, 2, 6}

max_match_count = 0
max_match_count_element = set()
for element in input_list:                    # iterate through list elements get first_set
    intersection_set = element.intersection(match_set)          # calculate intersection between sets
    element_count = len(intersection_set)                 # calculate length of intersection set
    if element_count > max_match_count:                # compare if matching count is more or not
        max_match_count = element_count
        max_match_count_element = element             # update matching set, if matching count is more

print(max_match_count_element)

Program Output{1, 2, 5, 6}

Test Cases

Test Case 1
Input: input_set = [{1, 2, 3}, {1, 4, 5}, {1, 2, 5, 6}] and match_set = {1, 2, 6}
Output: {1, 2, 5, 6}

Test Case 2
Input: input_set = [{21, 32, 12}, {1, 8, 5, 12}, {1, 7, 10, 5}, {11, 47, 70, 95}] and match_set = {1, 5, 12}
Output: {1, 8, 5, 12}

Test Case 3
Input: input_set = [{21, 32, 12}, {1, 8, 5, 12}, {1, 17, 13, 5}] and match_set = {1, 5, 13, 17}
Output: {1, 5, 13, 17}

Test Case 4
Input: input_set = [{121, 312, 132}, {11, 81, 54, 142}, {16, 187, 143, 52}] and match_set = {19, 95, 913, 917}
Output: set()

Solution Code 2 – while loop and intersection() method

input_list = [{21, 32, 12}, {1, 8, 5, 12}, {1, 7, 10, 5}, {11, 47, 70, 95}]
match_set = {1, 5, 12}

max_match_count = 0
max_match_count_element = set()
index = 0
while index < len(input_list):                        # iterate through list elements based on index position
    intersection_set = input_list[index].intersection(match_set)         # calculate intersection between sets
    element_count = len(intersection_set)               # calculate length of intersection set
    if element_count > max_match_count:              # compare if matching count is more or not
        max_match_count = element_count
        max_match_count_element = input_list[index]         # update matching set, if matching count is more
    index += 1

print(max_match_count_element)

Program Output{8, 1, 12, 5}

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

input_list = [{121, 312, 132}, {11, 81, 54, 142}, {16, 187, 143, 52}]
match_set = {19, 95, 913, 917}

max_match_count = 0
max_match_count_element = set()
for element in input_list:                          # iterate through list elements get first_set
    element_count = 0
    for num in element:                              # iterate through set elements
        if num in match_set:                        # using in operator
            element_count += 1
    if element_count > max_match_count:          # compare if matching count is more or not
        max_match_count = element_count
        max_match_count_element = element        # update matching set, if matching count is more

print(max_match_count_element)

Program Outputset(). It means no matching 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 *