From fbe91e08f07fd06f7b33a27c640e7ab64db6b54d Mon Sep 17 00:00:00 2001 From: obscene3190 <32190896+obscene3190@users.noreply.github.com> Date: Sun, 22 Mar 2020 19:05:12 +0300 Subject: [PATCH 1/7] hw1 is created --- hw1.py | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 hw1.py diff --git a/hw1.py b/hw1.py new file mode 100644 index 0000000..f3a8666 --- /dev/null +++ b/hw1.py @@ -0,0 +1,121 @@ +from array import array + +# для array используем только repr, len, индексацию и объединение, трактууем как сишный массив + +types = {'i' : "", 'f' : "", 'u' : ""} + +# для инициализации испольуем буковки i, f или u. Как для оригинальных array +class ArrayList: + def __init__(self, initial_capacity, initial_type, initial_length = 0): + if (initial_capacity > 0): + self.capacity = initial_capacity + self.length = initial_length + self.array = array(initial_type, []) + self.type = types[initial_type] + else: raise ValueError("Capacity can't be less, than 0") + + def __str__(self): + result = '' + for i in range(self.length): + result += str(self.array[i]) + ' ' + return result + + def __repr__(self): + return str(self) + + # добавление элементов(с возможностью добавления сразу нескольких(но минимум одного)) + def append(self, el, *keys): + if (self.length < self.capacity): + if (str(type(el)) == self.type): + self.array += array(self.array.typecode, [el]) + self.length += 1 + else: raise TypeError('Incorrect key type') + else: raise IndexError('Array is full') + for key in keys: + self.append(key) + + # вставка на определенную позицию + def insert(self, pos, el): + if (self.length < self.capacity): + if (str(type(el)) == self.type): + self.array = array(self.array.typecode, self.array[0:pos] + + array(self.array.typecode, [el]) + self.array[pos:self.length]) + self.length += 1 + else: raise TypeError('Incorrect key type') + else: raise IndexError('Array is full') + + def pop(self): + if (self.length > 0): + self.array = self.array[:-1] + self.length += -1 + else: raise IndexError('Array is empty') + + def remove(self, el): + if (self.length > 0): + pos = None + for i in range(self.length): + if (self.array[i] == el): pos = i; break + if (pos is not None): + self.array = self.array[:pos] + self.array[pos+1:] + self.length += -1 + else: + print('No such key in Array') + return + #raise KeyError('No such key in Array') + else: raise IndexError('Array is empty') + + def __len__(self): + return self.length + + def __getitem__(self, pos): + return self.array[pos] + + #__iter__, __reverse__, нет смысла определять, тк есть ___getitem__ и __len__(так написано тут https://pythonz.net/references/named/object.__reversed__/) + + def count(self, el): + counter = 0 + for i in range(self.length): + if (self.array[i] == el): counter += 1 + return counter + + def index(self, el): + for i in range(self.length): + if (self.array[i] == el): return i + return 'No such element' + + #def __add__(self, other): + if isinstance(other, ArrayList): + if (other.type == self.type): + return ArrayList(other.capacity + self.capacity, self.type, + other.length + self.length, other.array + self.array) + else: raise ValueError('Different types') + else: raise ValueError('Different types of objects') + + def __contains__(self, el): + for i in range(self.length): + if (self.array[i] == el): return True + return False + + +if __name__ == '__main__': + # инициализация + list1 = ArrayList(5, 'i') + # [. . . . .] + #вставляем элементы + list1.append(0, 2, 3) + # [0 2 3 . .] + # insert in position + list1.insert(1, 1) + # [0 1 2 3 .] + print(list1) + # pop last + list1.pop() + # [0 1 2 . .] + list1.remove(1) + print(list1) + # [0 2 . . .] + list1.remove(16) + print(list1) + # working with iterator: + for i in list1: + print('i = ', i) From d7fcd65b8109b0c931b1ecb59ee8cf019630be6b Mon Sep 17 00:00:00 2001 From: obscene3190 <32190896+obscene3190@users.noreply.github.com> Date: Sun, 22 Mar 2020 22:25:36 +0300 Subject: [PATCH 2/7] iterator class was added --- hw1.py | 52 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/hw1.py b/hw1.py index f3a8666..5ea4e94 100644 --- a/hw1.py +++ b/hw1.py @@ -1,5 +1,22 @@ from array import array + +class ArrayIterator: + def __init__(self, collection, length): + self.collection = collection + self.length = length + self.pos = 0 + + def __next__(self): + if (self.pos < self.length): + self.pos += 1 + return self.collection[self.pos-1] + else: + raise StopIteration + + def __iter__(self): + return self + # для array используем только repr, len, индексацию и объединение, трактууем как сишный массив types = {'i' : "", 'f' : "", 'u' : ""} @@ -12,7 +29,8 @@ def __init__(self, initial_capacity, initial_type, initial_length = 0): self.length = initial_length self.array = array(initial_type, []) self.type = types[initial_type] - else: raise ValueError("Capacity can't be less, than 0") + else: + raise ValueError("Capacity can't be less, than 0") def __str__(self): result = '' @@ -21,7 +39,7 @@ def __str__(self): return result def __repr__(self): - return str(self) + return repr(self.array) # добавление элементов(с возможностью добавления сразу нескольких(но минимум одного)) def append(self, el, *keys): @@ -29,8 +47,10 @@ def append(self, el, *keys): if (str(type(el)) == self.type): self.array += array(self.array.typecode, [el]) self.length += 1 - else: raise TypeError('Incorrect key type') - else: raise IndexError('Array is full') + else: + raise TypeError('Incorrect key type') + else: + raise IndexError('Array is full') for key in keys: self.append(key) @@ -41,20 +61,25 @@ def insert(self, pos, el): self.array = array(self.array.typecode, self.array[0:pos] + array(self.array.typecode, [el]) + self.array[pos:self.length]) self.length += 1 - else: raise TypeError('Incorrect key type') - else: raise IndexError('Array is full') + else: + raise TypeError('Incorrect key type') + else: + raise IndexError('Array is full') def pop(self): if (self.length > 0): self.array = self.array[:-1] self.length += -1 - else: raise IndexError('Array is empty') + else: + raise IndexError('Array is empty') def remove(self, el): if (self.length > 0): pos = None for i in range(self.length): - if (self.array[i] == el): pos = i; break + if (self.array[i] == el): + pos = i + break if (pos is not None): self.array = self.array[:pos] + self.array[pos+1:] self.length += -1 @@ -62,7 +87,8 @@ def remove(self, el): print('No such key in Array') return #raise KeyError('No such key in Array') - else: raise IndexError('Array is empty') + else: + raise IndexError('Array is empty') def __len__(self): return self.length @@ -70,6 +96,8 @@ def __len__(self): def __getitem__(self, pos): return self.array[pos] + def __iter__(self): + return ArrayIterator(self.array, self.length) #__iter__, __reverse__, нет смысла определять, тк есть ___getitem__ и __len__(так написано тут https://pythonz.net/references/named/object.__reversed__/) def count(self, el): @@ -88,8 +116,10 @@ def index(self, el): if (other.type == self.type): return ArrayList(other.capacity + self.capacity, self.type, other.length + self.length, other.array + self.array) - else: raise ValueError('Different types') - else: raise ValueError('Different types of objects') + else: + raise ValueError('Different types') + else: + raise ValueError('Different types of objects') def __contains__(self, el): for i in range(self.length): From 68294bab5b8f5b9b82745dd282058ec220af2ba9 Mon Sep 17 00:00:00 2001 From: obscene3190 <32190896+obscene3190@users.noreply.github.com> Date: Sun, 22 Mar 2020 22:30:40 +0300 Subject: [PATCH 3/7] - comment --- hw1.py | 1 - 1 file changed, 1 deletion(-) diff --git a/hw1.py b/hw1.py index 5ea4e94..0c6c5b2 100644 --- a/hw1.py +++ b/hw1.py @@ -98,7 +98,6 @@ def __getitem__(self, pos): def __iter__(self): return ArrayIterator(self.array, self.length) - #__iter__, __reverse__, нет смысла определять, тк есть ___getitem__ и __len__(так написано тут https://pythonz.net/references/named/object.__reversed__/) def count(self, el): counter = 0 From b5561135c9edfe87a1d66e64240457d17c8f928b Mon Sep 17 00:00:00 2001 From: obscene3190 <32190896+obscene3190@users.noreply.github.com> Date: Sun, 22 Mar 2020 22:37:45 +0300 Subject: [PATCH 4/7] reversed added --- hw1.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/hw1.py b/hw1.py index 0c6c5b2..82de855 100644 --- a/hw1.py +++ b/hw1.py @@ -99,6 +99,9 @@ def __getitem__(self, pos): def __iter__(self): return ArrayIterator(self.array, self.length) + def __reversed__(self): + return ArrayIterator(self.array[::-1], self.length) + def count(self, el): counter = 0 for i in range(self.length): @@ -148,3 +151,6 @@ def __contains__(self, el): # working with iterator: for i in list1: print('i = ', i) + print(next(iter(list1))) + # reversed возвращает итератор + print(reversed(list1)) From 60e35345b0be167177c5cc7371fbd59df417fda2 Mon Sep 17 00:00:00 2001 From: obscene3190 <32190896+obscene3190@users.noreply.github.com> Date: Sun, 22 Mar 2020 22:39:24 +0300 Subject: [PATCH 5/7] Update hw1.py --- hw1.py | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/hw1.py b/hw1.py index 82de855..10904d3 100644 --- a/hw1.py +++ b/hw1.py @@ -1,6 +1,6 @@ from array import array - +# класс итератора(вау) class ArrayIterator: def __init__(self, collection, length): self.collection = collection @@ -113,16 +113,6 @@ def index(self, el): if (self.array[i] == el): return i return 'No such element' - #def __add__(self, other): - if isinstance(other, ArrayList): - if (other.type == self.type): - return ArrayList(other.capacity + self.capacity, self.type, - other.length + self.length, other.array + self.array) - else: - raise ValueError('Different types') - else: - raise ValueError('Different types of objects') - def __contains__(self, el): for i in range(self.length): if (self.array[i] == el): return True From e4ea0520d055d5826e273855e0dfd1d3af5c5666 Mon Sep 17 00:00:00 2001 From: obscene3190 <32190896+obscene3190@users.noreply.github.com> Date: Mon, 23 Mar 2020 12:42:43 +0300 Subject: [PATCH 6/7] - initial length --- hw1.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/hw1.py b/hw1.py index 10904d3..b967569 100644 --- a/hw1.py +++ b/hw1.py @@ -21,16 +21,16 @@ def __iter__(self): types = {'i' : "", 'f' : "", 'u' : ""} -# для инициализации испольуем буковки i, f или u. Как для оригинальных array +# для инициализации используем буковки i, f или u. Как для оригинальных array class ArrayList: - def __init__(self, initial_capacity, initial_type, initial_length = 0): + def __init__(self, initial_capacity, initial_type): if (initial_capacity > 0): self.capacity = initial_capacity - self.length = initial_length + self.length = 0 self.array = array(initial_type, []) self.type = types[initial_type] else: - raise ValueError("Capacity can't be less, than 0") + raise ValueError("Capacity can't be less, than 1") def __str__(self): result = '' From 4d0ab6c71628de250bb876389c17198f9ac2de1b Mon Sep 17 00:00:00 2001 From: obscene3190 <32190896+obscene3190@users.noreply.github.com> Date: Wed, 1 Apr 2020 20:21:56 +0300 Subject: [PATCH 7/7] Update hw1.py --- hw1.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/hw1.py b/hw1.py index b967569..627d259 100644 --- a/hw1.py +++ b/hw1.py @@ -42,7 +42,7 @@ def __repr__(self): return repr(self.array) # добавление элементов(с возможностью добавления сразу нескольких(но минимум одного)) - def append(self, el, *keys): + def append(self, el): if (self.length < self.capacity): if (str(type(el)) == self.type): self.array += array(self.array.typecode, [el]) @@ -51,8 +51,6 @@ def append(self, el, *keys): raise TypeError('Incorrect key type') else: raise IndexError('Array is full') - for key in keys: - self.append(key) # вставка на определенную позицию def insert(self, pos, el):