-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSection 3 - Arrays.py
158 lines (118 loc) · 3.63 KB
/
Section 3 - Arrays.py
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
'''
1.Array:
- can store data of specified type
- elements of an array are located in a contiguous location in memory(no gap)
- each element has unique index
- homogenious data type
2.Types of array:
2.1. One Dimensional: an array with a bunch of values having been declared with a single index
- access by index: a[i]
2.2. Multi Dimensional
- two dimensional array a[i][j] -> a[row][column]
- three dimensional array a[i][j][k] -> a[depth][row][column] - in the memory it is represented as 1-dimensional arr
3.Insertion to Array:
- beginning of arr: time-consuming
'''
import array
my_array = array.array('i') #------> 0(1)
print(my_array)
my_array1 = array.array('i', [1, 2, 3, 4]) #----> O(n)
print(my_array1)
my_array1.insert(0, 6) #----> O(n) (Time); O(1)(Space); depends on the number of elements that need to be shifted right
print(my_array1)
my_array1.remove(3) #remove by element O(1) for the last el, O(n) for all other elements time complexity
print(my_array1)
def traverse_array(array):
for el in array: #--------> O(n)
print(el) #-----------> O(1)
def access_element(array, index):
if index >= len(array):
print('Error of index')
else:
print(array[index])
def linear_search(arr, target): #----> O(n)
for i in range(len(arr)-1):
if arr[i] == target:
return i
return 'Error of index'
access_element([1, 2, 3], 0)
access_element([1, 2, 3], 2)
access_element([1, 2, 3], 3)
import numpy as np
np_array = np.array([], dtype=int) #------> 0(1)
print(np_array)
np_array1 = np.array([1, 2, 3, 4]) #----> O(n)
print(np_array1)
print('*'*20)
'''
One-Dimensional Array Practice
'''
from array import *
print('Step 1')
arr = array('i', [1, 2, 3, 4, 5])
for el in arr:
print(el)
print('Step 2')
print(arr[0])
print('Step 3')
arr.append(6)
print(arr)
print('Step 4')
arr.insert(0, 11)
print(arr)
print('Step 5')
new_arr = array('i', [10, 11, 12])
arr.extend(new_arr)
print(arr)
print('Step 6')
temp_list = [21, 22, 23]
arr.fromlist(temp_list)
print(arr)
print('Step 7')
arr.remove(22) #remove deletes the first occurance of 22 - O(n)
print(arr)
print('Step 8')
arr.pop()
print(arr)
print('Step 9')
print(f"The index of number 21 in the array is: {arr.index(21)}")
print('Step 10')
arr.reverse()
print(arr)
print('Step 11')
print(arr.buffer_info())
print('Step 12')
print('Count returns the number of occurances of sth. in the array ')
print(arr.count(11))
print('Step 13')
str_temp = arr.tostring()
print(str_temp)
print('Step 14')
print(arr[1:4]) #works exclusive last i
print('Step 15')
print(arr.tolist())
'''
Two Dimensional Array
'''
two_d_array = np.array([[11, 15, 10, 6], [10, 14, 11, 5], [12, 17, 12, 8], [15, 18, 14, 9]]) #-----> O(m,n)
print(two_d_array)
new_two_d_array = np.insert(two_d_array, 0, [[1, 2, 3, 5]], axis=1)
print(new_two_d_array)
new_array = np.append(two_d_array, [[1, 1, 1, 1]], axis=0)
print(new_array)
def access_elements(array_to_search_in, row_index, column_index):
if row_index >= len(array_to_search_in) or column_index >= len(array_to_search_in[0]):
print('Incorrect index')
else:
print(array_to_search_in[row_index][column_index])
def traverse_t_d_array(array):
for i in range(len(array)):
for j in range(len(array[0])):
print(array[i][j])
def search_t_d_array(array, value):
for i in range(len(array)):
for j in range(len(array[0])):
if array[i][j] == value:
return f'The value is located at index {[i][j]}'
return 'The element is not found'
new_t_d_array = np.delete(two_d_array, 0, axis=0) # a new array is created in the memory