Swap Letters
Input: Python is a programming language
output: Python is e progremming lenguega
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()
Comments
Leave a comment