manipulating-strings/questions.rst
author Nishanth <nishanth@fossee.in>
Tue, 12 Oct 2010 11:19:09 +0530
changeset 333 91b427241f8f
parent 294 64d8bc8b33d4
permissions -rw-r--r--
Finished sets
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
266
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
     1
Objective Questions
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
     2
-------------------
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
     3
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
     4
.. A mininum of 8 questions here (along with answers)
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
     5
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
     6
1. Given the list week::
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
     7
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
     8
     ["sun", "mon", "tue", "wed", "thu", "fri", "sat"]
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
     9
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    10
   ``"Sun" in week`` returns True or False?
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    11
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    12
   Answer: False
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    13
   
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    14
#. Given the string ``s = "palindrome"``, what is returned by s[4:]
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    15
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    16
   Answer: ``ndrome``
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    17
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    18
#. Given the string ``s = "palindrome"``, what is returned by s[-4]
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    19
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    20
   Answer: ``r``
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    21
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    22
#. Given the string ``s = "palindrome"``, what is returned by s[4::-1]
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    23
266
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    24
   Answer: ``nilap``
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    25
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    26
#. Given the string ``s = "palindrome"``, what is returned by s[-4:]
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    27
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    28
   Answer: ``rome``
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    29
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    30
#. Given a string ``s = "this is a string"``, how will you change it
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    31
   to ``"this isn't a list"`` ?
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    32
266
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    33
   Answer::
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    34
   
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    35
     s = s.replace("string", "list")
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    36
     s = s.replace("is", "isn't")
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    37
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    38
#. Given a string ``s = "this is a string"``, how will you change it
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    39
   to ``"THIS ISN'T A LIST"`` ?
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    40
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    41
   Answer::
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    42
   
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    43
     s = s.replace("string", "list")
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    44
     s = s.replace("is", "isn't")
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    45
     s = s.upper()
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    46
266
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    47
#. Given a line from a CSV file (comma separated values), convert it
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    48
   to a space separated line. 
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    49
294
64d8bc8b33d4 reviewed questions
Nishanth <nishanth@fossee.in>
parents: 266
diff changeset
    50
   Answer: line.replace(',', ' ')
266
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    51
294
64d8bc8b33d4 reviewed questions
Nishanth <nishanth@fossee.in>
parents: 266
diff changeset
    52
#. Given the string "F.R.I.E.N.D.S" in s, obtain the "friends".
266
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    53
294
64d8bc8b33d4 reviewed questions
Nishanth <nishanth@fossee.in>
parents: 266
diff changeset
    54
   Answer: ``s[::2].lower()``
266
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    55
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    56
Larger Questions
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    57
----------------
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    58
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    59
.. A minimum of 2 questions here (along with answers)
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    60
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    61
1. Given the string friends, obtain the string "F.R.I.E.N.D.S". 
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    62
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    63
   Answer::
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    64
   
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    65
     s = "friends"
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    66
     s = s.upper()
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    67
     s_list = list(s)
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    68
     ".".join(s_list)
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    69
266
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    70
2. Given a string with double quotes and single quotes. Interchange
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    71
   all the double quotes to single quotes and vice-versa. [Solve it
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    72
   for this particular string. Not a generic one. Solve it for a
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    73
   generic string, if you know how to iterate over a list.]
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    74
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    75
   s = """ "Isn't this a 'simple' task?" "No, it isn't." "Yes! it is." """
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    76
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    77
   Answer::
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    78
   
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    79
     s = s.replace('"', '#')
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    80
     s = s.replace("'", '"')
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    81
     s = s.replace('#', "'")        
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    82
266
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    83
   .. depends on ``for`` which is not an LO dependency a generic string. 
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    84
   
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    85
   For generic string
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    86
   Answer:: 
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    87
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    88
     S = []
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    89
     s_list = s.split("'")
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    90
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    91
     for s_s in s_list:
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    92
         S.append(s_s.replace('"', "'"))
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    93
8018779e02b7 Added questions to manipulating strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    94
     s = '"'.join(S)