-1

Trying to make a function to print all the arrays that are dynamically stored inside. But I'm not able to make a function to print all the elements in the array

import ctypes

class myArray(object):
def __init__(self):
    self.length = 0
    self.capacity = 1
    self.Array = self.make_array(self.capacity)

def push(self, item):
    if self.length == self.capacity:
        self.resize(2*self.capacity)

    self.Array[self.length] = item
    self.length += 1
    print("Hello")

def getitem(self, index):
    if index >= self.length:
        return IndexError('Out Of Bounds')
    return self.Array[index]

def resize(self, new_cap):
    newArray = self.make_array(new_cap)

    for k in range(self.length):
        newArray[k] = self.Array[k]

    self.Array = newArray
    self.capacity = new_cap


def make_array(self, new_cap):
    return (new_cap * ctypes.py_object)()  
Yida Lin
  • 167
  • 2
  • 10
Vishal1809
  • 25
  • 1
  • 4

1 Answers1

1

Approach 1: Add a print_all() method

def print_all(self):
    print(self.Array[:self.length])

Approach 2: Create a string representation of the class

def __str__(self):
    return str(self.Array[:self.length])

Simple test:

arr = myArray()
arr.push(5)
arr.push(2)
arr.push(3)
arr.push(5)
arr.push(4)
arr.push(6)
arr.print_all()
print(arr)

Output:

Hello

Hello

Hello

Hello

Hello

Hello

[5, 2, 3, 5, 4, 6]

[5, 2, 3, 5, 4, 6]

Full definition of the class:

import ctypes

class myArray(object):
    def __init__(self):
        self.length = 0
        self.capacity = 1
        self.Array = self.make_array(self.capacity)

    def push(self, item):
        if self.length == self.capacity:
            self.resize(2*self.capacity)

        self.Array[self.length] = item
        self.length += 1
        print("Hello")

    def getitem(self, index):
        if index >= self.length:
            return IndexError('Out Of Bounds')
        return self.Array[index]

    def resize(self, new_cap):
        newArray = self.make_array(new_cap)

        for k in range(self.length):
            newArray[k] = self.Array[k]

        self.Array = newArray
        self.capacity = new_cap

    def make_array(self, new_cap):
        return (new_cap * ctypes.py_object)()  

    def print_all(self):
        print(self.Array[:self.length])

    def __str__(self):
        return str(self.Array[:self.length])
Yida Lin
  • 167
  • 2
  • 10
  • wow thanks for the fast reply @Yida Lin . can you explain self.Array[:self.length] – Vishal1809 Jul 05 '19 at 18:29
  • This is the slice notation of Python. It means from the beginning (index 0) all the way to self.length (but not including self.length). Have a read here for more info: https://stackoverflow.com/questions/509211/understanding-slice-notation – Yida Lin Jul 05 '19 at 18:30
  • Hi, can you help me out on this?? – Vishal1809 Jul 06 '19 at 22:15
  • @Vishal1809 If you have a list `lst` containing 5 items [0, 1, 2, 3, 4]. Selecting `lst[0:3]` will retrieve [0, 1, 2]. But you can omit the 0 and retrieve the same thing by using `lst[:3]`. This is a shorthand. – Yida Lin Jul 06 '19 at 22:24