Created
August 26, 2019 21:35
-
-
Save gunnarig/1c2a30eb9a0997ee2847fc3255b8d817 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ArrayList: | |
def __init__(self): | |
self.size = 0 | |
self.capacity = 4 | |
self.Arr = [None]*self.capacity | |
#self.index = 0 | |
def print_array_list(array_list): | |
count = 0 | |
while array_list.size != count: | |
if count >= array_list.size: | |
return | |
elif count <= array_list.size: | |
print(array_list.Arr[count]) | |
newcount = count+1 | |
count = newcount | |
elif array_list.arr[count] == None: | |
return | |
elif count == array_list.capacity: | |
return | |
def append(array_list,value): | |
if array_list.capacity == array_list.size: | |
#print("hit") | |
resize(array_list) | |
array_list.Arr[array_list.size] = value | |
newsize = array_list.size+1 | |
array_list.size = newsize | |
else: | |
array_list.Arr[array_list.size] = value | |
newsize = array_list.size+1 | |
array_list.size = newsize | |
def resize(array_list,value = None): | |
new_cap = array_list.capacity * 2 | |
new_arr = new_cap * [None] | |
if value != None: | |
new_arr[0] = value | |
for i in range(array_list.capacity): | |
new_arr[i+1] = array_list.Arr[i] | |
array_list.Arr = new_arr | |
array_list.capacity = new_cap | |
newsize = array_list.size+1 | |
array_list.size = newsize | |
else: | |
for i in range(array_list.capacity): | |
new_arr[i] = array_list.Arr[i] | |
array_list.Arr = new_arr | |
array_list.capacity = new_cap | |
def get_first(array_list): | |
return array_list.Arr[0] | |
def get_at(array_list, index): | |
if index > array_list.size: | |
return None | |
elif index < 0: | |
return None | |
else: | |
return array_list.Arr[index] | |
def get_last(array_list): | |
if array_list.size == 0: | |
return None | |
else: | |
return array_list.Arr[array_list.size-1] | |
def prepend(array_list, value): | |
if array_list.size+1 > array_list.capacity: | |
resize(array_list,value) | |
elif array_list.size < array_list.capacity: | |
new_arr = [None] * array_list.capacity | |
new_arr[0] = value | |
for i in range(array_list.capacity-1): | |
new_arr[i+1] = array_list.Arr[i] | |
array_list.Arr = new_arr | |
newsize = array_list.size +1 | |
array_list.size = newsize | |
def insert(array_list, value, index): | |
if index > array_list.capacity: | |
return | |
elif array_list.Arr[index] == None: | |
append(array_list,value) | |
elif array_list.Arr[index] != None: | |
new_arr = [None] * array_list.capacity | |
#target = array_list.Arr[index] | |
new_arr[index] = value | |
for i in range(array_list.capacity-1): | |
if new_arr[i] == None: | |
new_arr[i] = array_list.Arr[i] | |
elif new_arr[i] != None: | |
new_arr[i+1] = array_list.Arr[i] | |
array_list.Arr = new_arr | |
def remove(array_list, index): | |
if index > array_list.capacity: | |
return | |
elif array_list.Arr[index] != None: | |
array_list.Arr[index] = None | |
new_arr = [None] * array_list.capacity | |
flip = None | |
for i in range(array_list.capacity-1): | |
if array_list.Arr[i] == None and array_list.Arr[i+1] == None: | |
array_list.Arr = new_arr | |
return | |
else: | |
if array_list.Arr[i] == None: | |
new_arr[i] = array_list.Arr[i+1] | |
flip = 1 | |
elif array_list.Arr != None and flip == None: | |
new_arr[i] = array_list.Arr[i] | |
elif flip == 1: | |
new_arr[i] = array_list.Arr[i+1] | |
newsize = array_list.size-1 | |
array_list.size = newsize | |
array_list.Arr = new_arr | |
if __name__ == "__main__": | |
my_arr = ArrayList() | |
#print_array_list(my_arr) | |
append(my_arr,5) | |
append(my_arr,3) | |
append(my_arr,2) | |
append(my_arr,1) | |
append(my_arr,4) | |
prepend(my_arr,10) | |
prepend(my_arr,9) | |
prepend(my_arr,8) | |
prepend(my_arr,7) | |
prepend(my_arr,6) | |
insert(my_arr,100,5) | |
remove(my_arr,5) | |
print(str(get_at(my_arr,10))+ " out of bounds") | |
print(str(get_at(my_arr,-10))+ " Under bounds") | |
print(str(get_at(my_arr,4))+ " Index exists") | |
print(str(get_last(my_arr))+ " Get Last") | |
print_array_list(my_arr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment