Answer to Question #298912 in Python for Jyothi

Question #298912

Swap Letters

Input: Python is a programming language

output: Python is e progremming lenguega


1
Expert's answer
2022-02-17T05:21:10-0500
import string


def ferq_letters(line):
    d  = {}
    for ch in line:
        if ch in string.ascii_letters:
            d[ch] = d.get(ch, 0) + 1
    less_freq = len(d) + 1
    more_freq = 0
    for ch in d:
        if d[ch] < less_freq:
            less_freq = d[ch]
            less_ch = ch
        elif d[ch] == less_freq:
            if ch.lower() < less_ch.lower():
                less_ch = ch


        if d[ch] > more_freq:
            more_freq = d[ch]
            more_ch = ch
        elif d[ch] == more_freq:
            if ch.lower() < more_ch.lower():
                more_ch = ch
    return less_ch, more_ch



def swap_letters(line):
    ch1, ch2 = ferq_letters(line)
    res = ''
    for ch in line:
        if ch == ch1:
            res += ch2
        elif ch == ch2:
            res += ch1
        else:
            res += ch
    return res


def main():
    line = input()
    res = swap_letters(line)
    print(res)


if __name__ == '__main__':
    main()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS