Page 307 - Computer Science Class 11 Without Functions
P. 307

Solved Programming Questions

            1.  Write a program that accepts as input a list from a user and displays the list without duplicates.
              01 '''
              02     Objective: To remove duplicate from a list
              03     Input: list with duplicate elements
              04     Output: A list without duplicate elements
              05 '''
              06 myList = eval(input('Enter the list:\n'))
              07 print('Original List:' , myList)
              08 noDuplicateList = []
              09 for element in myList:
              10     if element not in noDuplicateList:
              11         #append it to noDuplicateList
              12         noDuplicateList.append(element)
              13 print('List with no duplicates:' , noDuplicateList)

            Output:
             >>> Enter the list:
                  [4, 6, 2, 5, 6, 3, 6, 4, 9, 2, 4, 6, 7, 4, 6, 7]
                  Original List: [4, 6, 2, 5, 6, 3, 6, 4, 9, 2, 4, 6, 7, 4, 6, 7]
                  List with no duplicates: [4, 6, 2, 5, 3, 9, 7]
            2.   Given a list (say, list1) of numbers, its cumulative list is another list (say, list2) whose ith element is the sum of all
                elements till the ith element of lst1. If a list is empty, then its cumulative list will also be an empty list. Write a
                program that accepts a list of integers and displays its cumulative list.
              01 '''
              02     Objective: To find the cumulative sum of elements of an integer list
              03     Input: a list of numbers
              04     output: cumulative sum of elements of the input list
              05 '''
              06 myList = eval(input('Enter a list of numbers: '))
              07
              08 if len(myList) == 0 :
              09     cumLst = []
              10 else:
              11     cumLst = [myList[0]] # myList[0] is sum of 1-element list
              12     for i in range(1,len(myList)):
              13         nextElement = cumLst[i-1] + myList[i]
              14         cumLst.append(nextElement)
              15 print('List of cumulative sums: ' ,cumLst)
            Output:

             >>> Enter a list of numbers: [10,20,30,40,50,60]
                  List of cumulative sums:  [10, 30, 60, 100, 150, 210]
            3.   Write a program that takes a tuple myTuple and a value key to be searched in myTuple as inputs from the
                user. When the key is present in myTuple, the program outputs True and the index where the search key is

                found. If it reaches the end of the list without finding the key, the program returns False.
              01 '''
              02 Objective: To search for a key in a tuple
              03 User Interface:
              04   1. User is asked to enter a tuple
              05   2. The user is asked to enter the key, and the program reports the search
              06        result.  This is repeated so long as the user wants to continue.
              07 '''
              08 myTuple = eval(input('Enter a tuple:\n'))
              09 print('Contents of Tuple:', myTuple)


                                                                                                Lists and Tuples  305
   302   303   304   305   306   307   308   309   310   311   312