Array Part 2 Operations
Array Part 2 Operations
1.2. All the non-dummy values will be at the left followed by the dummy values (zero/None/null)
1 import numpy as np
2 arr = np.array([0]*5)
3 print(arr)
4 print(type(arr))
5
6 arr1 = np.array([1, 2, 3, 4])
7 print(arr1)
8 print(type(arr1))
9
10 arr2 = np.zeros(6, dtype=int)
11 print(arr2)
12 print(type(arr2))
[0 0 0 0 0]
<class 'numpy.ndarray'>
[1 2 3 4]
<class 'numpy.ndarray'>
[0 0 0 0 0 0]
<class 'numpy.ndarray'>
3. Array Operations
3.1 Iteration
3.2 Resize
3.1 Iteration
Iteration refers to checking all the values index by index. The main idea is to go to that memory location and check all the values index by index.
3.2 Resize
We can not resize an array because it has a fixed memory location. However, if we ever need to resize an array, we need to create a new array
with a new length and then copy the values from the original array. For example, if we have an array [10, 20, 30, 40, 50] whose length is 5 and
want to resize the array with length 8. The new array will be [10, 20, 30, 40, 50, None, None, None].
1 import numpy as np
2
3 def resizeArray(arr, new_size):
4 arr2= np.array([0]* new_size)
5 for i in range(len(arr)):
6 arr2[i]= arr[i]
7 return arr2
8
9 arr1= np.array([1,2,3,4])
10 print(resizeArray(arr1, 6))
11 # x= resizeArray(arr1, 6)
[1 2 3 4 0 0]
1 arr1= np.array([1,2,3,4])
2 arr2= arr1 #pass by reference
3 print(id(arr1)) #id() function is used to find the memory location
4 print(id(arr2))
139056372101744
139056372101744
1 def copyArray(arr):
2 arr2= np.array([0]* len(arr))
3 for i in range(len(arr)):
4 arr2[i]= arr[i]
5 return arr2
6
7 arr1= np.array([1,2,3,4])
8 arr2= copyArray(arr1)
9 # arr2= arr1.copy()
10 print(id(arr1))
11 print(id(arr2))
136491905968784
136491905966672
1 def shiftLeft(arr):
2 for i in range(1, len(arr), 1):
3 arr[i-1]= arr[i]
4 arr[len(arr)-1]= 0
5 return arr
6
7 arr= np.array([1,2,3,4])
8 print(shiftLeft(arr))
[2 3 4 0]
Shifting an Array Right: Shifting an entire array right moves each element one (or more, depending how the shift amount) position to the right.
Obviously, the last element in the array will fall off at the end and be lost forever. The first slot of the array before the shift (ie., the slot where the
first element was until the shift) is now unused (we can put a None there to signify that). The size of the array remains the same however
because the assumption is that you would something in the now-unused slot. For example, shifting the array [5, 3, 9, 13, 2] right by one position
will result in the array [None, 5, 3, 9, 13]. Note how the array[4] element with the value of 2 is now lost, and there is an empty slot at the
beginning.
1 def shiftRight(arr):
2 for i in range(len(arr)-1, 0, -1):
3 arr[i]= arr[i-1]
4 arr[0]=0
5 return arr
6
7 arr= np.array([1,2,3,4])
8 print(shiftRight(arr))
[0 1 2 3]
Rotating an Array Right: Rotating an array right is equivalent to shifting a circular or cyclic array right where the last element will not be lost, but
rather move to the 1st slot. Rotating the array [5, 3, 9, 13, 2] right by one position will result in the array [2, 5, 3, 9, 13].
1 def rotateRight(arr):
2 temp= arr[len(arr)-1]
3 for i in range(len(arr)-1, 0, -1):
4 arr[i]= arr[i-1]
5 arr[0]= temp
6 return arr
7
8 arr= np.array([1,2,3,4])
9 print(rotateRight(arr))
[4 1 2 3]
However, an efficient approach might be to reverse the array in the original array. By this, we will not need to allocate extra spaces. This is
known as an in-place operation. To do so we need to start swapping values from the beginning position to the end position. The idea is to swap
starting value with the end value, then the second value with the second last value, and so on.
1 def reverse_out_of_place(arr):
2 arr2= np.zeros(len(arr), dtype=int)
3 i= 0
4 while(i<=len(arr)-1):
5 arr2[i]= arr[len(arr)-1-i]
6 i+=1
7
8 return arr2
9
10 arr= np.array([1,2,3,4])
11 print(reverse_out_of_place(arr))
12 arr= np.array([1,2,3,4,5])
13 print(reverse_out_of_place(arr))
[4 3 2 1]
[5 4 3 2 1]
1 def reverse_in_place(arr):
2 x, j = 0, len(arr)-1
3 for i in range(len(arr)//2):
4 arr[i], arr[j]= arr[j], arr[i]
5 x+=0
6 j-=1
7 return arr
8
9 arr= np.array([1,2,3,4])
10 print(reverse_in_place(arr))
11 arr= np.array([1,2,3,4,5])
12 print(reverse_in_place(arr))
[4 3 2 1]
[5 4 3 2 1]
1 #Complete the function print_non_dummies that prints all the non-dummy values of an array
2 import numpy as np
3
4 def print_non_dummies(arr, size):
5 for i in range(0, size):
6 print(arr[i])
7
8 arr= np.zeros(6, dtype=int)
9 arr[0], arr[1], arr[2] = 4, 0, 5
10 print(arr)
11 print_non_dummies(arr, 3) #array, 3
[4 0 5 0 0 0]
4
0
5
1 #Inserting anywhere
2 def insert_anywhere(arr, size, index, elem):
3 if index<0 or index>size:
4 return "Insertion Not Possible"
5
6 if size >= len(arr):
7 arr= resizeArray(arr, len(arr)+3)
8
9 for i in range(size, index, -1): #Right Shift
10 arr[i]= arr[i-1]
11
12 arr[index]= elem
13 return arr
14
15 arr= np.zeros(4, dtype=int)
16 arr[0], arr[1], arr[2] = 4, 6, 5
17 print(arr)
18 print(insert_anywhere(arr, 3, 1, 15)) #array, size, index, elem
19 print(insert_anywhere(arr, 4, 4, 25)) #array, size, index, elem, the size increased
20
21 # If you want to insert at the end with this function too,
22 # all you have to do is provide the index value equal to the size
[4 6 5 0]
[ 4 15 6 5]
[ 4 15 6 5 25 0 0]
output True
True
False
Function Call:
Output:
5231
Function Call:
Output:
[1 5 3 5 2]