Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a2dcb5b

Browse files
Arrays
1 parent de6c4be commit a2dcb5b

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed

‎Arrays.ipynb

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "f17262c8",
7+
"metadata": {},
8+
"outputs": [
9+
{
10+
"name": "stdout",
11+
"output_type": "stream",
12+
"text": [
13+
"2\n"
14+
]
15+
}
16+
],
17+
"source": [
18+
"#First index of element\n",
19+
"def first_index(arr,x):\n",
20+
" res = -1\n",
21+
" for i in range(len(arr)):\n",
22+
" if arr[i]==x:\n",
23+
" res=i\n",
24+
" break\n",
25+
" return res\n",
26+
"print(first_index([7,5,2,11,2,43,1,1],2))"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": 2,
32+
"id": "ba4b3c39",
33+
"metadata": {},
34+
"outputs": [
35+
{
36+
"name": "stdout",
37+
"output_type": "stream",
38+
"text": [
39+
"4\n"
40+
]
41+
}
42+
],
43+
"source": [
44+
"#Last index of element\n",
45+
"def last_index(arr,x):\n",
46+
" res = -1\n",
47+
" for i in range(len(arr)-1,-1,-1):\n",
48+
" if arr[i]==x:\n",
49+
" res=i\n",
50+
" break\n",
51+
" return res\n",
52+
"print(last_index([7,5,2,11,2,43,1,1],2))"
53+
]
54+
},
55+
{
56+
"cell_type": "code",
57+
"execution_count": 3,
58+
"id": "08958bb9",
59+
"metadata": {},
60+
"outputs": [
61+
{
62+
"name": "stdout",
63+
"output_type": "stream",
64+
"text": [
65+
"[7, 5, 2, 11, 2, 43, 1, 10]\n"
66+
]
67+
}
68+
],
69+
"source": [
70+
"#Reverse the array\n",
71+
"def array_reverse(arr):\n",
72+
" return arr[::-1]\n",
73+
"\n",
74+
"print(array_reverse([10, 1, 43, 2 ,11, 2, 5 ,7]))"
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": 4,
80+
"id": "622a1696",
81+
"metadata": {},
82+
"outputs": [
83+
{
84+
"name": "stdout",
85+
"output_type": "stream",
86+
"text": [
87+
"[2, 11, 2, 43, 1, 1, 7, 5]\n"
88+
]
89+
}
90+
],
91+
"source": [
92+
"#Rotate array\n",
93+
"def rotate_array(arr,k):\n",
94+
" res = arr[k:]+arr[:k]\n",
95+
" return res\n",
96+
"\n",
97+
"print(rotate_array([7, 5, 2 ,11 ,2 ,43, 1, 1],2))"
98+
]
99+
},
100+
{
101+
"cell_type": "code",
102+
"execution_count": 5,
103+
"id": "2c548fd9",
104+
"metadata": {},
105+
"outputs": [
106+
{
107+
"name": "stdout",
108+
"output_type": "stream",
109+
"text": [
110+
"(5, 12)\n"
111+
]
112+
}
113+
],
114+
"source": [
115+
"#K'th smallest/largest element in an array\n",
116+
"def kelement(arr,k):\n",
117+
" arr.sort()\n",
118+
" if len(arr)>=k:\n",
119+
" r1 = arr[k-1] #smallest\n",
120+
" r2 = arr[-k] #largest\n",
121+
" return r1,r2\n",
122+
"\n",
123+
"print(kelement([12, 3, 5, 7, 19],2))"
124+
]
125+
}
126+
],
127+
"metadata": {
128+
"kernelspec": {
129+
"display_name": "Python 3.9.7 64-bit",
130+
"language": "python",
131+
"name": "python3"
132+
},
133+
"language_info": {
134+
"codemirror_mode": {
135+
"name": "ipython",
136+
"version": 3
137+
},
138+
"file_extension": ".py",
139+
"mimetype": "text/x-python",
140+
"name": "python",
141+
"nbconvert_exporter": "python",
142+
"pygments_lexer": "ipython3",
143+
"version": "3.9.7"
144+
},
145+
"vscode": {
146+
"interpreter": {
147+
"hash": "0d591c6e422414675974e227c13f5382000c440fedd3c5006ef2be5d887f0ba7"
148+
}
149+
}
150+
},
151+
"nbformat": 4,
152+
"nbformat_minor": 5
153+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /