Skip to content

Commit 129b13f

Browse files
Day74
1 parent 101fc3e commit 129b13f

File tree

1 file changed

+276
-0
lines changed

1 file changed

+276
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# **Tuple and Sets Python Coding Questions**"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"### `Questions 1: Check is tuples are same or not?`\n",
15+
"Two tuples would be same if both tuples have same element at same index\n",
16+
"```\n",
17+
"t1 = (1,2,3,0)\n",
18+
"t2 = (0,1,2,3)\n",
19+
"\n",
20+
"t1 and t2 are not same\n",
21+
"```"
22+
]
23+
},
24+
{
25+
"cell_type": "code",
26+
"execution_count": 1,
27+
"metadata": {},
28+
"outputs": [
29+
{
30+
"name": "stdout",
31+
"output_type": "stream",
32+
"text": [
33+
"t1 and t2 are not same\n"
34+
]
35+
}
36+
],
37+
"source": [
38+
"t1 = (1,2,3,0)\n",
39+
"t2 = (0,1,2,3)\n",
40+
"\n",
41+
"# define function for same tuple\n",
42+
"\n",
43+
"def are_tuple_same(tuple1,tuple2):\n",
44+
" # check if the length of the tuple is same or not\n",
45+
" if len(tuple1)!=len(tuple2):\n",
46+
" return False\n",
47+
" \n",
48+
" # check if elements at corresponding indices are the same \n",
49+
" for i in range(len(tuple1)):\n",
50+
" if tuple1[i] != tuple2[i]:\n",
51+
" return False\n",
52+
" \n",
53+
" # tuples are same if all elemets are equal\n",
54+
" return True\n",
55+
"\n",
56+
"if are_tuple_same(t1,t2):\n",
57+
" print('t1 and t2 are the same')\n",
58+
" \n",
59+
"else:\n",
60+
" print('t1 and t2 are not same')"
61+
]
62+
},
63+
{
64+
"cell_type": "code",
65+
"execution_count": 2,
66+
"metadata": {},
67+
"outputs": [
68+
{
69+
"name": "stdout",
70+
"output_type": "stream",
71+
"text": [
72+
"t1 and t2 are the same\n"
73+
]
74+
}
75+
],
76+
"source": [
77+
"# EXAMPLE 2\n",
78+
"t1 = (1,2,3,4,5)\n",
79+
"t2 = (1,2,3,4,5)\n",
80+
"\n",
81+
"if are_tuple_same(t1,t2):\n",
82+
" print('t1 and t2 are the same')\n",
83+
" \n",
84+
"else:\n",
85+
" print('t1 and t2 are not same')\n",
86+
"\n"
87+
]
88+
},
89+
{
90+
"cell_type": "markdown",
91+
"metadata": {},
92+
"source": [
93+
"### `Question 2: Count no of tuples, list and set from a list`\n",
94+
"\n",
95+
"```list1 = [{'hi', 'bye'},{'Geeks', 'forGeeks'},('a', 'b'),['hi', 'bye'],['a', 'b']]```\n",
96+
"\n",
97+
"\n",
98+
"Output:\n",
99+
"\n",
100+
"```\n",
101+
"List-2\n",
102+
"Set-2\n",
103+
"Tuples-1\n",
104+
"```"
105+
]
106+
},
107+
{
108+
"cell_type": "code",
109+
"execution_count": 3,
110+
"metadata": {},
111+
"outputs": [
112+
{
113+
"name": "stdout",
114+
"output_type": "stream",
115+
"text": [
116+
"List-2\n",
117+
"Set-2\n",
118+
"Tuples-1\n"
119+
]
120+
}
121+
],
122+
"source": [
123+
"list1 = [{'hi', 'bye'}, {'Geeks', 'forGeeks'}, ('a', 'b'), ['hi', 'bye'], ['a', 'b']]\n",
124+
"\n",
125+
"# Initialize counters\n",
126+
"tuple_count = 0\n",
127+
"list_count = 0\n",
128+
"set_count = 0\n",
129+
"\n",
130+
"# Iterate through the elements in the list\n",
131+
"for elem in list1:\n",
132+
" if isinstance(elem, tuple):\n",
133+
" tuple_count += 1\n",
134+
" elif isinstance(elem, list):\n",
135+
" list_count += 1\n",
136+
" elif isinstance(elem, set):\n",
137+
" set_count += 1\n",
138+
"\n",
139+
"# Print the counts\n",
140+
"print(f\"List-{list_count}\")\n",
141+
"print(f\"Set-{set_count}\")\n",
142+
"print(f\"Tuples-{tuple_count}\")\n"
143+
]
144+
},
145+
{
146+
"cell_type": "markdown",
147+
"metadata": {},
148+
"source": [
149+
"### `Question 3: Write a program to find set of common elements in three lists using sets.`\n",
150+
"```\n",
151+
"Input : ar1 = [1, 5, 10, 20, 40, 80]\n",
152+
" ar2 = [6, 7, 20, 80, 100]\n",
153+
" ar3 = [3, 4, 15, 20, 30, 70, 80, 120]\n",
154+
"\n",
155+
"Output : [80, 20]\n",
156+
"```"
157+
]
158+
},
159+
{
160+
"cell_type": "code",
161+
"execution_count": 4,
162+
"metadata": {},
163+
"outputs": [
164+
{
165+
"name": "stdout",
166+
"output_type": "stream",
167+
"text": [
168+
"Output: [80, 20]\n"
169+
]
170+
}
171+
],
172+
"source": [
173+
"arr1 = [1,5,10,20,40,80]\n",
174+
"arr2 = [6,7,20,80,100]\n",
175+
"arr3 = [3,4,15,20,30,70,80,120]\n",
176+
"\n",
177+
"# convert list into sets\n",
178+
"set1 = set(arr1)\n",
179+
"set2 = set(arr2)\n",
180+
"set3 = set(arr3)\n",
181+
"\n",
182+
"# Find the common elements using set intersection\n",
183+
"common_elements = set1.intersection(set2, set3)\n",
184+
"\n",
185+
"# Convert the result back to a list\n",
186+
"result_list = list(common_elements)\n",
187+
"\n",
188+
"# Output\n",
189+
"print(\"Output:\", result_list)"
190+
]
191+
},
192+
{
193+
"cell_type": "markdown",
194+
"metadata": {},
195+
"source": [
196+
"### `Question 4: find union of n arrays.`\n",
197+
"\n",
198+
"**Example 1:**\n",
199+
"\n",
200+
"Input:\n",
201+
"```bash\n",
202+
"[[1, 2, 2, 4, 3, 6],\n",
203+
" [5, 1, 3, 4],\n",
204+
" [9, 5, 7, 1],\n",
205+
" [2, 4, 1, 3]]\n",
206+
"```\n",
207+
"\n",
208+
"Output:\n",
209+
"\n",
210+
"```bash\n",
211+
"[1, 2, 3, 4, 5, 6, 7, 9]\n",
212+
"```"
213+
]
214+
},
215+
{
216+
"cell_type": "code",
217+
"execution_count": 5,
218+
"metadata": {},
219+
"outputs": [
220+
{
221+
"name": "stdout",
222+
"output_type": "stream",
223+
"text": [
224+
"Output: [1, 2, 3, 4, 5, 6, 7, 9]\n"
225+
]
226+
}
227+
],
228+
"source": [
229+
"def find_union(arrays):\n",
230+
" # Initialize an empty set for the union\n",
231+
" union_set = set()\n",
232+
"\n",
233+
" # Iterate through each array and update the union set\n",
234+
" for array in arrays:\n",
235+
" union_set.update(array)\n",
236+
"\n",
237+
" # Convert the set to a list (if needed)\n",
238+
" union_list = list(union_set)\n",
239+
"\n",
240+
" return union_list\n",
241+
"\n",
242+
"# Example usage\n",
243+
"input_arrays = [\n",
244+
" [1, 2, 2, 4, 3, 6],\n",
245+
" [5, 1, 3, 4],\n",
246+
" [9, 5, 7, 1],\n",
247+
" [2, 4, 1, 3]\n",
248+
"]\n",
249+
"\n",
250+
"output_union = find_union(input_arrays)\n",
251+
"print(\"Output:\", output_union)"
252+
]
253+
}
254+
],
255+
"metadata": {
256+
"kernelspec": {
257+
"display_name": "base",
258+
"language": "python",
259+
"name": "python3"
260+
},
261+
"language_info": {
262+
"codemirror_mode": {
263+
"name": "ipython",
264+
"version": 3
265+
},
266+
"file_extension": ".py",
267+
"mimetype": "text/x-python",
268+
"name": "python",
269+
"nbconvert_exporter": "python",
270+
"pygments_lexer": "ipython3",
271+
"version": "3.9.18"
272+
}
273+
},
274+
"nbformat": 4,
275+
"nbformat_minor": 2
276+
}

0 commit comments

Comments
 (0)