manipulating_strings/questions.rst
changeset 522 d33698326409
equal deleted inserted replaced
521:88a01948450d 522:d33698326409
       
     1 Objective Questions
       
     2 -------------------
       
     3 
       
     4 .. A mininum of 8 questions here (along with answers)
       
     5 
       
     6 1. Given the list week::
       
     7 
       
     8      ["sun", "mon", "tue", "wed", "thu", "fri", "sat"]
       
     9 
       
    10    ``"Sun" in week`` returns True or False?
       
    11 
       
    12    Answer: False
       
    13    
       
    14 #. Given the string ``s = "palindrome"``, what is returned by s[4:]
       
    15 
       
    16    Answer: ``ndrome``
       
    17 
       
    18 #. Given the string ``s = "palindrome"``, what is returned by s[-4]
       
    19 
       
    20    Answer: ``r``
       
    21 
       
    22 #. Given the string ``s = "palindrome"``, what is returned by s[4::-1]
       
    23 
       
    24    Answer: ``nilap``
       
    25 
       
    26 #. Given the string ``s = "palindrome"``, what is returned by s[-4:]
       
    27 
       
    28    Answer: ``rome``
       
    29 
       
    30 #. Given a string ``s = "this is a string"``, how will you change it
       
    31    to ``"this isn't a list"`` ?
       
    32 
       
    33    Answer::
       
    34    
       
    35      s = s.replace("string", "list")
       
    36      s = s.replace("is", "isn't")
       
    37 
       
    38 #. Given a string ``s = "this is a string"``, how will you change it
       
    39    to ``"THIS ISN'T A LIST"`` ?
       
    40 
       
    41    Answer::
       
    42    
       
    43      s = s.replace("string", "list")
       
    44      s = s.replace("is", "isn't")
       
    45      s = s.upper()
       
    46 
       
    47 #. Given a line from a CSV file (comma separated values), convert it
       
    48    to a space separated line. 
       
    49 
       
    50    Answer: line.replace(',', ' ')
       
    51 
       
    52 #. Given the string "F.R.I.E.N.D.S" in s, obtain the "friends".
       
    53 
       
    54    Answer: ``s[::2].lower()``
       
    55 
       
    56 Larger Questions
       
    57 ----------------
       
    58 
       
    59 .. A minimum of 2 questions here (along with answers)
       
    60 
       
    61 1. Given the string friends, obtain the string "F.R.I.E.N.D.S". 
       
    62 
       
    63    Answer::
       
    64    
       
    65      s = "friends"
       
    66      s = s.upper()
       
    67      s_list = list(s)
       
    68      ".".join(s_list)
       
    69 
       
    70 2. Given a string with double quotes and single quotes. Interchange
       
    71    all the double quotes to single quotes and vice-versa. [Solve it
       
    72    for this particular string. Not a generic one. Solve it for a
       
    73    generic string, if you know how to iterate over a list.]
       
    74 
       
    75    s = """ "Isn't this a 'simple' task?" "No, it isn't." "Yes! it is." """
       
    76 
       
    77    Answer::
       
    78    
       
    79      s = s.replace('"', '#')
       
    80      s = s.replace("'", '"')
       
    81      s = s.replace('#', "'")        
       
    82 
       
    83    .. depends on ``for`` which is not an LO dependency a generic string. 
       
    84    
       
    85    For generic string
       
    86    Answer:: 
       
    87 
       
    88      S = []
       
    89      s_list = s.split("'")
       
    90 
       
    91      for s_s in s_list:
       
    92          S.append(s_s.replace('"', "'"))
       
    93 
       
    94      s = '"'.join(S)