Page 529 - Computer Science V2.0 Class 11
P. 529

string = input("Enter a string:")

                     num = int(input("Enter N:"))
                     output = repeatUniqueVowels(string, num)
                     print(output)
                 Program 38: Write a program that accepts a string and displays the count of the number of words in the string.


                 Ans. def countWords(string):
                         '''
                         Objective : To count number of words in a string
                         Input Parameter : string

                         Return Value : len(words) – numeric value
                         '''
                         words = string.split()
                         return len(words)
                     string = input("Enter a string: ")
                     wordCount = countWords(string)

                     print(f"Number of words in the string: {wordCount}")
                 Program 39: Write a program that accepts a list of n numbers and displays the number that appears the maximum
                 number of times.

                 Ans. def findMostFrequent(numbers):
                         '''

                         Objective : To find the number that appears the maximum number of times
                         Input Parameter : numbers - list
                         Return Value : mostFrequentNumbers - list
                         '''
                         countDict = {}

                         for num in numbers:
                             if num in countDict:
                                 countDict[num] += 1
                             else:
                                 countDict[num] = 1

                         maxCount = max(countDict.values())
                          mostFrequentNumbers = [num for num, count in countDict.items() if count ==
                           maxCount]

                         return mostFrequentNumbers


                     n = int(input("Enter the number of elements: "))
                     numbers = []

                     for i in range(n):


                                                                                                           Practical  515
   524   525   526   527   528   529   530   531   532   533   534