INFYTQ Coding Mock I - Previous Year

Problem I

Consider a non-empty string Instr containing only letters, numbers, and white spaces. Identity and print a string outstr based on the logic below:
• Form a string by removing all occurrences of the characters in Instr at special Index, if the character at the special index is a non-white space.
• Special index is the absolute index obtained by subtracting the number of occurrences of highest occurring letters (a-z and A-Z) and the number of occurrences of highest occurring digit (0-9) in Instr.
• If the character at the special index is a white space, ignore the removal of character . Replace all occurrences of white spaces in the above formed string with a $(dollar symbol).

Assumption:

1)The input string instr will not contain any leading or trailing spaces.
2)The input string instr will contain atleast two unique characters.

Input Format:

Read instr from standard input stream.

Output Format:

Print the outstr to standard output stream.


Example I:

Input:999 aa 22aa

Output:$aa$22aa

Example II:

Input:Hi there

Output:Hi$there



def solution(instr):
    freqLetter = {}
    freqDigit = {}
    for ch in instr:
        if((ord(ch)>=65 and ord(ch)<= 90) or ( ord(ch)>=97 and ord(ch)<=122)):
            if ch in freqLetter:
                freqLetter[ch]+=1
            elif ch!=' ':
                freqLetter[ch]=1
        elif(ord(ch)>=48 and ord(ch)<=57):
            if ch in freqDigit:
                freqDigit[ch]+=1
            elif ch!=' ':
                freqDigit[ch]=1
    letter,digit = 0,0
    for k,v in freqLetter.items():
        letter = max(letter,v)
    for k,v in freqDigit.items():
        digit = max(digit,v)
    index = abs(letter-digit)
    ch = instr[index]
    res = ""
    if ch!=' ':
        for i in instr:
            if i!=ch and i !=' ':
              res += i
            if i == " ":
              res += "$"
    else:
        for i in instr:
            if i ==' ':
                res += "$"
            else:
                res += i
    return res

instr=input()
solution(instr)