'array implementation using python from scratch [closed]

My teacher has asked me to implement an array using python without using any inbuilt functions but I am confused and I don't know how to? this is the complete question...

Write a program in Python to implement the “Array” data structure. Perform operations like add or insert, delete or remove and display. Your program should be able to add/insert at any position in an array or remove/delete any element from an array. Take care of extreme conditions such as an empty array or full array and display an appropriate message to the user.

any help would be highly appreciated.



Solution 1:[1]

You can store the array in a list L, and write a function for each list operation. For example, to search for an element x in a list L and return the index of the first occurrence of x in L, rather than using the built-in function index, you would implement the linear search algorithm. So, the following code would be incorrect because it uses the built-in function index:

def search(L,x):
    return L.index(x)

The following code would be acceptable because you are implementing the linear search algorithm yourself (presumably your teacher wants you to write programs from scratch, which is very good practice):

def search(L,x):
    #input: a list L and an element x
    #output: the index of first occurrence of x in L, or -1 if x not in L
    n = len(L)
    for i in range(n):
        if L[i] == x:
            return i
    return -1    

L=[3,1,4,2]
print(search(L,7))

    

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1