Answer to Question #283326 in Python for Prathyusha

Question #283326

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

1
Expert's answer
2021-12-29T07:57:35-0500
# Python program for the above approach
from collections import Counter


# Python program to print words
# which occurs k times
def printWords(sentence, k):


	# splitting the string
	lis = list(sentence.split(" "))


	# Calculating frequency of every word
	frequency = Counter(lis)


	# Traversing the frequency
	for i in frequency:


		# checking if frequency is k


		if(frequency[i] == k):
		
			# print the word
			print(i, end=" ")




# Driver code
# Given string
sentence = "sky is blue and my favourite color is blue"


# Given value of K
K = 2


printWords(sentence, K)


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