Friday, February 1, 2019

Intersection

  • Problem Description

    Write a program to find the intersection of two lists
  • CODING ARENA
  • def intersection(a, b):
        return list(set(a) & set(b))
    def main():
        alist=[]
        blist=[]
        n1=int(input())
        n2=int(input())
        for x in range(0,n1):
            element=int(input())
            alist.append(element)
        for x in range(0,n2):
            element=int(input())
            blist.append(element)
        ans=[]
        for x in alist:
            for y in blist:
                if(x==y):
                    ans.append(x)
        print("The intersection is")
        print(ans)
    main()
  • Test Case 1

    Input (stdin)
    5
    
    5
    
    12
    
    13
    
    14
    
    15
    
    16
    
    11
    
    12
    
    10
    
    14
    
    16
    
    
    Expected Output
    The intersection is
    
    [12, 14, 16]
  • Test Case 2

    Input (stdin)
    5
    
    5
    
    12
    
    13
    
    11
    
    15
    
    20
    
    11
    
    10
    
    10
    
    15
    
    16
    
    
    Expected Output
    The intersection is
    
    [11, 15]

No comments:

Post a Comment