manipulating-lists/questions.rst
author anand
Thu, 11 Nov 2010 00:03:57 +0530
changeset 472 fcdec2d28c9a
parent 312 8cb703eee88d
permissions -rw-r--r--
Checklist OK for `The other kinds of plots`
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
312
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     1
Objective Questions
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     2
-------------------
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     3
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     4
.. A mininum of 8 questions here (along with answers)
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     5
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     6
1. Given the list primes, ``primes = [2, 3, 5, 7, 11, 13, 17, 19, 23,
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     7
   29]``, How do you obtain the last 4 primes?
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     8
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     9
   Answer: primes[-4:]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    10
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    11
#. Given the list primes, ``primes = [2, 3, 5, 7, 11, 13, 17, 19, 23,
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    12
   29]``, What is the output of ``primes[::5]``?
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    13
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    14
   Answer: ``[2, 13]``
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    15
   
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    16
#. Given a list, p, of unknown length, obtain the first 3 (or all, if
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    17
   there are fewer) characters of it. 
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    18
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    19
   Answer: p[:3]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    20
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    21
#. The method ``reverse`` reverses a list in-place. True or False?
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    22
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    23
   Answer: True
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    24
   
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    25
#. ``reversed`` function reverses a list in-place. True or False?
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    26
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    27
   Answer: False
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    28
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    29
#. Given the list ``p = [1, 2, 3]``. p[4] produces an IndexError. True
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    30
   or False?
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    31
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    32
   Answer: True
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    33
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    34
#. Given the list ``p = [1, 2, 3]``. p[:4] produces an IndexError. True
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    35
   or False?
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    36
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    37
   Answer: False
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    38
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    39
#. Given the list primes, ``primes = [2, 3, 5, 7, 11]``, What is the
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    40
   output of ``primes[::-1]``?
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    41
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    42
   Answer: [11, 7, 5, 3, 2]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    43
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    44
#. Given the list primes, ``primes = [2, 3, 5, 7, 11]``, What is the
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    45
   output of ``primes[::-3]``?
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    46
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    47
   Answer: [11, 3]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    48
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    49
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    50
Larger Questions
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    51
----------------
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    52
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    53
.. A minimum of 2 questions here (along with answers)
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    54
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    55
#. Given a list p. Append it's reverse to itself. 
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    56
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    57
   Answer::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    58
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    59
      p = p + reversed(p)
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    60
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    61
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    62
#. Marks is a list containing the roll numbers of students followed by
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    63
   marks. [This is not a recommended way to hold the marks details,
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    64
   but the teacher knows only so much Python!] Now she wants to get
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    65
   the average of the marks. Help her do it. 
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    66
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    67
   Answer::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    68
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    69
     marks = [1, 9, 2, 8, 3, 3, 4, 10, 5, 2]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    70
     average = sum(marks[1::2])/len(marks[1::2])
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    71
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    72