Friday, February 1, 2019

Compare Dictionary

  • Problem Description

    Write a program to get the input of key and value of the element in dictionary.

    Display the key-value pair of the dictionary in the python dictionary format.

    Compare the dictionary in the following sequence:

    1. First and Second Dictionary
    2. Second and Third Dictionary
    3.Third and Fourth Dictionary
    4.Fourth and First Dictionary

    Refer sample input and output for formatting specification. 

    All float values are displayed correct to 2 decimal places. 

    All text in bold corresponds to input and the rest corresponds to output.
  • CODING ARENA
  • d1={}
    k1=int(input())
    v1=int(input())
    d1.update({k1:v1})
    print("First Dictionary")
    print (d1)
    d2={}
    k2=int(input())
    v2=int(input())
    d2.update({k2:v2})
    print("Second Dictionary")
    print (d2)
    d3={}
    k3=int(input())
    v3=int(input())
    d3.update({k3:v3})
    print("Third Dictionary")
    print (d3)
    d4={}
    k4=int(input())
    v4=int(input())
    d4.update({k4:v4})
    print("Fourth Dictionary")
    print (d4)
    l1=list(d1.values())
    #print(l1)
    l2=list(d2.values())
    l3=list(d3.values())
    l4=list(d4.values())
    def cmp(a, b):
        flag=0
        for i in range(len(a)):
            if a[i]==b[i]:
                flag=1
            elif(a[i]>b[i]):
                print(1)
                break
            else:
                print(-1)
                break
        if flag==1:
            print(0)

    cmp(l1,l2)
    cmp(l2,l3)
    cmp(l3,l4)
    cmp(l4,l1)
  • Test Case 1

    Input (stdin)
    23
    
    33
    
    43
    
    53
    
    32
    
    33
    
    34
    
    35
    
    
    Expected Output
    First Dictionary
    
    {23: 33}
    
    Second Dictionary
    
    {43: 53}
    
    Third Dictionary
    
    {32: 33}
    
    Fourth Dictionary
    
    {34: 35}
    
    -1
    
    1
    
    -1
    
    1
  • Test Case 2

    Input (stdin)
    53
    
    93
    
    33
    
    43
    
    2
    
    10
    
    3
    
    35
    
    
    Expected Output
    First Dictionary
    
    {53: 93}
    
    Second Dictionary
    
    {33: 43}
    
    Third Dictionary
    
    {2: 10}
    
    Fourth Dictionary
    
    {3: 35}
    
    1
    
    1
    
    -1
    
    -1

No comments:

Post a Comment