Join Regular Classroom : Visit ClassroomTech

Wipro Coding Question Solve | list dictionary tuple count | Codewindow.in

Question 1

How to Attempt?

Write a logic inside the given function, to do the following task on a given input
string.
Find the number of Lists, Dictionaries and Tuples that occurs in the input string
and return the count in the order of List, Dictionary and Tuple (LDT). That is,
“L”+listcount+”D”+dictionarycount+T+tuplecount.

For example,
return “L2D413”, like that.

1. Lists are identified with the square bracket : []
2. Tuples are identified with the parenthesis: ()
3. Dictionaries are identified with the curly brackets:{}
Note:
1) If there is no Tuple, no List and no Dictionaries are available in the given string,
then return “False”.
2) All the opening tokens will have a matching, closing token. That is, All (.[.{ will
have a corresponding ).].}.
3) Input and Output are case sensitive.
4) No need to read input from keyboard. Input will be supplied as argument to the
function. Returning the final answer at the end is essential.
Function Prototype:
def CountList DictTuple(Input1):
write your code

 

Sample input-output 1:
Input1: “{[0])”
Expected ouput: “L1D1T1”

Explanation:
There is one open and close square braces available in the given input string then
the List Count is 1.
There is one open and close curly braces available in the given input string then
the DictCount is 1.
There is one open and close parenthesis available in the given input string then
the TupleCount is 1.
Putting all the counts together, return the string value “L1D1T1” as the final
answer.

 

Sample input-output 2:
Input: “wipro123”
Expected output: “False:
Explanation:
There is no open and close square braces available in the given input string.
There is no open and dose curly braces available in the given input string.
There is no open and dose parenthesis available in the given input string.
So, return the string value “False” as the final answer.


Sample input-output 3:
Input: “[(Hi”,”all”),[“we”, “are”,”from”]{a: BDC’,b:’CDC’}]”
Expected ouput: “L2D1T1

Explanation:
There are two open and close square braces available in the given input string
then the ListCount is 2.
There is one open and close curly braces available in the given input string then
the DictCount is 1.
There is one open and close parenthesis available in the given input string then
the TupleCount is 1.
Putting all the counts together, return the string value “L2D1T1” as the final
answer.

Solution:
Python 3.7:

# Solution by codewindow.in

input1 = input()

TS = input1.count('(')
TE = input1.count(')')
if TS ==TE:
    T=TS
LS = input1.count('[')
LE = input1.count(']')
if LS ==LE:
    L=LS
DS = input1.count('{')
DE = input1.count('}')
if DS ==DE:
    D=DS
    
if L==0 and T==0 and D==0:
    print("False")
else:
    res="L"+str(L)+"D"+str(D)+"T"+str(T)
    print(res)

Also Checkout

Recent Posts
Pages