Welcome to my website - Ankit
|
1 """
2 CLASS XII
3
4 COMPUTER SCIENCE QUESTIONS FOR PRACTICAL FILE
5
6 Q1. Write a program that accepts comma separated words as input in the form of list of strings from
7 the user in main driver program and does following by calling functions for each one of the following and
8 passing this list as parameter to these functions
9
10 a) UNIQUE_STR() Prints the unique words in sorted form
11 b) REMOVE_STRI() Remove a particular string
12 c) VOWEL_STR Printing all words which start which Vowel
13
14 SOURCE CODE 1 :
15 """
16 a = input("enter the comma separated words : ")
17 list_input = a.split(',')
18
19
20 def unique_str(list1):
21 list_set = set(list1)
22 unique_list = (list(list_set))
23 list1 = sorted(list1)
24 return ' '.join(map(str, unique_list))
25
26
27 def remove_string(list1):
28 str_rem = input("enter the string to be removed : ")
29 while str_rem in list1: list1.remove(str_rem)
30 return ' '.join(map(str, list1))
31
32
33 def vowel_str(list1):
34 list2 = []
35 for x in range(len(list1)):
36 if list1[x][0] == 'a' or list1[x][0] == 'e' or list1[x][0] == 'i' or list1[x][0] == 'o' or list1[x][0] == 'u':
37 list2.append(list1[x])
38 return ' '.join(map(str, list2))
39
40
41 print("unique words are: ", unique_str(list_input))
42 print("string after removing the word : ", remove_string(list_input))
43 print("the word starting with vowel : ", vowel_str(list_input))
44
45 """
46 ANSWER 1 :
47
48 enter the comma separated words : ankit,ank,ank,ank,a,an,ank
49 unique words are: a an ankit ank
50 enter the string to be removed : ank
51 string after removing the word : ankit a an
52 the word starting with vowel : ankit a an
53 """
54 """
55 Q3 Finding , whether the number is Prime in Python using recursion ?
56
57 SOURCE CODE 3 :
58 """
59
60
61 def is_prime(n3, i3=2):
62 if n3 <= 2:
63 return True if (n3 == 2) else False
64 if n3 % i3 == 0:
65 return False
66 if i3 * i3 > n3:
67 return True
68
69 return is_prime(n3, i3 + 1)
70
71
72 n = 15
73 if is_prime(n):
74 print("Yes")
75 else:
76 print("No")
77
78 # ANSWER 3 : no
79
80 """
81 Q4. Given a dictionary
82 my_dict = {1: 'apple', 2: 'ball',3:”Rubber”, 4:”banana”,5:”also”}
83 Example of dictionary is suggestive , can take any string values.
84
85 Write a function count() which receives this dictionary in expanded form and returns the count of the number of strings with equal length.
86 For eg. In above case it will return 2.
87
88
89 SOURCE CODE 4 :
90 """
91 a = {1: 'abc', 2: 'defg', 3: 'ghij'}
92 b = list(a.values())
93
94 c = len(b)
95 d = 0
96
97 for i in range(len(b)):
98
99 for j in range(len(b)):
100
101 if len(b[i]) == len(b[j]):
102 d = d + 1
103
104 print("answer is : ", (d - c))
105
106 """
107 ANSWER 4 :
108
109 answer is : 2
110 """
111 """
112 Q5. Write a function swap() which takes a list as an argument and swaps neighbouring elements of List.
113 eg. If L1=[10,20,30,40,50,60]
114 then after swap(L1) should be printed as [20,10,40,30,60,50]
115
116
117 SOURCE CODE 5 :
118 """
119
120
121 def swap():
122 a5 = str(input("enter the elements of list separated by comma : ")) # a = [10,20,30,40,50]
123 b5 = a5.split(',')
124
125 if len(b5) % 2 == 0:
126 for i5 in range(0, len(b5), 2):
127 b5[i5], b5[i5 + 1] = b5[i5 + 1], b5[i5]
128 else:
129 for i5 in range(0, len(b5) - 1, 2):
130 b5[i5], b5[i5 + 1] = b5[i5 + 1], b5[i5]
131
132 return b5
133
134
135 print(swap())
136
137 """
138 ANSWER 5 :
139
140
141 enter the elements of list separated by comma : 12,23,34,45,56
142 ['23', '12', '45', '34', '56']
143 """
144 """
145 Q6. Print multiplication table of a number using Recursion. Pass the number as parameter
146
147
148 SOURCE CODE 6 :
149 """
150
151
152 def table(n6, i6=1):
153 print(n6 * i6)
154 if i6 != 10:
155 table(n6, i6 + 1)
156
157
158 num = int(input("enter a number n: "))
159 table(num)
160
161 """
162 ANSWER 6 :
163
164
165 enter a number: 4
166 4
167 8
168 12
169 16
170 20
171 24
172 28
173 32
174 26
175 40
176 """
177 """
178 Q7. Write a function to calculate power of a number raised to other ( ab) using recursion.
179
180 SOURCE CODE 7 :
181 """
182
183
184 def power(x, y, p7):
185 if y >= 1:
186 p7 = p7 * x
187 return power(x, y - 1, p7)
188 else:
189 return p7
190
191
192 p = 1
193 num = int(input("Enter a Number: "))
194 n = int(input("Enter a power: "))
195 pow1 = power(num, n, p)
196
197 print(pow1)
198
199 """
200 ANSWER 7 :
201
202
203 Enter a Number: 6
204 Enter a Power: 2
205 36
206 """
207 """
208 Q 9 . Write a recursive code to find the sum of all elements of a list.
209
210
211 SOURCE CODE 9 :
212 """
213
214
215 def sum_lst(l9, n9):
216 if n9 == 0:
217 return 0
218 else:
219 return sum_lst(l9, n9 - 1) + l9[n9 - 1]
220
221
222 List = eval(input("enter list "))
223 num = len(List)
224 s = sum_lst(List, num)
225 print("sum of list elements is ", s)
226
227 """
228 ANSWER 9 :
229
230 enter a list[4,5,6]
231 sum of list elements is 15
232 """
233 """
234 Q10. Write a recursive code to compute the nth Fibonacci number.
235
236
237 SOURCE CODE 10 :
238 """
239
240
241 def fib(n10):
242 if n10 == 1:
243 return 0
244 elif n10 == 2:
245 return 1
246 else:
247 return fib(n10 - 1) + fib(n10 - 2)
248
249
250 n = int(input("enter last term required : "))
251 for i in range(1, n + 1):
252 print(fib(i), end=',')
253 print(", , ,")
254
255 """
256 ANSWER 10 :
257
258 enter last term required : 5
259 0,1,1,2,3,5
260 """
261