0% found this document useful (0 votes)
9 views5 pages

LECTURE 1 ArrayUtils - Instance - Methods

Uploaded by

ishmam omi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

LECTURE 1 ArrayUtils - Instance - Methods

Uploaded by

ishmam omi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

ArrayUtils Methods (Instance-based)

with Explanations
1. iterate(self, arr)
This method prints each element of the given array on the same line, separated by spaces.

import numpy as np

class ArrayUtils:
def iterate(self, arr):
for element in arr:
print(element, end=" ")
print()

# Example usage
utils = ArrayUtils()
array = np.array([1, 2, 3])
utils.iterate(array)

2. resize(self, arr, new_size)


This method resizes the array to a new size, padding with zeros if the new size is larger.

import numpy as np

class ArrayUtils:
def resize(self, arr, new_size):
resized = np.zeros(new_size, dtype=arr.dtype)
for i in range(min(len(arr), new_size)):
resized[i] = arr[i]
return resized

# Example usage
utils = ArrayUtils()
array = np.array([1, 2, 3])
resized = utils.resize(array, 6)
print(resized)

3. copy_array(self, arr)
This method creates a copy of the array by manually copying each element.

import numpy as np
class ArrayUtils:
def copy_array(self, arr):
copy = np.zeros_like(arr)
for i in range(len(arr)):
copy[i] = arr[i]
return copy

# Example usage
utils = ArrayUtils()
array = np.array([1, 2, 3])
copied = utils.copy_array(array)
print(copied)

4. shift_left(self, arr)
This method shifts all elements of the array one position to the left, inserting 0 at the end.

import numpy as np

class ArrayUtils:
def shift_left(self, arr):
for i in range(len(arr) - 1):
arr[i] = arr[i + 1]
arr[-1] = 0

# Example usage
utils = ArrayUtils()
array = np.array([1, 2, 3, 4])
utils.shift_left(array)
print(array)

5. shift_right(self, arr)
This method shifts all elements of the array one position to the right, inserting 0 at the
beginning.

import numpy as np

class ArrayUtils:
def shift_right(self, arr):
for i in range(len(arr) - 1, 0, -1):
arr[i] = arr[i - 1]
arr[0] = 0

# Example usage
utils = ArrayUtils()
array = np.array([1, 2, 3, 4])
utils.shift_right(array)
print(array)
6. rotate_left(self, arr)
This method rotates the array to the left, moving the first element to the last position.

import numpy as np

class ArrayUtils:
def rotate_left(self, arr):
first = arr[0]
for i in range(len(arr) - 1):
arr[i] = arr[i + 1]
arr[-1] = first

# Example usage
utils = ArrayUtils()
array = np.array([10, 20, 30])
utils.rotate_left(array)
print(array)

7. rotate_right(self, arr)
This method rotates the array to the right, moving the last element to the first position.

import numpy as np

class ArrayUtils:
def rotate_right(self, arr):
last = arr[-1]
for i in range(len(arr) - 1, 0, -1):
arr[i] = arr[i - 1]
arr[0] = last

# Example usage
utils = ArrayUtils()
array = np.array([10, 20, 30])
utils.rotate_right(array)
print(array)

8. reverse_out_of_place(self, arr)
This method returns a new array that is the reverse of the input array.

import numpy as np

class ArrayUtils:
def reverse_out_of_place(self, arr):
reversed_array = np.zeros_like(arr)
for i in range(len(arr)):
reversed_array[i] = arr[len(arr) - 1 - i]
return reversed_array

# Example usage
utils = ArrayUtils()
array = np.array([1, 2, 3])
reversed_arr = utils.reverse_out_of_place(array)
print("Original:", array)
print("Reversed:", reversed_arr)

9. reverse_in_place(self, arr)
This method reverses the array in place by swapping elements from the ends inward.

import numpy as np

class ArrayUtils:
def reverse_in_place(self, arr):
left, right = 0, len(arr) - 1
while left < right:
arr[left], arr[right] = arr[right], arr[left]
left += 1
right -= 1

# Example usage
utils = ArrayUtils()
array = np.array([1, 2, 3])
utils.reverse_in_place(array)
print(array)

10. insert(self, arr, index, value, current_size)


This method inserts a value into the array at the specified index, resizing if necessary.

import numpy as np

class ArrayUtils:
def resize(self, arr, new_size):
resized = np.zeros(new_size, dtype=arr.dtype)
for i in range(min(len(arr), new_size)):
resized[i] = arr[i]
return resized

def insert(self, arr, index, value, current_size):


if index < 0 or index > current_size:
raise IndexError("Invalid index")
if current_size == len(arr):
arr = self.resize(arr, len(arr) * 2)
for i in range(current_size, index, -1):
arr[i] = arr[i - 1]
arr[index] = value
return arr

# Example usage
utils = ArrayUtils()
array = np.array([1, 2, 4, 5, 0, 0], dtype=int)
current_size = 4
array = utils.insert(array, 2, 3, current_size)
print(array)

11. delete(self, arr, index, current_size)


This method deletes an element from the specified index, shifting all elements after it to the
left.

import numpy as np

class ArrayUtils:
def delete(self, arr, index, current_size):
if index < 0 or index >= current_size:
raise IndexError("Invalid index")
for i in range(index, current_size - 1):
arr[i] = arr[i + 1]
arr[current_size - 1] = 0

# Example usage
utils = ArrayUtils()
array = np.array([1, 2, 3, 4, 5, 0], dtype=int)
current_size = 5
utils.delete(array, 2, current_size)
print(array)

You might also like