Skip to content

Commit bb71240

Browse files
committed
Python Implementation of Array
1 parent d25e094 commit bb71240

File tree

1 file changed

+260
-0
lines changed

1 file changed

+260
-0
lines changed

Arrays/Arrays.ipynb

Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"#### Author: OMKAR PATHAK"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"# Arrays"
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {},
20+
"source": [
21+
"## What is an Array?"
22+
]
23+
},
24+
{
25+
"cell_type": "markdown",
26+
"metadata": {},
27+
"source": [
28+
"* Array is a data structure used to store homogeneous elements at contiguous locations.\n",
29+
"* One memory block is allocated for the entire array Lo hold the clements of the arTny. The array elements can be accessed in constant time by using the index of the parliculnr element as the subscript."
30+
]
31+
},
32+
{
33+
"cell_type": "markdown",
34+
"metadata": {},
35+
"source": [
36+
"## Properties of Arrays:"
37+
]
38+
},
39+
{
40+
"cell_type": "markdown",
41+
"metadata": {},
42+
"source": [
43+
"* Arrays stores similar data types. That is, array can hold data of same data type values. This is one of the limitations of arrays compared to other data structures.\n",
44+
"\n",
45+
"* Each value stored, in an array, is known as an element and all elements are indexed. The first element added, by default, gets 0 index. That is, the 5th element added gets an index number of 4.\n",
46+
"\n",
47+
"* Elements can be retrieved by their index number. (__random access__)\n",
48+
"\n",
49+
"* Array elements are stored in contiguous (continuous) memory locations.\n",
50+
"\n",
51+
"* One array name can represent multiple values. Array is the easiest way to store a large quantity of data of same data types. For example, to store the salary of 100 employees, it is required to declare 100 variables. But with arrays, with one array name all the 100 employees salaries can be stored.\n",
52+
"\n",
53+
"* At the time of creation itself, array size should be declared (array initialization does not require size)."
54+
]
55+
},
56+
{
57+
"cell_type": "markdown",
58+
"metadata": {},
59+
"source": [
60+
"## Arrays in Python:"
61+
]
62+
},
63+
{
64+
"cell_type": "markdown",
65+
"metadata": {},
66+
"source": [
67+
"Python does not have a native support for arrays, but has a more generic data structure called LIST. List provides all the options as array with more functionality.\n",
68+
"But with few tweaks we can implement Array data structure in Python.\n",
69+
"We will be seeing how to do this."
70+
]
71+
},
72+
{
73+
"cell_type": "markdown",
74+
"metadata": {},
75+
"source": [
76+
"### Creating an array:"
77+
]
78+
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": 58,
82+
"metadata": {},
83+
"outputs": [
84+
{
85+
"name": "stdout",
86+
"output_type": "stream",
87+
"text": [
88+
"0 0 0 0 0 0 0 0 0 0\n"
89+
]
90+
}
91+
],
92+
"source": [
93+
"class Array(object):\n",
94+
" ''' sizeOfArray: denotes the total size of the array to be initialized\n",
95+
" arrayType: denotes the data type of the array(as all the elements of the array have same data type)\n",
96+
" arrayItems: values at each position of array\n",
97+
" '''\n",
98+
" def __init__(self, sizeOfArray, arrayType = int):\n",
99+
" self.sizeOfArray = len(list(map(arrayType, range(sizeOfArray))))\n",
100+
" self.arrayItems =[arrayType(0)] * sizeOfArray # initialize array with zeroes\n",
101+
" \n",
102+
" def __str__(self):\n",
103+
" return ' '.join([str(i) for i in self.arrayItems])\n",
104+
" \n",
105+
" # function for search\n",
106+
" def search(self, keyToSearch):\n",
107+
" for i in range(self.sizeOfArray):\n",
108+
" if (self.arrayItems[i] == keyToSearch): # brute-forcing\n",
109+
" return i # index at which element/ key was found\n",
110+
" \n",
111+
" return -1 # if key not found, return -1\n",
112+
" \n",
113+
" # function for inserting an element\n",
114+
" def insert(self, keyToInsert, position):\n",
115+
" if(self.sizeOfArray > position):\n",
116+
" for i in range(self.sizeOfArray - 2, position - 1, -1):\n",
117+
" self.arrayItems[i + 1] = self.arrayItems[i]\n",
118+
" self.arrayItems[position] = keyToInsert\n",
119+
" else:\n",
120+
" print('Array size is:', self.sizeOfArray)\n",
121+
" \n",
122+
" # function to delete an element\n",
123+
" def delete(self, keyToDelete, position):\n",
124+
" if(self.sizeOfArray > position):\n",
125+
" for i in range(position, self.sizeOfArray - 1):\n",
126+
" self.arrayItems[i] = self.arrayItems[i + 1]\n",
127+
" else:\n",
128+
" print('Array size is:', self.sizeOfArray)\n",
129+
" \n",
130+
"a = Array(10, int)\n",
131+
"print(a)"
132+
]
133+
},
134+
{
135+
"cell_type": "markdown",
136+
"metadata": {},
137+
"source": [
138+
"### Common array operations:"
139+
]
140+
},
141+
{
142+
"cell_type": "markdown",
143+
"metadata": {},
144+
"source": [
145+
"* Search\n",
146+
"* Insert\n",
147+
"* Delete"
148+
]
149+
},
150+
{
151+
"cell_type": "markdown",
152+
"metadata": {},
153+
"source": [
154+
"### Search Operation on Array:"
155+
]
156+
},
157+
{
158+
"cell_type": "code",
159+
"execution_count": 16,
160+
"metadata": {},
161+
"outputs": [
162+
{
163+
"name": "stdout",
164+
"output_type": "stream",
165+
"text": [
166+
"Element found at: 0\n"
167+
]
168+
}
169+
],
170+
"source": [
171+
"a = Array(10, int)\n",
172+
"index = a.search(0)\n",
173+
"print('Element found at:', index)"
174+
]
175+
},
176+
{
177+
"cell_type": "markdown",
178+
"metadata": {
179+
"collapsed": true
180+
},
181+
"source": [
182+
"### Insert Operation:"
183+
]
184+
},
185+
{
186+
"cell_type": "code",
187+
"execution_count": 46,
188+
"metadata": {},
189+
"outputs": [
190+
{
191+
"name": "stdout",
192+
"output_type": "stream",
193+
"text": [
194+
"0 0 1 2 3 0 0 0 0 0\n"
195+
]
196+
}
197+
],
198+
"source": [
199+
"a = Array(10, int)\n",
200+
"a.insert(1, 2)\n",
201+
"a.insert(2,3)\n",
202+
"a.insert(3,4)\n",
203+
"print(a)"
204+
]
205+
},
206+
{
207+
"cell_type": "markdown",
208+
"metadata": {},
209+
"source": [
210+
"### Delete Operation:"
211+
]
212+
},
213+
{
214+
"cell_type": "code",
215+
"execution_count": 63,
216+
"metadata": {},
217+
"outputs": [
218+
{
219+
"name": "stdout",
220+
"output_type": "stream",
221+
"text": [
222+
"0 0 1 2 0 0 0 0 0 0\n",
223+
"Element found at: 2\n"
224+
]
225+
}
226+
],
227+
"source": [
228+
"a = Array(10, int)\n",
229+
"a.insert(1, 2)\n",
230+
"a.insert(2,3)\n",
231+
"a.insert(3,4)\n",
232+
"a.delete(3, 4)\n",
233+
"print(a)\n",
234+
"index = a.search(1)\n",
235+
"print('Element found at:',index)"
236+
]
237+
}
238+
],
239+
"metadata": {
240+
"kernelspec": {
241+
"display_name": "Python 3",
242+
"language": "python",
243+
"name": "python3"
244+
},
245+
"language_info": {
246+
"codemirror_mode": {
247+
"name": "ipython",
248+
"version": 3
249+
},
250+
"file_extension": ".py",
251+
"mimetype": "text/x-python",
252+
"name": "python",
253+
"nbconvert_exporter": "python",
254+
"pygments_lexer": "ipython3",
255+
"version": "3.5.2"
256+
}
257+
},
258+
"nbformat": 4,
259+
"nbformat_minor": 2
260+
}

0 commit comments

Comments
 (0)