Python - Create Generalized Set Functions using variable argument passing

Description

Create Generalized Set Functions using variable argument passing

Demo

def intersect(*args): 
    res = [] #   ww  w  .  j  ava2s .c om
    for x in args[0]:                  # Scan first sequence 
        if x in res: continue          # Skip duplicates 
        for other in args[1:]:         # For all other args 
            if x not in other: break   # Item in each one? 
        else:                          # No: break out of loop 
            res.append(x)              # Yes: add items to end 
    return res 

def union(*args): 
    res = [] 
    for seq in args:                   # For all args 
        for x in seq:                  # For all nodes 
            if not x in res: 
                res.append(x)          # Add new items to result 
    return res 

s1, s2, s3 = "TEST", "SCAM", "SLAM" 

d=intersect(s1, s2), union(s1, s2)           # Two operands 
print( d )

d=intersect([1, 2, 3], (1, 4))               # Mixed types 
print( d )

intersect(s1, s2, s3)                      # Three operands 
print( d )

d=union(s1, s2, s3) 
print( d )

d=intersect([1, 2, 1, 3], (1, 1, 4)) 
print( d )

d=union([1, 2, 1, 3], (1, 1, 4)) 
print( d )

Result

Related Topic