diff --git a/hw1/Array_list.py b/hw1/Array_list.py new file mode 100644 index 0000000..ca59dad --- /dev/null +++ b/hw1/Array_list.py @@ -0,0 +1,158 @@ +from array import array + + +class Array_list: + def __init__(self, *args): + if not args: + self.arr_type = None + self.data = None + elif (type(args[0]) == int): + for i in args: + if type(i) != int: + raise Exception("Wrong type of input data") + self.arr_type = type(args[0]) + self.data = array('i', args) + elif (type(args[0]) == float): + for i in args: + if type(i) != float: + raise Exception("Wrong type of input data") + self.arr_type = type(args[0]) + self.data = array('d', args) + elif (type(args[0]) == str): + for i in args: + if type(i) != str: + raise Exception("Wrong type of input data") + self.arr_type = type(args[0]) + self.data = array('u', args) + + def __iter__(self): + if self.data is not None: + for i in range(self.data.buffer_info()[1]): + yield self.data[i] + + def __len__(self): + k = 0 + if self.data is not None: + for i in self.data: + k += 1 + return k + return 0 + + def __contains__(self, item): + if self.data is None: + raise Exception("Item does not exist") + elif type(item) != self.arr_type: + raise Exception("Wrong type of input data") + return item in self.data + + def __getitem__(self, index): + if self.data is None: + raise Exception("Item does not exist") + elif index >= len(self.data): + raise Exception("Index out of range") + return self.data[index] + + def __setitem__(self, index, item): + if self.data is None: + raise Exception("Item does not exist") + elif type(item) != self.arr_type: + raise Exception("Wrong type of input data") + elif index >= len(self.data): + raise Exception("Index out of range") + self.data[index] = item + + def __reversed__(self): + if self.data is not None: + return self.data[::-1] + + def __delitem__(self, index): + if self.data is None: + raise Exception("Index does not exist") + elif index >= len(self.data): + raise Exception("Index out of range") + del self.data[index] + + def __iadd__(self, other): + if other.data is None: + return self + elif self.data is None: + self.arr_type = other.arr_type + self.data = other.data + return self + elif self.arr_type == type(other[0]): + self.data = self.data + other.data + return self + else: + raise Exception("Wrong type of input data") + + def index(self, item): + if self.data is None: + raise Exception("Item does not exist") + elif type(item) != self.arr_type: + raise Exception("Wrong type of input data") + i = None + for i, k in enumerate(self.data): + if k == item: + return i + if i is None: + raise Exception("Item does not exist") + + def count(self, item): + if self.data is None: + return 0 + elif type(item) != self.arr_type: + raise Exception("Wrong type of input data") + i = 0 + for k in self.data: + if k == item: + i += 1 + return i + + def append(self, item): + if self.data is None: + self.arr_type = type(item) + if (type(item) == int): + self.data = array('i', [item]) + return + elif (type(item) == float): + self.data = array('d', [item]) + return + elif (type(item) == str): + self.data = array('u', [item]) + return + elif type(item) != self.arr_type: + raise Exception("Wrong type of input data") + else: + self.data = self.data + Array_list(item).data + return + + def clear(self): + self.data = None + self.arr_type = None + return self + + def extend(self, other): + return self.__iadd__(other) + + def pop(self, index): + if self.data is None: + raise Exception("Item does not exist") + elif index >= len(self.data): + raise Exception("Index out of range") + x = self.data[index] + del self.data[index] + return x + + def remove(self, item): + if self.data is None: + raise Exception("Item does not exist") + elif type(item) != self.arr_type: + raise Exception("Wrong type of input data") + else: + i = None + for i, k in enumerate(self.data): + if k == item: + self.pop(i) + return + if i is None: + raise Exception("Item does not exist") diff --git a/hw1/test.py b/hw1/test.py new file mode 100644 index 0000000..267bf17 --- /dev/null +++ b/hw1/test.py @@ -0,0 +1,81 @@ +import Array_list + +print("TEST INT") +arr_int_0 = Array_list.Array_list() +arr_int_1 = Array_list.Array_list(1, 2, 3, 2, 1) +arr_int_2 = Array_list.Array_list(11, 22, 33) + +print(arr_int_1.index(3)) +print(arr_int_1.count(2)) +arr_int_1.append(999) +print(*arr_int_1.data) +arr_int_1.extend(arr_int_2) +print(*arr_int_1.data) +arr_int_1.pop(6) +print(*arr_int_1.data) +arr_int_1.remove(3) +print(*arr_int_1.data) +print(999 in arr_int_1) +arr_int_1 += arr_int_2 +print(*arr_int_1.data) +print(len(arr_int_1)) +print(arr_int_1[5]) +arr_int_1[5] = 1488 +print(*arr_int_1.data) +reversed(arr_int_1) +print(*reversed(arr_int_1)) +arr_int_1.clear() +print(arr_int_1.data) + +print() +print("TEST FLOAT") +arr_float_0 = Array_list.Array_list() +arr_float_1 = Array_list.Array_list(1.1, 2.2, 3.3, 1.1, 5.5) +arr_float_2 = Array_list.Array_list(0.1, 0.2, 0.3, 0.4) + +print(arr_float_1.index(5.5)) +print(arr_float_1.count(1.1)) +arr_float_1.append(999.999) +print(*arr_float_1.data) +arr_float_1.extend(arr_float_2) +print(*arr_float_1.data) +arr_float_1.pop(2) +print(*arr_float_1.data) +arr_float_1.remove(0.2) +print(*arr_float_1.data) +print(999.1 in arr_float_1) +arr_float_1 += arr_float_2 +print(*arr_float_1.data) +print(len(arr_float_1)) +print(arr_float_1[4]) +arr_float_1[4] = 4.20 +print(*arr_float_1.data) +print(*reversed(arr_float_1)) +arr_float_1.clear() +print(arr_float_1.data) + +print() +print("TEST STR") +arr_str_0 = Array_list.Array_list() +arr_str_1 = Array_list.Array_list('a', 'b', 'c', 'a', 'a', 'a') +arr_str_2 = Array_list.Array_list('Z', 'Y', 'X') +print(arr_str_1.index('c')) +print(arr_str_1.count('a')) +arr_str_1.append('L') +print(*arr_str_1.data) +arr_str_1.extend(arr_str_2) +print(*arr_str_1.data) +arr_str_1.pop(6) +print(*arr_str_1.data) +arr_str_1.remove('a') +print(*arr_str_1.data) +print('Z' in arr_str_1) +arr_str_1 += arr_str_2 +print(*arr_str_1.data) +print(len(arr_str_1)) +print(arr_str_1[5]) +arr_str_1[3] = '@' +print(*arr_str_1.data) +print(*reversed(arr_str_1)) +arr_str_1.clear() +print(arr_str_1.data)