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

inputList = []
                    for i in range(n):
                        num = int(input(f"Enter integer {i + 1}: "))
                        inputList.append(num)

                    resultMedian = calculateMedian(inputList)

                    print("\nOriginal List:", inputList)
                    print("Median:", resultMedian)
                 6.  Write a program to read a list of elements. Modify this list so that it does not contain any duplicate elements, i.e., all elements occurring
                    multiple times in the list should appear only once.
              Ans. def removeDuplicates(lst1):
                        lst2 = []
                        for element in lst1:
                            if element not in lst2:
                                #append  it to lst2
                                lst2.append(element)
                        return lst2

                    myList = eval(input('Enter the list:\n'))
                    result = removeDuplicates(myList)
                    print('Original List:' , myList)
                    print('List with no duplicates:' , result)
                 7.  Write a program to read a list of elements. Input an element from the user that has to be inserted in the list. Also input the position at
                    which it is to be inserted. Write a user defined function to insert the element at the desired position in the list.
               Ans.  def insertElementAtPosition(inputList, element, position):
                        inputList.insert(position, element)
                        return inputList

                    n = int(input("Enter the number of elements in the list: "))

                    inputList = []
                    for i in range(n):
                        element = int(input(f"Enter element {i + 1}: "))
                        inputList.append(element)

                    print("\nOriginal List:", inputList)

                    elementToInsert = int(input("Enter the element to insert: "))
                    positionToInsert = int(input("Enter the position to insert: "))

                    modifiedList = insertElementAtPosition(inputList, elementToInsert, positionToInsert)

                    print("\nList after insertion:", modifiedList)
                 8.  Write a program to read elements of a list.
                    a.  The program should ask for the position of the element to be deleted from the list. Write a function to delete the element at the
                      desired position in the list.
                    b.  The program should ask for the value of the element to be deleted from the list. Write a function to delete the element of this value
                      from the list.
               Ans.  def deleteElementByPosition(myList, position):
                        if 1 <= position <= len(myList):
                            del myList[position - 1]

               434   Touchpad Computer Science (Ver. 2.0)-XI
   443   444   445   446   447   448   449   450   451   452   453