Check Subset in Python | HackerRank Solution

Hello coders, today we are going to solve Check Subset HackerRank Solution in Python.

Check Subset in Python

Contents

Task

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.

Solution – Check Subset in Python

# Enter your code here. Read input from STDIN. Print output to STDOUT
for i in range(int(input())):
    a = int(input())
    set_a = set(map(int, input().split()))

    b = int(input())
    set_b = set(map(int, input().split()))

    if len(set_a - set_b) == 0:
        print("True")
    else:
        print("False")

Disclaimer: The above Problem (Check Subset) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

2 thoughts on “Check Subset in Python | HackerRank Solution”

  1. t=int(input())
    for i in range(t):
    a=int(input())
    a1=set(map(int,input().split()))
    b=int(input())
    b1=set(map(int,input().split()))
    if (a1.issubset(b1)):
    print(“True”)
    else:
    print(“False”)

Leave a Comment

Your email address will not be published.