HackerRank Solution: Python Check Strict Superset [4 Methods]

HackerRank Solution: Python Check Strict Superset [4 Methods]

Question: Check Strict Superset [Python Sets]

You are given a setAandnother sets.
Your job is to find whether setAis a strict superset of each of theNsets.

PrintTrue, ifAis astrict supersetof each of theNsets. Otherwise, printFalse.

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

Example
Set([1, 3, 4])is astrict supersetof set([1, 3]).
Set([1, 3, 4])is not astrict supersetof set([1, 3, 4]).
Set([1, 3, 4])is not astrict supersetof set([1, 3, 5]).

Input Format

The first line contains the space separated elements of setA.
The second line contains integern, the number of other sets.
The nextnlines contains the space separated elements of the other sets.

Constraints

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

Output Format

PrintTrueif setAis astrict supersetof all otherNsets. Otherwise, printFalse.

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

SetAis thestrict supersetof the set([1, 2, 3, 4, 5])but not of the set ([100, 11, 12])because100is not in setA.
Hence, the output isFalse.

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:

image


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]