HackerRank Solution: Python Check Subset [3 Methods]

HackerRank Solution: Python Check Subset [3 Methods]

Question: Check Subset [Python Sets]

You are given two sets,AandB.
Your job is to find whether setAis a subset of setB.

If setAis subset of setB, printTrue.
If setAis not a subset of setB, printFalse.

Input Format

The first line will contain the number of test cases,T.
The first line of each test case contains the number of elements in setA.
The second line of each test case contains the space separated elements of setA.
The third line of each test case contains the number of elements in setB.
The fourth line of each test case contains the space separated elements of setB.

Constraints

0 < T < 21

0 < Number of elements in each set < 1001

Output Format

OutputTrueorFalsefor each test case on separate lines.

Sample Input

3
5
1 2 3 5 6
9
9 8 5 6 3 2 1 4 7
1
2
5
3 6 5 4 1
7
1 2 3 5 6 8 9
3
9 8 2

Sample Output

True 
False
False

Explanation

Test Case 01 Explanation

SetA= {1 2 3 5 6}
SetB= {9 8 5 6 3 2 1 4 7}
All the elements of setAare elements of setB.
Hence, setAis a subset of setB.

If you are new to python then I would recommend reading out Python Sets before attempting this question.


Possible Solutions

1. Using Subset Method

In this solution, we begin by reading the number of test cases. For each test case, we read the number of elements and the elements themselves for two sets using python for loop. We convert these elements into sets of integers. We then use the issubset method to check if the first set is a subset of the second set. Based on the result, we print True if it is a subset and False otherwise. This method uses the issubset method for subset checking.

# Read number of test cases
t = int(input())

for _ in range(t):
    # Read set A
    n = int(input())
    set_a = set(map(int, input().split()))

    # Read set B
    m = int(input())
    set_b = set(map(int, input().split()))

    # Check if set A is a subset of set B
    print(set_a.issubset(set_b))

2. Using Comparison Operators

In this solution, we start by reading the number of test cases. For each test case, we read the number of elements and the elements themselves for two sets, converting them into sets of integers. We then use the <= operator to check if the first set is a subset of the second set. The result is printed as True if the first set is a subset and False otherwise. This method uses the <= operator for subset checking.

# Read number of test cases
t = int(input())

for _ in range(t):
    # Read set A
    n = int(input())
    set_a = set(map(int, input().split()))

    # Read set B
    m = int(input())
    set_b = set(map(int, input().split()))

    # Check if set A is a subset of set B
    print(set_a <= set_b)

3. Using For Loop and All Function

In this solution, we begin by reading the number of test cases. For each test case, we read the number of elements and the elements themselves for two sets, converting them into sets of integers. We then use a for loop combined with the all function to check if all elements of the first set are present in the second set. Based on the result, we print True if the first set is a subset and False otherwise. This method uses a for loop and the all function for subset checking.

# Read number of test cases
t = int(input())

for _ in range(t):
    # Read set A
    n = int(input())
    set_a = set(map(int, input().split()))

    # Read set B
    m = int(input())
    set_b = set(map(int, input().split()))

    # Check if set A is a subset of set B using for loop and all function
    is_subset = all(elem in set_b for elem in set_a)
    print(is_subset)

When we run the code from all three possible solutions on Hacker Rank, the sample Test case is shown as successful:

image


Summary

In this tutorial, we explained three possible solutions to solve HackerRank’s Python problem on checking if one set is a subset of another. The first solution uses the issubset method to directly check the subset relationship. The second solution employs the <= operator for a concise comparison between sets. The third solution uses a for loop combined with the all function to manually verify if all elements of one set are present in another. Each method demonstrates different Python techniques to efficiently solve the problem.


Further Reading

Question on Hacker Rank: Python Check Subset [Sets]