- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.py
More file actions
Latest commit
106 lines (95 loc) · 4.21 KB
/
Copy pathArray.py
File metadata and controls
106 lines (95 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#This example illustrates how an array can be implemened using Python
classArray(object):
def__init__(self, size, defaultValue=None):
'''
size: indicates the static size of the Array
defaultValue indicates the default value that Array takes while creation, you can also
pass preinitialized list to set the values of the array elements
'''
self.size=size
# If only array size is initialized then, initialize all the elements as None type
if(defaultValue==None):
self.items=list()
foriinrange(size):
self.items.append(defaultValue)
else:
# If user has given the default values for the array
self.items=list()
if(len(defaultValue) ==sizeorlen(defaultValue) <size):
forjinrange(len(defaultValue)):
if(defaultValue[j]):
self.items.append(defaultValue[j])
foriinrange(len(defaultValue), size):
self.items.append(None)
else:
print('Elements are more than the size specified')
defmyLen(self):
''' This function returns the length of the Array (Only initialised number of elements)'''
length=0
foriinself.items:
ifi==None:
continue
else:
length+=1
returnlength
definsertFirst(self, element):
''' This function adds the element to the beginning of the array '''
if (self.myLen() <self.size):
foriinrange(self.myLen(), 0, -1):
self.items[i] =self.items[i-1]
self.items[0] =element
else:
print('Element index out of range')
definsertAtIndex(self, index, element):
''' This function adds the element to the beginning of the array '''
if (self.myLen() <self.size):
foriinrange(self.myLen(), index, -1):
self.items[i] =self.items[i-1]
self.items[index] =element
else:
print('Element index out of range')
definsertAfterIndex(self, index, element):
''' This function adds the element to the beginning of the array '''
if (self.myLen() <self.size):
foriinrange(self.myLen(), index+1, -1):
self.items[i] =self.items[i-1]
self.items[index+1] =element
else:
print('Element index out of range')
definsertBeforeIndex(self, index, element):
''' This function adds the element to the beginning of the array '''
if (self.myLen() <self.size):
foriinrange(self.myLen(), index-1, -1):
self.items[i] =self.items[i-1]
self.items[index-1] =element
else:
print('Element index out of range')
defdelete(self, element):
ifelementinself.items:
Index=self.items.index(element)
self.items[Index] =None
else:
print('This element is not in the Array!')
defsearch(self, element):
ifelementinself.items:
position=0
foriinrange(self.myLen()):
if(self.items[i] ==element):
break
else:
position+=1
print('Element {} found at position {}'.format(element, position))
else:
print('This element is not in the Array!')
if__name__=='__main__':
myArray=Array(5, [1])
print(myArray.items, myArray.myLen()) # [1, None, None, None, None] 1
myArray.insertFirst(3)
print(myArray.items, myArray.myLen()) # [3, 1, None, None, None] 2
myArray.insertAfterIndex(1,4)
print(myArray.items, myArray.myLen()) # [3, 1, 4, None, None] 3
myArray.insertBeforeIndex(3,5)
print(myArray.items, myArray.myLen()) # [3, 1, 5, 4, None] 4
myArray.delete(5)
print(myArray.items, myArray.myLen()) # [3, 1, None, 4, None] 3
myArray.search(4) # Element 4 found at position 3