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

return meanLst
                    '''
                    Objective: To count number of elements in a list
                    Input: a list
                    Output: count of elements
                    '''


                    def countElements(lst):
                        count = 0
                        for element in lst:
                            count += 1
                        return count
                    '''
                    Objective: To compute the most frequently occuring element (mode) in a list
                    Input: a list of numbers
                    Output: mode of the elements of list
                    '''

                    def modeList(lst):
                        frequency = {}
                        maxCount = 0
                        mostFrequent = None

                        for element in lst:
                            if element in frequency:
                                frequency[element] += 1
                            else:
                                frequency[element] = 1

                            if frequency[element] > maxCount:
                                maxCount = frequency[element]
                                mostFrequent = element

                        return mostFrequent

                    #Objective: To read numbers representing ages and mean, count, and  mode of all ages.


                    more = 'y'
                    ageList = []


                    while more == 'y' or more == 'Y':
                        age = int(input("Enter the age : "))
                        ageList.append(age)
                        more = input("Do you wish to enter more (y/n): ")

                    print("Average age is: ", meanList(ageList))
                    print("Count of friends: ", countElements(ageList))
                    print("The age value that occurs the most is: ", modeList(ageList))

               400   Touchpad Computer Science (Ver. 2.0)-XI
   409   410   411   412   413   414   415   416   417   418   419