Answer to Question #297838 in Python for Soumya

Question #297838

anil is given a sentence as he tries to make a sentence special a sentence can be made special by swapping the character with high frequency with the character having low frequency in the sentence help anil by transforming the sentence into. a special sentence in python.


swap most frequent letter with least given a sentence, swap the occurrences of the most frequenti with the least frequent letter and vice-versa. submisdong note: • consider upper and lower case letters as different . if there are multiple letters with the same frequency, choose the lower case letter that comes earliest in dictionary order o if letters like x and b have same frequency consider x o if letters like x and b have same frequency consider b.


Input: Python is a programming language


Output should be: Python is e progremming lenguega


Input : check your blood pressure frequently


Output : check ybur olbbd pressure frequently


1
Expert's answer
2022-02-14T15:04:19-0500
# Python 3.9.5

def get_most_less_char(test_str):
    all_freq = {}
    for i in test_str:
        if i == ' ':
            continue
        else:
            if i in all_freq:
                all_freq[i] += 1
            else:
                all_freq[i] = 1
    res_max = max(all_freq, key=all_freq.get)
    res_min = min(all_freq, key=all_freq.get)
    index = all_freq[res_min]
    all_freq.pop(res_min)
    if list(all_freq.keys())[list(all_freq.values()).index(index)] and\
        list(all_freq.keys())[list(all_freq.values()).index(index)].islower():
        res_min = list(all_freq.keys())[list(all_freq.values()).index(index)]

    return res_min, res_max

def main():
    strng = input('Enter sentence: ')
    res_min, res_max = get_most_less_char(strng)
    print(res_min, res_max)
    print("Original sentence: " + strng)
    strng = strng.replace(res_max, res_min)
    print("Special sentence: " + strng)

if __name__ == '__main__':
    main()

The examples given in the assignment are not correct. In the first example, since we meet y earlier than e. In the second, r is mentioned 4 times, o 3.


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