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 list week:: |
7 2. Question 2 |
|
8 3. Question 3 |
|
9 |
7 |
|
8 ["sun", "mon", "tue", "wed", "thu", "fri", "sat"] |
10 |
9 |
11 Programming |
10 ``"Sun" in week`` returns True or False? |
12 ----------- |
|
13 |
11 |
14 .. A minimum of 2 questions here. |
12 Answer: False |
|
13 |
|
14 #. Given the string ``s = "palindrome"``, what is returned by s[4:] |
15 |
15 |
16 1. Programming Assignment 1 |
16 Answer: ``ndrome`` |
17 2. Programming Assignment 2 |
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) |