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

Program 41: Write a function that takes a list as an input and returns the middle element of the list. If the list has even
                 number of elements, then it returns average of both elements.

                 Ans. def findMiddleElement(inputList):
                         '''
                         Objective : To find middle element of the list
                         Input Parameter : inputList - list
                         Return Value : numeric value

                         '''
                         length = len(inputList)
                         if length == 0:
                             return None # Return None for an empty list
                         elif length % 2 == 1: # Odd number of elements
                             middleIndex = length // 2
                             return inputList[middleIndex]
                         else: # Even number of elements
                              middleIndex1 = length // 2 - 1
                              middleIndex2 = length // 2
                              middleElement1 = inputList[middleIndex1]

                              middleElement2 = inputList[middleIndex2]
                              return (middleElement1 + middleElement2) / 2
                     # Example usage:
                     myListOdd = [1, 2, 3, 4, 5]
                     middleResultOdd = findMiddleElement(myListOdd)
                     myListEven = [1, 2, 3, 4]
                     middleResultEven = findMiddleElement(myListEven)
                     if middleResultOdd is not None:

                         print(f"Middle element (odd): {middleResultOdd}")
                     if middleResultEven is not None:
                         print(f"Middle element (even): {middleResultEven}")
                                                             Unsolved


                   1.   Write a program to accept temperature in Fahrenheit and print the temperature in Celsius. Note: Formula for
                     conversion
                      C = (F-32) * 5 / 9
                   2.   Write a program  to accept marks in three subjects and display the percentage of marks. Assume the maximum
                     marks in each subject is 40.
                   3.  Write a program to accept a number and print its square and cube.
                   4.   Write  a  program  that  accepts  the  radius,  r,  height,  h  and  slant  height,  sh  of  a  cone,  and  calculates  its
                     volume and surface area. Save the file as cone.py. Also, execute the program and write the output in the
                     space provided.
                                                   2
                      The volume of a cone = (1 / 3) πr h cubic units
                      Surface area = π * r (r + L)


                                                                                                           Practical  517
   526   527   528   529   530   531   532   533   534   535   536