ABOUT ME

My photo
Rajahmundry, AndhraPradesh, India

Wipro NLTH Previous Coding Questions

Question No 1: Check For Balanced Parenthesis.

Examples:

Input : {[]{()}}
Output : Balanced

Input : [{}{}(]
Output : Unbalanced
    inp=input()
    l=list()
    par_list={"{":"}","(":")","[":"]","}":"{",")":"(","]":"["}
    open_list=["{","(","["]
    closed_list=["}",")","]"]
    i=0
    while i<len(inp):
        if inp[i] in open_list:
            l.append(inp[i])
            i+=1
        elif inp[i] in closed_list:
            if par_list[inp[i]]==l[-1]:
                l.pop()
                i+=1
            else:
                break
    if len(l)==0:
        print("BALANCED PARANTHESIS")
    else:
        print("UNBALANCED PARANTHESIS") 

Question No 2: Print a given matrix in spiral form.

Given a 2D array, print it in spiral form. See the following examples.
Input:
        1    2   3   4
        5    6   7   8
        9   10  11  12
        13  14  15  16 
Output: 
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 
Input:
        1   2   3   4  5   6
        7   8   9  10  11  12
        13  14  15 16  17  18 
Output: 
1 2 3 4 5 6 12 18 17 16 15 14 13 7 8 9 10 11


def spiralPrint(m,n,a): 
    k=0;l=0
#k - starting row index 
#m - ending row index 
#l - starting column index 
#n - ending column index 
#i - iterator
    while (k<m and l<n): 
    # Print the first row from 
    # the remaining rows 
        for i in range(l,n) : 
            print(a[k][i],end=" ") 
        k+=1
        # Print the last column from 
        # the remaining columns 
        for i in range(k,m) : 
            print(a[i][n-1],end = " ") 
        n-=1
        # Print the last row from 
        # the remaining rows 
        if (k<m) : 
            for i in range(n-1,(l-1),-1) : 
                print(a[m-1][i],end=" ")
        m-=1

        # Print the first column from 
        # the remaining columns 
        if (l<n) : 
            for i in range(m-1,k-1,-1) : 
                print(a[i][l],end=" ")
        l+=1

# Driver Code 
a = [ [1, 2, 3, 4, 5, 6], 
[7, 8, 9, 10, 11, 12], 
[13, 14, 15, 16, 17, 18] ] 

R = 3; C = 6
spiralPrint(R, C, a)

    


No comments:

Post a Comment