suamy
contestada

Python:
If the condition only has the function "subsets (s, r)" as shown below.

def subsets(s, r):

How can I get a given number(r) of subsets without using itertools or itertools.combinations.



The test code:
print(list(subsets(set(range(4)),3)))

The test result should be:
(0, 1, 2),(0, 1, 3), (0, 2, 3), (1, 2, 3)

Respuesta :

Answer:

# Python Program to Print  

# all subsets of given size of a set  

 

import itertools  

 

def findsubsets(s, n):  

   return list(itertools.combinations(s, n))  

 

# Driver Code  

s = {1, 2, 3}  

n = 2

 

print(findsubsets(s, n))

-----------------------------------------------

# Python Program to Print  

# all subsets of given size of a set  

 

import itertools  

# def findsubsets(s, n):  

def findsubsets(s, n):  

   return [set(i) for i in itertools.combinations(s, n)]  

     

# Driver Code  

s = {1, 2, 3, 4}  

n = 3

 

print(findsubsets(s, n))

-------------------------------------------------------------

# Python Program to Print  

# all subsets of given size of a set  

 

import itertools  

from itertools import combinations, chain  

 

def findsubsets(s, n):  

   return list(map(set, itertools.combinations(s, n)))  

     

# Driver Code  

s = {1, 2, 3}  

n = 2

 

print(findsubsets(s, n))