HackerRank Solution: Python Check Strict Superset [4 Methods]


Hacker Rank Python

Author: Bashir Alam
Reviewer: Deepak Prasad

Question: Check Strict Superset [Python Sets]

You are given a set A and n other sets.
Your job is to find whether set A is a strict superset of each of the N sets.

Print True, if A is a strict superset of each of the N sets. Otherwise, print False.

A strict superset has at least one element that does not exist in its subset.

Example
Set([1, 3, 4]) is a strict superset of set([1, 3]).
Set([1, 3, 4]) is not a strict superset of set([1, 3, 4]).
Set([1, 3, 4]) is not a strict superset of set([1, 3, 5]).

Input Format

The first line contains the space separated elements of set A.
The second line contains integer n, the number of other sets.
The next n lines contains the space separated elements of the other sets.

Constraints

  • 0 < len(set(A)) < 501
  • 0 < N < 21
  • 0 < len(otherSets) < 101

Output Format

Print True if set A is a strict superset of all other N sets. Otherwise, print False.

Sample Input 0

1 2 3 4 5 6 7 8 9 10 11 12 23 45 84 78
2
1 2 3 4 5
100 11 12

Sample Output 0

False

Explanation 0

Set A is the strict superset of the set([1, 2, 3, 4, 5]) but not of the set ([100, 11, 12]) because 100 is not in set A.
Hence, the output is False.

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

 

Possible Solutions

1. Using the issuperset Method

In this solution, we start by reading the elements of the main set and converting them into a set of integers. We also read the number of other sets. For each of these sets, we read the elements and convert them into a set of integers inside Python for loop. We then use the issuperset method to check if the main set is a strict superset of each of the other sets. If the main set is a strict superset of all other sets, we print True; otherwise, we print False. This method uses the issuperset method for superset checking.

# Read main set
main_set = set(map(int, input().split()))

# Read number of other sets
n = int(input())

is_strict_superset = True
for _ in range(n):
    other_set = set(map(int, input().split()))
    if not (main_set > other_set):  # Strict superset check
        is_strict_superset = False
        break

print(is_strict_superset)

 

2. Using All Function and Set Operations

In this solution, we begin by reading the elements of the main set and converting them into a set of integers. We also read the number of other sets. For each of these sets, we read the elements and convert them into a set of integers. We then use a combination of the all function and set operations to check if the main set is a strict superset of each of the other sets. The result is printed as True if the main set is a strict superset and False otherwise. This method uses the all function for comprehensive checking.

# Read main set
main_set = set(map(int, input().split()))

# Read number of other sets
n = int(input())

other_sets = [set(map(int, input().split())) for _ in range(n)]

# Check if main set is a strict superset of all other sets
print(all(main_set > other_set for other_set in other_sets))

 

3. Using For Loop and Subset Checking

In this solution, we start by reading the elements of the main set and converting them into a set of integers. We also read the number of other sets. For each of these sets, we read the elements and convert them into a set of integers. We then use a for loop to manually check if the main set is a strict superset of each of the other sets using the > operator. Based on the result, we print True if the main set is a strict superset of all other sets and False otherwise. This method uses a for loop and the > operator for checking.

# Read main set
main_set = set(map(int, input().split()))

# Read number of other sets
n = int(input())

is_strict_superset = True
for _ in range(n):
    other_set = set(map(int, input().split()))
    if not main_set > other_set:  # Strict superset check
        is_strict_superset = False
        break

print(is_strict_superset)

 

4. Using List Comprehension and All Function

In this solution, we begin by reading the elements of the main set and converting them into a set of integers. We also read the number of other sets. For each of these sets, we read the elements and convert them into a set of integers. We then use list comprehension combined with the all function to check if the main set is a strict superset of each of the other sets. The result is printed as True if the main set is a strict superset and False otherwise. This method uses list comprehension and the all function for concise checking.

# Read main set
main_set = set(map(int, input().split()))

# Read number of other sets
n = int(input())

# Check if main set is a strict superset of all other sets using list comprehension and all function
print(all(main_set > set(map(int, input().split())) for _ in range(n)))

 

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 Strict Superset [4 Methods]

 

Summary

In this tutorial, we explained four possible solutions to solve HackerRank's Python problem on checking if a set is a strict superset of other sets. The first solution uses the issuperset method to directly check the superset relationship. The second solution employs the all function combined with set operations for comprehensive checking. The third solution uses a for loop with the > operator to manually verify the superset relationship. The fourth solution utilizes list comprehension and the all function for concise checking. Each method demonstrates different Python techniques to efficiently solve the problem.

 

Further Reading

Question on Hacker Rank: Python Check Strict Superset [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