1
+ {
2
+ "nbformat" : 4 ,
3
+ "nbformat_minor" : 0 ,
4
+ "metadata" : {
5
+ "colab" : {
6
+ "provenance" : []
7
+ },
8
+ "kernelspec" : {
9
+ "name" : " python3" ,
10
+ "display_name" : " Python 3"
11
+ },
12
+ "language_info" : {
13
+ "name" : " python"
14
+ }
15
+ },
16
+ "cells" : [
17
+ {
18
+ "cell_type" : " code" ,
19
+ "execution_count" : null ,
20
+ "metadata" : {
21
+ "colab" : {
22
+ "base_uri" : " https://localhost:8080/"
23
+ },
24
+ "id" : " XUsOSSuUUzhk" ,
25
+ "outputId" : " 62119328-e829-435e-c681-5ff39be56f6b"
26
+ },
27
+ "outputs" : [
28
+ {
29
+ "output_type" : " stream" ,
30
+ "name" : " stdout" ,
31
+ "text" : [
32
+ " ['Mahesh', 'Ali', 'Jacob']\n "
33
+ ]
34
+ }
35
+ ],
36
+ "source" : [
37
+ " # Q1\n " ,
38
+ " \n " ,
39
+ " # Defining two lists of employee names\n " ,
40
+ " list1 = [\" Ramesh\" , \" Suresh\" , \" Mahesh\" , \" Ali\" , \" Jacob\" , \" Saritha\" ]\n " ,
41
+ " list2 = [\" Ali\" , \" Mukesh\" , \" Mahesh\" , \" Jacob\" , \" Sai\" , \" Sarita\" ]\n " ,
42
+ " \n " ,
43
+ " common_names = []\n " ,
44
+ " \n " ,
45
+ " for name in list1:\n " ,
46
+ " if name in list2:\n " ,
47
+ " common_names.append(name)\n " ,
48
+ " \n " ,
49
+ " # Display the common names\n " ,
50
+ " print(common_names)\n " ,
51
+ " \n "
52
+ ]
53
+ },
54
+ {
55
+ "cell_type" : " code" ,
56
+ "source" : [
57
+ " # Q2\n " ,
58
+ " \n " ,
59
+ " # Defining the string with names\n " ,
60
+ " names_str = \" Ramesh Suresh Mohit\"\n " ,
61
+ " \n " ,
62
+ " names_list = names_str.split()\n " ,
63
+ " \n " ,
64
+ " # Define the ages for each name\n " ,
65
+ " ages = {\n " ,
66
+ " \" Ramesh\" : 25,\n " ,
67
+ " \" Suresh\" : 22,\n " ,
68
+ " \" Mohit\" : 26\n " ,
69
+ " }\n " ,
70
+ " \n " ,
71
+ " for name in names_list:\n " ,
72
+ " print(f\" {name}: {ages[name]}\" )\n " ,
73
+ " \n "
74
+ ],
75
+ "metadata" : {
76
+ "colab" : {
77
+ "base_uri" : " https://localhost:8080/"
78
+ },
79
+ "id" : " e77kMPNk7Edl" ,
80
+ "outputId" : " b9fdb21b-8c26-480a-9db7-62748b0cdafc"
81
+ },
82
+ "execution_count" : null ,
83
+ "outputs" : [
84
+ {
85
+ "output_type" : " stream" ,
86
+ "name" : " stdout" ,
87
+ "text" : [
88
+ " Ramesh: 25\n " ,
89
+ " Suresh: 22\n " ,
90
+ " Mohit: 26\n "
91
+ ]
92
+ }
93
+ ]
94
+ },
95
+ {
96
+ "cell_type" : " code" ,
97
+ "source" : [
98
+ " # Q3\n " ,
99
+ " \n " ,
100
+ " # Defining the quantities and costs\n " ,
101
+ " quantities = {\" paracetamol\" : 2, \" azithromycin\" : 3, \" vitamin_c\" : 5}\n " ,
102
+ " costs = {\" paracetamol\" : 35, \" azithromycin\" : 49, \" vitamin_c\" : 33}\n " ,
103
+ " \n " ,
104
+ " # Calculate the total cost for each medicine\n " ,
105
+ " total_cost_each = {medicine: quantities[medicine] * costs[medicine] for medicine in quantities}\n " ,
106
+ " \n " ,
107
+ " # Calculate the total cost of all medicines\n " ,
108
+ " total_cost_all = sum(total_cost_each.values())\n " ,
109
+ " \n " ,
110
+ " # Amount given by the patient\n " ,
111
+ " amount_given = 2000\n " ,
112
+ " \n " ,
113
+ " # Calculate the refund\n " ,
114
+ " refund = amount_given - total_cost_all\n " ,
115
+ " \n " ,
116
+ " total_cost_each, total_cost_all, refund\n " ,
117
+ " \n "
118
+ ],
119
+ "metadata" : {
120
+ "colab" : {
121
+ "base_uri" : " https://localhost:8080/"
122
+ },
123
+ "id" : " 1E9xRZ0F7LSm" ,
124
+ "outputId" : " a52e7936-d442-48ea-d1a2-355ddbd10a86"
125
+ },
126
+ "execution_count" : null ,
127
+ "outputs" : [
128
+ {
129
+ "output_type" : " execute_result" ,
130
+ "data" : {
131
+ "text/plain" : [
132
+ " ({'paracetamol': 70, 'azithromycin': 147, 'vitamin_c': 165}, 382, 1618)"
133
+ ]
134
+ },
135
+ "metadata" : {},
136
+ "execution_count" : 8
137
+ }
138
+ ]
139
+ },
140
+ {
141
+ "cell_type" : " code" ,
142
+ "source" : [
143
+ " # Q4\n " ,
144
+ " \n " ,
145
+ " def count_vowels(sentence):\n " ,
146
+ " # Define vowels\n " ,
147
+ " vowels = \" aeiouAEIOU\"\n " ,
148
+ " \n " ,
149
+ " # Count vowels\n " ,
150
+ " vowel_count = sum(1 for char in sentence if char in vowels)\n " ,
151
+ " \n " ,
152
+ " return vowel_count\n " ,
153
+ " \n " ,
154
+ " # Test the function with the provided sentence\n " ,
155
+ " user_sentence = input(\" Enter a sentence: \" )\n " ,
156
+ " vowel_count = count_vowels(user_sentence)\n " ,
157
+ " \n " ,
158
+ " \n " ,
159
+ " print(f\" The sentence '{user_sentence}' contains {vowel_count} vowels.\" )\n " ,
160
+ " \n "
161
+ ],
162
+ "metadata" : {
163
+ "colab" : {
164
+ "base_uri" : " https://localhost:8080/"
165
+ },
166
+ "id" : " TB2huMID7Zd5" ,
167
+ "outputId" : " 2c8f5635-6e41-480e-dbb0-0e7688c64e9d"
168
+ },
169
+ "execution_count" : null ,
170
+ "outputs" : [
171
+ {
172
+ "output_type" : " stream" ,
173
+ "name" : " stdout" ,
174
+ "text" : [
175
+ " Enter a sentence: hey this is somnath\n " ,
176
+ " The sentence 'hey this is somnath' contains 5 vowels.\n "
177
+ ]
178
+ }
179
+ ]
180
+ },
181
+ {
182
+ "cell_type" : " code" ,
183
+ "source" : [
184
+ " # Q5\n " ,
185
+ " \n " ,
186
+ " # Get user input for age\n " ,
187
+ " age = int(input(\" Enter your age: \" ))\n " ,
188
+ " \n " ,
189
+ " # Define the eligibility criteria\n " ,
190
+ " voting_age = 18\n " ,
191
+ " \n " ,
192
+ " # Check eligibility and display the result\n " ,
193
+ " if age >= voting_age:\n " ,
194
+ " print(\" Congrats! You are eligible for voting.\" )\n " ,
195
+ " else:\n " ,
196
+ " years_to_wait = voting_age - age\n " ,
197
+ " print(f\" you have to return after {years_to_wait} number of years. The eligibility criteria for voting is 18 years. \" )\n "
198
+ ],
199
+ "metadata" : {
200
+ "colab" : {
201
+ "base_uri" : " https://localhost:8080/"
202
+ },
203
+ "id" : " KoPAtMsD7g-0" ,
204
+ "outputId" : " 2bbe4661-517d-4fa8-ca08-215815f9cd71"
205
+ },
206
+ "execution_count" : null ,
207
+ "outputs" : [
208
+ {
209
+ "output_type" : " stream" ,
210
+ "name" : " stdout" ,
211
+ "text" : [
212
+ " Enter your age: 21\n " ,
213
+ " Congrats! You are eligible for voting.\n "
214
+ ]
215
+ }
216
+ ]
217
+ },
218
+ {
219
+ "cell_type" : " code" ,
220
+ "source" : [
221
+ " # Q6\n " ,
222
+ " \n " ,
223
+ " # Given list\n " ,
224
+ " A = [1, 2, 3, 4, 5]\n " ,
225
+ " \n " ,
226
+ " # Initialize an empty list to store cumulative sums\n " ,
227
+ " cumulative_sum = []\n " ,
228
+ " \n " ,
229
+ " # Initialize a variable to keep track of the running sum\n " ,
230
+ " running_sum = 0\n " ,
231
+ " \n " ,
232
+ " # Calculate cumulative sum and store in the new list\n " ,
233
+ " for num in A:\n " ,
234
+ " running_sum += num\n " ,
235
+ " cumulative_sum.append(running_sum)\n " ,
236
+ " \n " ,
237
+ " # Display the result\n " ,
238
+ " print(\" Input List:\" , A)\n " ,
239
+ " print(\" Cumulative Sum List:\" , cumulative_sum)\n " ,
240
+ " \n " ,
241
+ " \n "
242
+ ],
243
+ "metadata" : {
244
+ "colab" : {
245
+ "base_uri" : " https://localhost:8080/"
246
+ },
247
+ "id" : " S0IIQQEC7nB6" ,
248
+ "outputId" : " cc904747-bc8c-48e1-bf17-0b1fdf7efddc"
249
+ },
250
+ "execution_count" : null ,
251
+ "outputs" : [
252
+ {
253
+ "output_type" : " stream" ,
254
+ "name" : " stdout" ,
255
+ "text" : [
256
+ " Input List: [1, 2, 3, 4, 5]\n " ,
257
+ " Cumulative Sum List: [1, 3, 6, 10, 15]\n "
258
+ ]
259
+ }
260
+ ]
261
+ },
262
+ {
263
+ "cell_type" : " code" ,
264
+ "source" : [
265
+ " # Q7\n " ,
266
+ " \n " ,
267
+ " def encode_message(sentence):\n " ,
268
+ " vowels = \" aeiouAEIOU\"\n " ,
269
+ " words = sentence.split()\n " ,
270
+ " encoded_words = []\n " ,
271
+ " \n " ,
272
+ " for word in words:\n " ,
273
+ " # Check if the word starts with a vowel\n " ,
274
+ " if word[0] in vowels:\n " ,
275
+ " encoded_word = word[0] + word[-1]\n " ,
276
+ " else:\n " ,
277
+ " # Remove all vowels if the word starts with a consonant\n " ,
278
+ " encoded_word = ''.join([char for char in word if char not in vowels])\n " ,
279
+ " encoded_words.append(encoded_word)\n " ,
280
+ " \n " ,
281
+ " return ' '.join(encoded_words)\n " ,
282
+ " \n " ,
283
+ " # Test the function with the provided sentence\n " ,
284
+ " input_text = \" The quick brown fox used to sleep inside this box\"\n " ,
285
+ " encoded_msg = encode_message(input_text)\n " ,
286
+ " encoded_msg\n " ,
287
+ " \n "
288
+ ],
289
+ "metadata" : {
290
+ "colab" : {
291
+ "base_uri" : " https://localhost:8080/" ,
292
+ "height" : 35
293
+ },
294
+ "id" : " yBKYA4Ey7szk" ,
295
+ "outputId" : " 40a80a6a-8c61-4d42-8153-41fba0d868bf"
296
+ },
297
+ "execution_count" : null ,
298
+ "outputs" : [
299
+ {
300
+ "output_type" : " execute_result" ,
301
+ "data" : {
302
+ "text/plain" : [
303
+ " 'Th qck brwn fx ud t slp ie ths bx'"
304
+ ],
305
+ "application/vnd.google.colaboratory.intrinsic+json" : {
306
+ "type" : " string"
307
+ }
308
+ },
309
+ "metadata" : {},
310
+ "execution_count" : 13
311
+ }
312
+ ]
313
+ },
314
+ {
315
+ "cell_type" : " code" ,
316
+ "source" : [
317
+ " # Q8\n " ,
318
+ " \n " ,
319
+ " def run_length_encoding(input_string):\n " ,
320
+ " # Initialize variables\n " ,
321
+ " encoded_string = \"\"\n " ,
322
+ " current_count = 1\n " ,
323
+ " \n " ,
324
+ " # Iterate over the string\n " ,
325
+ " for i in range(1, len(input_string)):\n " ,
326
+ " # Check if the current character is the same as the previous\n " ,
327
+ " if input_string[i] == input_string[i - 1]:\n " ,
328
+ " current_count += 1\n " ,
329
+ " else:\n " ,
330
+ " # Append the count and character to the encoded string\n " ,
331
+ " encoded_string += str(current_count) + input_string[i - 1]\n " ,
332
+ " current_count = 1\n " ,
333
+ " \n " ,
334
+ " # Append the last character and its count\n " ,
335
+ " encoded_string += str(current_count) + input_string[-1]\n " ,
336
+ " \n " ,
337
+ " return encoded_string\n " ,
338
+ " \n " ,
339
+ " # Test the function with the provided string\n " ,
340
+ " input_str = \" aabbbccdddae\"\n " ,
341
+ " encoded_str = run_length_encoding(input_str)\n " ,
342
+ " encoded_str\n " ,
343
+ " \n "
344
+ ],
345
+ "metadata" : {
346
+ "id" : " IqqnwtY17w5-"
347
+ },
348
+ "execution_count" : null ,
349
+ "outputs" : []
350
+ }
351
+ ]
352
+ }
0 commit comments