Created
August 27, 2019 09:52
-
-
Save gunnarig/aac45d05929201542adf9f83a7919f99 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): | |
str_val = "" | |
for i in range(array_list.size - 1): | |
if array_list.Arr[i] != None: | |
if array_list.size <= 0: | |
return | |
else: | |
str_val += str(array_list.Arr[i]) + ", " | |
# if(array_list.size > 0): | |
# str_val += str(array_list.Arr[array_list.size - 1]) | |
print(str_val) | |
# 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 | |
array_list.size += 1 | |
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__": | |
arr_lis = ArrayList() | |
append(arr_lis, 3) | |
append(arr_lis, 5) | |
append(arr_lis, 4) | |
print_array_list(arr_lis) | |
append(arr_lis, 9) | |
append(arr_lis, 8) | |
append(arr_lis, 7) | |
print_array_list(arr_lis) | |
append(arr_lis, 13) | |
append(arr_lis, 15) | |
append(arr_lis, 14) | |
print_array_list(arr_lis) | |
print(arr_lis.size) | |
print(arr_lis.capacity) | |
print(arr_lis.Arr) | |
print(get_first(arr_lis)) | |
print(get_at(arr_lis, 0)) | |
print(get_at(arr_lis, 7)) | |
print(get_at(arr_lis, 9)) | |
print(get_last(arr_lis)) | |
print_array_list(arr_lis) | |
prepend(arr_lis, 101) | |
print_array_list(arr_lis) | |
insert(arr_lis, 102, 11) | |
print_array_list(arr_lis) | |
insert(arr_lis, 102, 10) | |
print_array_list(arr_lis) | |
insert(arr_lis, 103, 8) | |
print_array_list(arr_lis) | |
insert(arr_lis, 104, 0) | |
print_array_list(arr_lis) | |
remove(arr_lis, 0) | |
print_array_list(arr_lis) | |
remove(arr_lis, 7) | |
print_array_list(arr_lis) | |
remove(arr_lis, 11) | |
print_array_list(arr_lis) | |
remove(arr_lis, 10) | |
print_array_list(arr_lis) | |
# 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