Friday, November 15, 2019

Check Subset - Hacker Rank Solution

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


If set  is subset of set , print True.
If set  is not a subset of set , print False.
Input Format
The first line will contain the number of test cases, .
The first line of each test case contains the number of elements in set .
The second line of each test case contains the space separated elements of set .
The third line of each test case contains the number of elements in set .
The fourth line of each test case contains the space separated elements of set .
Constraints
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  = {1 2 3 5 6}
Set  = {9 8 5 6 3 2 1 4 7}
All the elements of set  are elements of set .
Hence, set  is a subset of set .

Check Subset - Hacker Rank Solution



.issubset() or <=

These tools return whether the set is a subset of the set of elements in an iterable.

For example:
print set([2, 9, 7, 1]).issubset(set([1, 7])) #Output: False
print set([2, 9, 7, 1]).issubset(set([1, 2, 3, 4, 5, 6, 7, 8, 9])) #Output: True
Complexity analysis of issubset() can be found here:
http://stackoverflow.com/questions/27674289/the-complextiy-of-python-issubset
Problem Setter's code:
for i in range(int(raw_input())): 
    a = int(raw_input()); A = set(raw_input().split()); 
    b = int(raw_input()); B = set(raw_input().split());
    print A <= B

No comments:

Post a Comment

Powered by Blogger.