class ArrayStack:

    #implements the ADT Stack (Stack.py)
    #uses the DynamicArray class (DynamicArray.py)
    def __init__( self ):
        self._A = DynamicArray()

    def __len__( self ):
        return len( self._A )

    def is_empty( self ):
        return len( self._A ) == 0

    def __str__( self ):
        pp = str( self._A )
        pp += "[top = " + str( len( self._A ) - 1 ) + ")"
        return pp

    #push obj
    def push( self, obj ):
        self._A.append( obj )

    #pop
    def pop( self ):
        return self._A.remove( len( self._A ) )

    #top
    def top( self ):
        return self._A.get( len( self._A ) )
Beispiel #2
0
class ArrayStack:

    #implements the ADT Stack (Stack.py)
    #uses the DynamicArray class (DynamicArray.py)
    def __init__(self):
        self._A = DynamicArray()

    def __len__(self):
        return len(self._A)

    def is_empty(self):
        return len(self._A) == 0

    def __str__(self):
        pp = str(self._A)
        pp += "[top = " + str(len(self._A) - 1) + ")"
        return pp

    #push obj
    def push(self, obj):
        self._A.append(obj)

    #pop
    def pop(self):
        return self._A.remove(len(self._A))

    #top
    def top(self):
        return self._A.get(len(self._A))
Beispiel #3
0
class Stack:
    def __init__(self):
        self.list = DynamicArray(None)

    def clear(self) -> None:
        self.list.remove_all_element()
        return None

    def is_empty(self):
        return self.list.is_empty()

    def size(self):
        return self.list.get_size()

    def push(self, element):
        self.list.add_last(element)

    def pop(self):
        return self.list.remove(self.list.size - 1)

    def top(self):
        return self.list.get(self.size() - 1)

    def travel(self):
        self.list.print_array()
Beispiel #4
0
class ArrayList(List):

    # implémente l'ADT List (List.py)
    # utilise la classe DynamicArray (DynamicArray.py)
    def __init__(self):
        self._A = DynamicArray()

    # retourne le nombre d'éléments
    def __len__(self):
        return len(self._A)

    # retourne une chaîne de caractères représentant la liste
    def __str__(self):
        return str(self._A)

    # accès avec notation des crochets
    def __getitem__(self, k):
        return self._A[k]

    # ajoute l'élément obj à la fin de la liste
    def append(self, obj):
        self._A.append(obj)

    # retire le ième élément de la liste
    def remove(self, i):
        return self._A.remove(i)

    # retourne l'index de l'élément obj s'il existe
    def find(self, obj):
        return self._A.find(obj)
Beispiel #5
0
class ArrayList:

    #implements the ADT List (List.py)
    #uses the DynamicArray class (DynamicArray.py)
    def __init__( self ):
        self._A = DynamicArray()

    def __len__( self ):
        return len( self._A )

    def __str__( self ):
        return str( self._A )

    def __getitem__( self, k ):
        return self._A[k]

    #append at the end of the list
    def append( self, obj ):
        self._A.append( obj )

    #remove the ith element of the list
    def remove( self, i ):
        if len( self._A ) == 0:
            return False
        else:
            self._A.remove( i )
            return True

    #return the rank of obj in the list
    def find( self, obj ):
        return self._A.find( obj )
 def test_to_list(self):
     da = DynamicArray()
     self.assertEqual(da.to_list(), [])
     da.add(1)
     self.assertEqual(da.to_list(), [1])
     da.add(2)
     self.assertEqual(da.to_list(), [1, 2])
 def test_size(self):
     da = DynamicArray()
     self.assertEqual(da.size(), 0)
     da.add(1)
     self.assertEqual(da.size(), 1)
     da.add(2)
     self.assertEqual(da.size(), 2)
Beispiel #8
0
 def test_pop_len(self):
     a = DynamicArray()
     a.append(2)
     a.append(1)
     a.append(4)
     a.append(6)
     a.pop()
     assert len(a) == 3
Beispiel #9
0
class Stack(object):

    # The __init__ function is run when you instantiate an instance of this object
    def __init__(self):
        self._size = 0
        self._dynamic_array = DynamicArray()

    #################################
    ## DO YOUR WORK BELOW THIS LINE #
    #################################

    # Return whether or not the stack is empty
    def is_empty(self):
        #TODO: return True or False to answer if the stack is empty
        if self._size == 0:
            return True

    # Return the size on the stack
    def size(self):
        #TODO: return the current size of the stack
        return self._size

    # look at the top of the stack (but don't remove)
    def top(self):
        #TODO: return the current top of the stack
        return self._dynamic_array.get(self._size - 1)

    # Push a value on to the top of the stack
    def push(self, value):
        #TODO: push the pased value onto the stack
        self._dynamic_array.add(value)
        self._size += 1

    # take the top value off of the stack and return it to the user
    def pop(self):
        #TODO: pop the top value off the stack
        self._dynamic_array.remove(self._size - 1)
        self._size -= 1
        return 1

    # DO NOT MODIFY THIS PRINT
    def print_stack(self):
        print '====\n' + '\n----\n'.join(
            [str(v)
             for v in self._dynamic_array._data[:self._size]]) + '\n^^^^'
Beispiel #10
0
class Stack(object):

    # The __init__ function is run when you instantiate an instance of this object
    def __init__(self):
        self._size = 0
        self._dynamic_array = DynamicArray()

    #################################
    ## DO YOUR WORK BELOW THIS LINE #
    #################################

    def is_empty(self):
        return self._size == 0

    def size(self):
        return self._dynamic_array.size()

    def top(self):
        return self._dynamic_array.get(self._dynamic_array.size() - 1)

    def push(self, value):
        self._dynamic_array.add(value)


    def pop(self):
        return self._dynamic_array.remove(self._dynamic_array.size() - 1)

    # DO NOT MODIFY THIS PRINT
    def print_stack(self):
        print '====\n' + '\n----\n'.join([str(v) for v in self._dynamic_array._data[:self._size]]) + '\n^^^^'
 def test_filter(self):
     x = [1, 2, 4, 8, 3, 7, 9]
     da = DynamicArray()
     da.from_list(x)
     da.filter("is_even")
     # self.assertEqual(da.to_list(), [1, 3, 7,9])
     print(da.to_list())
 def test_iter(self):
     x = [1, 2, 3]
     da = DynamicArray()
     da.from_list(x)
     tmp = []
     for e in da.to_list():
         tmp.append(e)
     self.assertEqual(x, tmp)
     self.assertEqual(da.to_list(), tmp)
     i = iter(DynamicArray())
     self.assertRaises(StopIteration, lambda: next(i))
Beispiel #13
0
class ArrayStack(Stack):

    # implémente l'ADT Stack (Stack.py)
    # avec un tableau dynamique (DynamicArray.py)
    def __init__(self):
        self._A = DynamicArray()

    def __len__(self):
        return len(self._A)

    def is_empty(self):
        return len(self._A) == 0

    def __str__(self):
        pp = str(self._A)
        if self.is_empty():
            pp += "[empty stack]"
        else:
            obj = self.top()
            pp += "[top = " + str(obj) + ", idx = " + str(len(self._A) -
                                                          1) + "]"
        return pp

    # push implémenté avec append
    def push(self, obj):
        self._A.append(obj)

    # pop récupère l'erreur de DynamicArray
    def pop(self):
        try:
            return self._A.pop()
        except IndexError:
            return None

    # top implémenté avec get du dernier élément
    def top(self):
        return self._A.get(len(self._A) - 1)
Beispiel #14
0
 def __init__( self ):
     self._A = DynamicArray()
 def test_find(self):
     x = [1, 2, 4, 8]
     da = DynamicArray()
     da.from_list(x)
     index, _ = da.find("is_even")
     self.assertEqual(index, [1, 2, 3])
 def test_mconcat(self):
     x = [1, 2, 3]
     y = [4, 5, 6]
     da = DynamicArray()
     B = DynamicArray()
     da.from_list(x)
     B.from_list(y)
     da.mconcat(B)
     self.assertEqual(da.to_list(), [1, 2, 3, 4, 5, 6])
 def test_remove(self):
     x = [1, 2, 3]
     da = DynamicArray()
     da.from_list(x)
     da.remove(2)
     self.assertEqual(da.to_list(), [1, 3])
 def test_reduce(self):
     da = DynamicArray()
     self.assertEqual(da.reduce(lambda st, e: st + e, 0), 0)
     da = DynamicArray()
     da.from_list([1, 2, 3])
     self.assertEqual(da.reduce(lambda st, e: st + e, 0), 6)
Beispiel #19
0
 def test_append_len(self):
     arr = DynamicArray()
     arr.append(1)
     arr.append(2)
     assert len(arr) == 2
 def test_from_list(self):
     test_data = [[], [1], [1, 2]]
     for lst in test_data:
         da = DynamicArray()
         da.from_list(lst)
         self.assertEqual(da.to_list(), lst)
Beispiel #21
0
 def __init__(self):
     self.list = DynamicArray(None)
Beispiel #22
0
#########################################################################################
# Here are some basic tests to make sure your dynamic array implementation runs correctly
# You can run them by running 'python DynamicArrayTests.py' from the command line
#########################################################################################

from DynamicArray import DynamicArray

dyn_arr = DynamicArray()

print "Checking basic variable initializations..."
assert dyn_arr._size == dyn_arr.size()
assert dyn_arr._capacity == dyn_arr.capacity()

dyn_arr.print_data()
print "done\n"

###################################

print "Checking add function..."

dyn_arr.add(1)
assert dyn_arr.size() == 1
assert dyn_arr.capacity() == DynamicArray.INIT_CAPACITY

for v in range(2, dyn_arr.capacity() + 1):
    dyn_arr.add(v)

assert dyn_arr.capacity() == DynamicArray.INIT_CAPACITY

dyn_arr.add(7)
 def __init__(self):
     DynamicArray.__init__(self)
Beispiel #24
0
from DynamicArray import DynamicArray

arr = DynamicArray()
for i in range(11):
    arr.append(i)

for i in range(len(arr)):
    print(arr[i])
Beispiel #25
0
 def test_append_indexing(self):
     arr = DynamicArray()
     arr.append(1)
     assert arr[0] == 1
 def test_map(self):
     da = DynamicArray()
     da.map(str)
     self.assertEqual(da.to_list(), [])
     da = DynamicArray()
     da.from_list([1, 2, 3])
     da.map(str)
     self.assertEqual(da.to_list(), ["1", "2", "3"])
__author__ = 'Stephen'

from DynamicArray import DynamicArray
from SortedList import SortedList
import sys

array = DynamicArray()
array.append(198479874)
for i in range(5000):
    array.append("1")
    size = sys.getsizeof(array)
    print "Size in bytes: " + str(size)
Beispiel #28
0
 def test_pop_i(self):
     a = DynamicArray()
     a.append(2)
     a.append(1)
     a.append(4)
     assert a.pop(1) == 1
Beispiel #29
0
 def __init__(self):
     self._size = 0
     self._dynamic_array = DynamicArray()
 def test_get_item(self):
     da = DynamicArray()
     da.add(1)
     self.assertEqual(da.__getitem__(0), 1)
Beispiel #31
0
 def __init__(self):
     self._A = DynamicArray()
Beispiel #32
0
from DynamicArray import DynamicArray

a = DynamicArray()
a.append(5)
a.append(10)
a.append(15)

print(a)