HackerRank Solution: Python Check Subset [3 Methods]


Hacker Rank Python

Author: Bashir Alam
Reviewer: Deepak Prasad

Question: Check Subset [Python Sets]

You are given two sets, A and B.
Your job is to find whether set A is a subset of set B.

If set A is subset of set B, print True.
If set A is not a subset of set B, print False.

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 set A.
The second line of each test case contains the space separated elements of set A.
The third line of each test case contains the number of elements in set B.
The fourth line of each test case contains the space separated elements of set B.

 

Constraints

0 < T < 21

0 < Number of elements in each set < 1001

Output Format

Output True or False for 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

Set A = {1 2 3 5 6}
Set B = {9 8 5 6 3 2 1 4 7}
All the elements of set A are elements of set B.
Hence, set A is a subset of set B.

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:

HackerRank Solution: Python Check Subset [3 Methods]

 

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]

 

Bashir Alam

Bashir Alam

He is a Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in Python, Java, Machine Learning, OCR, text extraction, data preprocessing, and predictive models. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment