Page 364 - Computer Science Class 11 With Functions
P. 364
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))
362 Touchpad Computer Science-XI

