1
+ {
2
+ "nbformat" : 4 ,
3
+ "nbformat_minor" : 0 ,
4
+ "metadata" : {
5
+ "colab" : {
6
+ "name" : " 8960 Candida - Practical 2.ipynb" ,
7
+ "provenance" : [],
8
+ "collapsed_sections" : [],
9
+ "authorship_tag" : " ABX9TyP4m+vvP+jimTa1VMbrbyaQ" ,
10
+ "include_colab_link" : true
11
+ },
12
+ "kernelspec" : {
13
+ "name" : " python3" ,
14
+ "display_name" : " Python 3"
15
+ },
16
+ "language_info" : {
17
+ "name" : " python"
18
+ }
19
+ },
20
+ "cells" : [
21
+ {
22
+ "cell_type" : " markdown" ,
23
+ "metadata" : {
24
+ "id" : " view-in-github" ,
25
+ "colab_type" : " text"
26
+ },
27
+ "source" : [
28
+ " <a href=\" https://colab.research.google.com/github/Candida18/Python/blob/main/8960_Candida_Practical_2.ipynb\" target=\" _parent\" ><img src=\" https://colab.research.google.com/assets/colab-badge.svg\" alt=\" Open In Colab\" /></a>"
29
+ ]
30
+ },
31
+ {
32
+ "cell_type" : " markdown" ,
33
+ "metadata" : {
34
+ "id" : " DW2vJ9nhFmYq"
35
+ },
36
+ "source" : [
37
+ " **Name : Candida Ruth Noronha**\n " ,
38
+ " \n " ,
39
+ " **Class : SE COMPS B**\n " ,
40
+ " \n " ,
41
+ " **Roll No. : 8960**\n " ,
42
+ " \n " ,
43
+ " **Title : Python Experiment 2**"
44
+ ]
45
+ },
46
+ {
47
+ "cell_type" : " markdown" ,
48
+ "metadata" : {
49
+ "id" : " WyAdgO-VFqY0"
50
+ },
51
+ "source" : [
52
+ " **OOPS**\n " ,
53
+ " \n " ,
54
+ " 1. Write a Python class to find a pair of elements (indices of the two numbers) from a given array whose sum equals a specific target number.\n " ,
55
+ " \n " ,
56
+ " **Code :**"
57
+ ]
58
+ },
59
+ {
60
+ "cell_type" : " code" ,
61
+ "metadata" : {
62
+ "id" : " ORLMkyb-FaFO" ,
63
+ "colab" : {
64
+ "base_uri" : " https://localhost:8080/"
65
+ },
66
+ "outputId" : " e279e730-3831-434e-ca31-f97032a18549"
67
+ },
68
+ "source" : [
69
+ " \n " ,
70
+ " class Pair :\n " ,
71
+ " def __init__(self,list,target):\n " ,
72
+ " self.list = list\n " ,
73
+ " self.target=target;\n " ,
74
+ " \n " ,
75
+ " def pairOfNum(self):\n " ,
76
+ " print(\" The pair of indices are : \\ n\" )\n " ,
77
+ " for i in range(0,len(self.list)):\n " ,
78
+ " for j in range(i+1,len(self.list)):\n " ,
79
+ " if(self.list[i] + self.list[j] == self.target):\n " ,
80
+ " print((i+1,j+1))\n " ,
81
+ " \n " ,
82
+ " \n " ,
83
+ " if __name__ == \" __main__\" :\n " ,
84
+ " numbers=[10,20,10,40,50,60,70]\n " ,
85
+ " target= 50\n " ,
86
+ " t = Pair(numbers,target)\n " ,
87
+ " t.pairOfNum()\n " ,
88
+ " \n " ,
89
+ " \n "
90
+ ],
91
+ "execution_count" : null ,
92
+ "outputs" : [
93
+ {
94
+ "output_type" : " stream" ,
95
+ "text" : [
96
+ " The pair of indices are : \n " ,
97
+ " \n " ,
98
+ " (1, 4)\n " ,
99
+ " (3, 4)\n "
100
+ ],
101
+ "name" : " stdout"
102
+ }
103
+ ]
104
+ },
105
+ {
106
+ "cell_type" : " markdown" ,
107
+ "metadata" : {
108
+ "id" : " AD8o0nRTjNf5"
109
+ },
110
+ "source" : [
111
+ " **OOPS**\n " ,
112
+ " \n " ,
113
+ " 2. Write a Python class to get all possible unique subsets from a set of distinct integers.\n " ,
114
+ " \n " ,
115
+ " **Code :**"
116
+ ]
117
+ },
118
+ {
119
+ "cell_type" : " code" ,
120
+ "metadata" : {
121
+ "id" : " osQTLZAZjM0k" ,
122
+ "colab" : {
123
+ "base_uri" : " https://localhost:8080/"
124
+ },
125
+ "outputId" : " 89059592-6967-4fe0-ba60-379a8dbbee40"
126
+ },
127
+ "source" : [
128
+ " class Subset:\n " ,
129
+ " def f1(self, s1):\n " ,
130
+ " return self.f2([], sorted(s1))\n " ,
131
+ " \n " ,
132
+ " def f2(self, curr, s1):\n " ,
133
+ " if s1:\n " ,
134
+ " return self.f2(curr, s1[1:]) + self.f2(curr + [s1[0]], s1[1:])\n " ,
135
+ " return [curr]\n " ,
136
+ " \n " ,
137
+ " \n " ,
138
+ " a = []\n " ,
139
+ " n = int(input(\" Enter number of elements in the list: \" ))\n " ,
140
+ " for i in range(0, n):\n " ,
141
+ " b = int(input(\" Enter an element: \" ))\n " ,
142
+ " a.append(b)\n " ,
143
+ " print(\"\\ nSubsets : \" )\n " ,
144
+ " print(Subset().f1(a))"
145
+ ],
146
+ "execution_count" : null ,
147
+ "outputs" : [
148
+ {
149
+ "output_type" : " stream" ,
150
+ "text" : [
151
+ " Enter number of elements in the list: 3\n " ,
152
+ " Enter an element: 4\n " ,
153
+ " Enter an element: 5\n " ,
154
+ " Enter an element: 6\n " ,
155
+ " \n " ,
156
+ " Subsets : \n " ,
157
+ " [[], [6], [5], [5, 6], [4], [4, 6], [4, 5], [4, 5, 6]]\n "
158
+ ],
159
+ "name" : " stdout"
160
+ }
161
+ ]
162
+ },
163
+ {
164
+ "cell_type" : " markdown" ,
165
+ "metadata" : {
166
+ "id" : " Sd-jQiTV1vMl"
167
+ },
168
+ "source" : [
169
+ " **Exception Handling**\n " ,
170
+ " \n " ,
171
+ " An interactive caluculator: Program reads an expression as input and has to\n " ,
172
+ " calculate the value of expression. The program throws an exception if the given\n " ,
173
+ " expression is not in expected format.\n " ,
174
+ " \n " ,
175
+ " **Code :**"
176
+ ]
177
+ },
178
+ {
179
+ "cell_type" : " code" ,
180
+ "metadata" : {
181
+ "colab" : {
182
+ "base_uri" : " https://localhost:8080/"
183
+ },
184
+ "id" : " XnkF_Y043Rw2" ,
185
+ "outputId" : " 429de4b8-2691-4fa2-b078-d30cbd153890"
186
+ },
187
+ "source" : [
188
+ " class Calculator:\n " ,
189
+ " def perform(str):\n " ,
190
+ " try:\n " ,
191
+ " if len(str) > 3:\n " ,
192
+ " raise ValueError(\" Please enter 2 operands and one operator only\" )\n " ,
193
+ " else:\n " ,
194
+ " val_1 = int(str[0])\n " ,
195
+ " op = str[1]\n " ,
196
+ " val_2 = int(str[2])\n " ,
197
+ " if op == '+':\n " ,
198
+ " ans = val_1 + val_2\n " ,
199
+ " elif op == '-':\n " ,
200
+ " ans = val_1 - val_2\n " ,
201
+ " elif op == '*':\n " ,
202
+ " ans = val_1 * val_2\n " ,
203
+ " elif op == '/'\n " ,
204
+ " ans = val_1 / val_2\n " ,
205
+ " elif op == '//'\n " ,
206
+ " ans = val_1 // val_2\n " ,
207
+ " print(ans)\n " ,
208
+ " except:\n " ,
209
+ " print(\"\\ nEntry of two operands and one operator is only allowed !\" )\n " ,
210
+ " \n " ,
211
+ " if __name__ == \" __main__\" :\n " ,
212
+ " expr = []\n " ,
213
+ " expr = input(\" Enter the expression : \" ) \n " ,
214
+ " Calculator.perform(expr)\n "
215
+ ],
216
+ "execution_count" : null ,
217
+ "outputs" : [
218
+ {
219
+ "output_type" : " stream" ,
220
+ "text" : [
221
+ " Enter the expression : 2 + 3 / 8\n " ,
222
+ " \n " ,
223
+ " Entry of two operands and one operator is only allowed!\n "
224
+ ],
225
+ "name" : " stdout"
226
+ }
227
+ ]
228
+ }
229
+ ]
230
+ }
0 commit comments