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

10 while True:
          11     key = int(input('Enter the number to be searched:'))
          12     found = False
          13     for index in range(len(myTuple)):
          14         if myTuple[index] == key:
          15             found = True
          16             break
          17     if found:
          18         print(key,' is present in the tuple at index', index)
          19     else:
          20         print(key,' is not present in the tuple')
          21     searchAgain = input('Continue another search? say Y/y for yes, N/n for no:')
          22     if searchAgain != 'Y' and searchAgain != 'y':
          23         break
        Output:

         >>> Enter a tuple:
              (14, 6, 1, 8, 14, 50, 61, 89, 37, 109, 3, 21, 89, 90, 60)
              Contents of Tuple: (14, 6, 1, 8, 14, 50, 61, 89, 37, 109, 3, 21, 89, 90, 60)
         >>> Enter the number to be searched:37
              37  is present in the tuple at index 8
              Continue another search? say Y/y for yes, N/n for no:y
         >>> Enter the number to be searched:10
              10  is not present in the tuple
              Continue another search? say Y/y for yes, N/n for no:n
        4.   Write a program that accepts a tuple of integers and displays the count of frequency of each element in the tuple.

          01 '''
          02 Objective: To count the frequency of each element in myTuple
          03 Input: a tuple of keys
          04 Output:
          05     frequency: A list comprising (key, frequency) tuples
          06 '''
          07
          08 myTuple = eval(input('Enter a tuple: '))
          09
          10 print('Tuple:', myTuple)
          11
          12 frequency = []
          13 uniqueMyTuple = set(myTuple)
          14
          15 for item in uniqueMyTuple:
          16     frequency.append((item, myTuple.count(item)))
          17
          18 print('List of (key, frequency) tuples is as follows\n', frequency)
        Output:

         >>> Enter a tuple: (1,7,1,9,4,2,9,1,2,4,4,9)
              Tuple: (1, 7, 1, 9, 4, 2, 9, 1, 2, 4, 4, 9)
              List of (key, frequency) tuples is as follows
              [(1, 3), (2, 2), (4, 3), (7, 1), (9, 3)]













         306   Touchpad Computer Science-XI
   303   304   305   306   307   308   309   310   311   312   313