Skip to content

Commit

Permalink
add and remove working
Browse files Browse the repository at this point in the history
  • Loading branch information
naren-m committed Aug 15, 2021
1 parent 97194cd commit f645b33
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 13 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion data_structures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
__email__ = 'narenuday595@gmail.com'
__version__ = '0.1.0'

from . import stack, linked_list, queue, bst, graph
from . import stack, linked_list, queue, bst, graph, dynamic_arrays
43 changes: 39 additions & 4 deletions data_structures/dynamic_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,50 @@
# - get, set, pushback, remove, size

class DynamicArray:
def __init__(self, size=4):
self.capacity = size
def __init__(self, capacity=4):
self.capacity = capacity
self._size = 0
self.array = [None] * self.capacity
self._increaseFactor = 2

def get(self, i):
return self.array[i]

def set(self, i, val):
self.array[i] = val

def pushback(self, i, val):
pass
def pushback(self, val):
if self.capacity == self._size:
# resize the array
_array = [None] * (self.capacity * self._increaseFactor)
for i in range(self.capacity):
_array[i] = self.array[i]
self.array = _array
del _array

self.capacity *= self._increaseFactor

self.array[self._size] = val
self._size += 1

@property
def size(self):
return self._size

def remove(self, i):
# Remove the element and adjust the array.
while i < self._size:
self.array[i] = self.array[i+1]
print(i, self.array[i])
i = i + 1
self._size = self._size - 1

def __str__(self):
s = '{}, '
out = ''
for i in range(self._size):
out += s.format(self.array[i])
if i is None:
break

return '[{}]'.format(out.strip())
3 changes: 0 additions & 3 deletions tests/__init__.py

This file was deleted.

3 changes: 3 additions & 0 deletions tests/context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
4 changes: 0 additions & 4 deletions tests/test_data_structures_bst.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import unittest





if __name__ == '__main__':
unittest.main()
42 changes: 41 additions & 1 deletion tests/test_dynamic_arrays.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
import context

import unittest

from data_structures.dynamic_arrays import DynamicArray


class DynamicArrays(unittest.TestCase):
def test__init(self):
pass
array = DynamicArray()
for i in range(0,4):
array.pushback(i)

self.assertEqual(array.size, 4)
self.assertEqual(array.capacity, 4)

def test_pushback(self):
array = DynamicArray()

for i in range(0, 10):
array.pushback(i)

self.assertEqual(array.size, 10)
self.assertEqual(array.capacity, 16)

def test_remove(self):
array = DynamicArray()

for i in range(0, 10):
array.pushback(i)

self.assertEqual(array.size, 10)
self.assertEqual(array.capacity, 16)

array.remove(4)
self.assertEqual(str(array), '[0, 1, 2, 3, 5, 6, 7, 8, 9,]')

array.remove(4)
self.assertEqual(str(array), '[0, 1, 2, 3, 6, 7, 8, 9,]')

array.remove(0)
self.assertEqual(str(array), '[1, 2, 3, 6, 7, 8, 9,]')

array.remove(6)
self.assertEqual(str(array), '[1, 2, 3, 6, 7, 8,]')

0 comments on commit f645b33

Please sign in to comment.