Check Subset in Python - HackerRank Solution

Check Subset - Python HackerRank Solution

Hello Friends, How are you? Today I am going to solve the HackerRank Check Subset Problem in Python with a very easy explanation. In this article, you will get one or more approaches to solving this problem. So let's start-

{tocify} $title={Table of Contents}

Default Arguments Python HackerRank Solution

HackerRank Python Check Subset Solution - Problem Statement

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 a 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 0

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 {codeBox}

Sample Output 0

True False False {codeBox}

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.

Python Check Subset - Hacker Rank Solution

Approach I: Check Subset HackerRank Python Solution

# ========================
#       Information
# ========================

# Name: Check Subset in Python HackerRank
# Direct Link: https://www.hackerrank.com/challenges/py-check-subset/problem
# Difficulty: Easy
# Max Score: 10
# Language: Pypy 3

# ========================
#         Solution Start
# ========================

#Check Subset in Python - Hacker Rank Solution
   
# 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")
   
#Check Subset in Python - Hacker Rank Solution END
# MyEduWaves


Disclaimer: The above Problem ( Python Check Subset ) is generated by Hackerrank but the Solution is Provided by MyEduWaves. This tutorial is only for Educational and Learning purposes. Authority if any queries regarding this post or website fill out the contact form.

I hope you have understood the solution to this HackerRank Problem. All these three solutions will pass all the test cases. Now visit Python Check Subset Hackerrank Problem and try to solve it again.

All the Best!

Post a Comment

Previous Post Next Post