Array ADT


Submit solution

Points: 10 (partial)
Time limit: 100.0s
Memory limit: 12M

Authors:
Problem type
Allowed languages
C++, Java, Python

Build an ADT for the 'Array' data structure.

Please go through the template code (you can also find it during submission with locked parts) has three functions, viz maximum, minimum, and reverse, that are left unfilled. Please complete the function definitions and submit the code. The insert and delete functions are already filled in to give you an idea of how the storage works.

from __future__ import print_function

class Array(object):
    '''maxCapacity: denotes the maximum number that can be stored in array 
       sizeOfArray: denotes the number of elements currently in the array
       arrayType: denotes the data type of the array(as all the elements of the array have same data type)
       arrayItems: values at each position of array
    '''
    def __init__(self, maxC, arrayType = int):
        #self.sizeOfArray = len(list(map(arrayType, range(sizeOfArray))))
        self.maxCapacity = maxC
        self.arrayItems =[arrayType(-1)] * self.maxCapacity    # initialize array with zeroes
        self.arrayType = arrayType
        self.currentSize = 0

    # function to return the string representation of the array
    def __str__(self):
        return ' '.join([str(i) for i in self.arrayItems])

    # function to print the array
    def printArray(self):
        for i in range(self.currentSize):
            print(self.arrayItems[i], end=" ")
        print()

    # function for search
    def search(self, keyToSearch):
        for i in range(self.currentSize):
            if (self.arrayItems[i] == keyToSearch):      
                return i                                 
        return -1                                        

    # function for inserting an element    
    def insert(self, keyToInsert, position):
        index = position - 1  # Adjusting for zero-based index
        if(self.currentSize >= self.maxCapacity):
            return -1
        if(index < 0 or index > self.currentSize):
            return -2
        # Shift elements to the right to make space
        for i in range(self.currentSize, index, -1):
            self.arrayItems[i] = self.arrayItems[i - 1]
        self.arrayItems[index] = keyToInsert
        self.currentSize += 1
        return 0

    # function for deleting an element
    def delete(self, position):
        index = position - 1  # Adjusting for zero-based index
        if index < 0 or index >= self.currentSize:
            return -1
        # Shift elements to the left to fill the gap
        for i in range(index, self.currentSize - 1):
            self.arrayItems[i] = self.arrayItems[i + 1]
        # Set the last element to the default value
        self.arrayItems[self.currentSize - 1] = self.arrayType(-1)
        self.currentSize -= 1
        return 0

    # function to return the length of the array
    def length(self):
        return self.currentSize

    # function to return the maximum value in the array
    def maximum(self):
        #@start-editable@

        #@end-editable@
        return max_val

    # function to return the minimum value in the array
    def minimum(self):
        #@start-editable@

        #@end-editable@
        return min_val

    # function to reverse the array
    def reverse(self):
        #@start-editable@

        #@end-editable@


# Driver code.---------------------------------------------
def testArray():
    size = int(input())  # Size of the array
    a = Array(size, int)
    inputs = int(input())  # Number of operations to perform
    while inputs > 0:
        command = input()
        operation = command.split()

        if operation[0] == "L":
            print(a.length())

        elif operation[0] == "I":
            flag = a.insert(int(operation[1]), int(operation[2]))
            if flag == 0:
                a.printArray()
            elif flag == -1:
                print("Array is full")
            else:
                print("Invalid position")

        elif operation[0] == "D":
            flag = a.delete(int(operation[1]))
            if flag == 0:
                a.printArray()
            else:
                print("Invalid position")

        elif operation[0] == "Mx":
            max_value = a.maximum()
            if max_value is not None:
                print(max_value)
            else:
                print("Array is empty")

        elif operation[0] == "Mn":
            min_value = a.minimum()
            if min_value is not None:
                print(min_value)
            else:
                print("Array is empty")

        elif operation[0] == "R":
            a.reverse()
            a.printArray()

        inputs -= 1

def main():
    testArray()

if __name__ == '__main__':
    main()

Comments

There are no comments at the moment.