accessing-pieces-arrays/questions.rst
changeset 224 4e0cdb5cca53
parent 217 b595f90016c5
equal deleted inserted replaced
223:ce0e8c99eaee 224:4e0cdb5cca53
     1 Objective
     1 Objective Questions
     2 ---------
     2 -------------------
     3 
     3 
     4 .. A mininum of 8 questions here. 
     4 .. A mininum of 8 questions here (along with answers)
     5 
     5 
     6 1. Question 1
     6 1. Given the array, ``A = array([12, 15, 18, 21])``, how do we access
     7 2. Question 2
     7    the element ``18``?
     8 3. Question 3
       
     9 
     8 
       
     9    Answer: ``A[2]``
    10 
    10 
       
    11 #. Given the array, ``A = array([12, 15, 18, 21])``, what element is
       
    12    accessed by ``A[1]``
       
    13 
       
    14    Answer: ``15``  
       
    15 
       
    16 #. Given the array, ``A = array([12, 15, 18, 21])``, what element is
       
    17    accessed by ``A[-3]``?
       
    18 
       
    19    Answer: ``15``  
       
    20 
       
    21 #. Given the array, ``A = array([12, 15, 18, 21])``, how do you change
       
    22    the element ``15`` to ``51``?
       
    23 
       
    24    Answer: ``A[1] = 51``
       
    25 
       
    26 #. Given the array, ``A = array([12, 15, 18, 21])``, what is ``A``
       
    27    after the execution of the statement ``A[3] = 24``?
       
    28 
       
    29    Answer: ``array([12, 15, 18, 24])``
       
    30 
       
    31 #. Given the array, ``A = array([12, 15, 18, 21])``, what is the
       
    32    result of the statement, ``A[4] = 24``?
       
    33 
       
    34    Answer: IndexError: index out of bounds
       
    35 
       
    36 #. Given the array, ``B = array([[12, 15], [18, 21]])``, how do we
       
    37    access the element ``18``? 
       
    38 
       
    39    Answer: ``A[0, 0]``
       
    40 
       
    41 #. Given the array, ``B = array([[12, 15], [18, 21]])``, what is the
       
    42    result of ``B[1]``? 
       
    43 
       
    44    Answer:  ``array([18, 21])``
       
    45 
       
    46 #. Given the array, ``B = array([[12, 15], [18, 21]])``, what is the
       
    47    result of ``B[1, 1]``?
       
    48 
       
    49    Answer:  ``21``
       
    50 
       
    51 #. Given the array, ``B = array([[12, 15], [18, 21]])``, what element
       
    52    is accessed by ``B[-1, -2]``?
       
    53 
       
    54    Answer:  ``18``   
       
    55 
       
    56 #. Given the array, ``B = array([[12, 15], [18, 21]])``, how do you
       
    57    change the element ``15`` to ``51``?
       
    58 
       
    59    Answer:  ``B[0, 1] = 51``   
       
    60 
       
    61 #. Given the array, ``B = array([[12, 15], [18, 21]])``, what is the
       
    62    value of B, after executing, ``B[0] = 0``?
       
    63 
       
    64    Answer:  ``array([[0, 0], [18, 21]])``   
       
    65 
       
    66 #. Given the array, 
       
    67    
       
    68    ::
       
    69    
       
    70      B = array([[10, 11, 12, 13],
       
    71                 [20, 21, 22, 23],
       
    72                 [30, 31, 32, 33],
       
    73                 [40, 41, 42, 43]])
       
    74 
       
    75    Obtain the elements, ``[[21, 22], [32, 32]]``
       
    76 
       
    77    Answer:  ``B[1:3, 1:3]``   
       
    78 
       
    79 #. Given the array, 
       
    80    
       
    81    ::
       
    82    
       
    83      B = array([[10, 11, 12, 13],
       
    84                 [20, 21, 22, 23],
       
    85                 [30, 31, 32, 33],
       
    86                 [40, 41, 42, 43]])
       
    87 
       
    88    Obtain the elements, ``[[10, 13], [30, 33]]``
       
    89 
       
    90    Answer:  ``B[::2, ::3]``
       
    91 
       
    92 #. What command is used to read an image file?
       
    93 
       
    94    a. imshow
       
    95    #. imread
       
    96    #. imopen
       
    97    #. open
       
    98 
       
    99    Answer: ``imread``   
       
   100 
       
   101 #. If ``P`` is ``array([12, 13, 14, 15])`` then ``P.shape`` gives
       
   102    ``(4, 1)``. True or False?
       
   103 
       
   104    Answer: False
       
   105      
    11 Larger Questions
   106 Larger Questions
    12 ----------------
   107 ----------------
    13 
   108 
    14 .. A minimum of 2 questions here. 
   109 .. A minimum of 2 questions here (along with answers)
    15 
   110 
    16 1. Programming Assignment 1
   111 1. Given the array, 
    17 2. Programming Assignment 2
   112    ::
       
   113    
       
   114      B = array([[10, 11, 12, 13],
       
   115                 [20, 21, 22, 23],
       
   116                 [30, 31, 32, 33],
       
   117                 [40, 41, 42, 43]])
       
   118 
       
   119    Change the array to 
       
   120    ::
       
   121    
       
   122      B = array([[10, 11, 10, 11],
       
   123                 [20, 21, 20, 21],
       
   124                 [10, 11, 10, 11],
       
   125                 [20, 21, 20, 21]])
       
   126 
       
   127    using slicing and striding. 
       
   128 
       
   129    Answer::
       
   130 
       
   131      B[:2, 2:] = B[:2, :2]
       
   132      B[2:, :2] = B[:2, :2]
       
   133      B[2:, 2:] = B[:2, :2]
       
   134 
       
   135 #. Find out how to display a grayscale image with proper colors. Hint,
       
   136    use ``?`` on ``imshow``. 
       
   137    
       
   138    Answer: ``cmap=cm.gray``
       
   139                   
       
   140 #. From the image, ``tagore-einstein.png``, 
       
   141    a. show only the face of Einstein. 
       
   142    b. show only the face of Tagore. 
       
   143 
       
   144    Hint, use ``imread`` to read the image and ``imshow`` to show the
       
   145    image.  Use ``cm.gray`` as ``cmap`` to display the image properly.   
       
   146 
       
   147    Answer:: 
       
   148 
       
   149      F = imread('tagore-einstein.png')
       
   150      imshow(F[:210, 20:180], cmap=cm.gray)
       
   151 
       
   152      F = imread('tagore-einstein.png')
       
   153      imshow(F[30:300, 320:480], cmap=cm.gray)
       
   154 
       
   155 .. #[Puneeth: This question contains answer to previous
       
   156 .. question. Shouldn't be shown at the same time.]
       
   157 
       
   158 
       
   159