From 87fb60acc56ee7dac55aa2c6b6c83eaba00d260f Mon Sep 17 00:00:00 2001 From: Alexey Chubukov Date: Mon, 23 Mar 2020 13:21:09 +0300 Subject: [PATCH 1/5] Sequence ArrayList --- DZ1/ArrayList_Sequence.py | 90 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 DZ1/ArrayList_Sequence.py diff --git a/DZ1/ArrayList_Sequence.py b/DZ1/ArrayList_Sequence.py new file mode 100644 index 0000000..656e03d --- /dev/null +++ b/DZ1/ArrayList_Sequence.py @@ -0,0 +1,90 @@ + +from array import array + +class ArrayList(): + def __init__(self, type, init_list=[]): + try: + self.data=array(type, init_list) + except TypeError as er: + self.data=array(type) + print(er) + + def __getitem__(self, item): + try: + return self.data[item] + except IndexError as er: + print(er) + except TypeError as er: + print(er) + + def __len__(self): + return len(self.data) + + + def __contains__(self, item): + for i in range(len(self.data)): + if item==self.data[i]: + return True + return False + + def __iter__(self): + return (self.data[i] for i in range(len(self.data))) + + def __reversed__(self): + return self.data[::-1] + + def index(self, item): + for i in range(len(self.data)): + if item == self.data[i]: + return i + raise ValueError + + def count(self, item): + return len([self.data[i] for i in range(len(self.data)) if item==self.data[i]]) + + +if __name__=="__main__": + my_array1 = ArrayList('l',[1,2,2,3,4,5]) + my_array2 = ArrayList('d', [1.5, 7.7, 123.1]) + my_array3 = ArrayList('u', ['s','t','r','i','n','g']) + + # Проверка получения значения по индексу + print('my_array1[0] = {}'.format(my_array1[0])) + print('my_array2[1] = {}'.format(my_array2[1])) + print('my_array3[2] = {}'.format(my_array3[2])) + + # Проверка длины + print('len my_array1 = {}'.format(len(my_array1))) + print('len my_array2 = {}'.format(len(my_array2))) + print('len my_array3 = {}'.format(len(my_array3))) + + # Проверка вхождения элементов + print('10 in [1,2,2,3,4,5] -> {}'.format(10 in my_array1)) + print('1.5 in [1.5, 7.7, 123.1] -> {}'.format(1.5in my_array2)) + print("'g' in ['s','t','r','i','n','g'] -> {}".format('g' in my_array3)) + + # Проверка итераторов + for v in my_array1: + print(v, end=' ') + print() + for v in my_array2: + print(v, end=' ') + print() + for v in my_array3: + print(v, end=' ') + print() + + # Переворот массивов + print("Reversed [1,2,2,3,4,5] is {}".format(reversed(my_array1))) + print("Reversed [1.5, 7.7, 123.1] is {}".format(reversed(my_array2))) + print("Reversed ['s','t','r','i','n','g'] is {}".format(reversed(my_array3))) + + # Проверка вхождений индекса + print("3 on the {} place in [1,2,2,3,4,5]".format(my_array1.index(3))) + print("7.7 on the {} place in [1.5, 7.7, 123.1]".format(my_array2.index(7.7))) + print("i on the {} place in ['s','t','r','i','n','g']".format(my_array3.index('i'))) + + # Проверка количества элементов + print("count of 2 in [1,2,2,3,4,5] is {}".format(my_array1.count(2))) + print("count of 1.1 in [1.5, 7.7, 123.1] is {}".format(my_array2.count(1.1))) + print("count of n in ['s','t','r','i','n','g'] is {}".format(my_array3.count('n'))) From c60c0b9c86c69a0f22a8b973fb088f873e4de0f1 Mon Sep 17 00:00:00 2001 From: Alexey Chubukov Date: Mon, 23 Mar 2020 23:58:42 +0300 Subject: [PATCH 2/5] changed reversed and errors --- DZ1/ArrayList.py | 95 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 DZ1/ArrayList.py diff --git a/DZ1/ArrayList.py b/DZ1/ArrayList.py new file mode 100644 index 0000000..6f00557 --- /dev/null +++ b/DZ1/ArrayList.py @@ -0,0 +1,95 @@ + +from array import array + +class ArrayList: + def __init__(self, type, init_list=[]): + self.data=array(type, init_list) + + def __getitem__(self, item): + return self.data[item] + + def __len__(self): + return len(self.data) + + + def __contains__(self, item): + for i in range(len(self.data)): + if item==self.data[i]: + return True + return False + + def __iter__(self): + return (self.data[i] for i in range(len(self.data))) + + def __reversed__(self): + #for i in range(len(self.data)): + # yield self.data[len(self.data)-i] + return (self.data[len(self.data)-1-i] for i in range(len(self.data))) + + def index(self, item): + for i in range(len(self.data)): + if item == self.data[i]: + return i + raise ValueError + + def count(self, item): + counter = 0 + for i in range(len(self.data)): + if self.data[i]==item: + counter += 1 + return counter + + +if __name__=="__main__": + my_array1 = ArrayList('l',[1,2,2,3,4,5]) + my_array2 = ArrayList('d', [1.5, 7.7, 123.1]) + my_array3 = ArrayList('u', ['s','t','r','i','n','g']) + + # Проверка получения значения по индексу + print('my_array1[0] = {}'.format(my_array1[0])) + print('my_array2[1] = {}'.format(my_array2[1])) + print('my_array3[2] = {}'.format(my_array3[2])) + + # Проверка длины + print('len my_array1 = {}'.format(len(my_array1))) + print('len my_array2 = {}'.format(len(my_array2))) + print('len my_array3 = {}'.format(len(my_array3))) + + # Проверка вхождения элементов + print('10 in [1,2,2,3,4,5] -> {}'.format(10 in my_array1)) + print('1.5 in [1.5, 7.7, 123.1] -> {}'.format(1.5in my_array2)) + print("'g' in ['s','t','r','i','n','g'] -> {}".format('g' in my_array3)) + + # Проверка итераторов + for v in my_array1: + print(v, end=' ') + print() + for v in my_array2: + print(v, end=' ') + print() + for v in my_array3: + print(v, end=' ') + print() + itr = iter(my_array1) + while True: + try: + print(next(itr), end=' ') + except StopIteration: + break + print() + + # Переворот массивов + print("Reversed [1,2,2,3,4,5] is {}".format(list(reversed(my_array1)))) + print("Reversed [1.5, 7.7, 123.1] is {}".format(list(reversed(my_array2)))) + print("Reversed ['s','t','r','i','n','g'] is {}".format(list(reversed(my_array3)))) + + # Проверка вхождений индекса + print("3 on the {} place in [1,2,2,3,4,5]".format(my_array1.index(3))) + print("7.7 on the {} place in [1.5, 7.7, 123.1]".format(my_array2.index(7.7))) + print("i on the {} place in ['s','t','r','i','n','g']".format(my_array3.index('i'))) + + # Проверка количества элементов + print("count of 2 in [1,2,2,3,4,5] is {}".format(my_array1.count(2))) + print("count of 1.1 in [1.5, 7.7, 123.1] is {}".format(my_array2.count(1.1))) + print("count of n in ['s','t','r','i','n','g'] is {}".format(my_array3.count('n'))) + From 0b4a42ec5590e37b7ee2d9eb4f6175cc4bc4c7a1 Mon Sep 17 00:00:00 2001 From: Alexey Chubukov Date: Mon, 23 Mar 2020 23:59:59 +0300 Subject: [PATCH 3/5] rename ArrayList to Arraylist_Sequnece --- DZ1/ArrayList_Sequence.py | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/DZ1/ArrayList_Sequence.py b/DZ1/ArrayList_Sequence.py index 656e03d..6f00557 100644 --- a/DZ1/ArrayList_Sequence.py +++ b/DZ1/ArrayList_Sequence.py @@ -1,21 +1,12 @@ from array import array -class ArrayList(): +class ArrayList: def __init__(self, type, init_list=[]): - try: - self.data=array(type, init_list) - except TypeError as er: - self.data=array(type) - print(er) + self.data=array(type, init_list) def __getitem__(self, item): - try: - return self.data[item] - except IndexError as er: - print(er) - except TypeError as er: - print(er) + return self.data[item] def __len__(self): return len(self.data) @@ -31,7 +22,9 @@ def __iter__(self): return (self.data[i] for i in range(len(self.data))) def __reversed__(self): - return self.data[::-1] + #for i in range(len(self.data)): + # yield self.data[len(self.data)-i] + return (self.data[len(self.data)-1-i] for i in range(len(self.data))) def index(self, item): for i in range(len(self.data)): @@ -40,7 +33,11 @@ def index(self, item): raise ValueError def count(self, item): - return len([self.data[i] for i in range(len(self.data)) if item==self.data[i]]) + counter = 0 + for i in range(len(self.data)): + if self.data[i]==item: + counter += 1 + return counter if __name__=="__main__": @@ -73,11 +70,18 @@ def count(self, item): for v in my_array3: print(v, end=' ') print() + itr = iter(my_array1) + while True: + try: + print(next(itr), end=' ') + except StopIteration: + break + print() # Переворот массивов - print("Reversed [1,2,2,3,4,5] is {}".format(reversed(my_array1))) - print("Reversed [1.5, 7.7, 123.1] is {}".format(reversed(my_array2))) - print("Reversed ['s','t','r','i','n','g'] is {}".format(reversed(my_array3))) + print("Reversed [1,2,2,3,4,5] is {}".format(list(reversed(my_array1)))) + print("Reversed [1.5, 7.7, 123.1] is {}".format(list(reversed(my_array2)))) + print("Reversed ['s','t','r','i','n','g'] is {}".format(list(reversed(my_array3)))) # Проверка вхождений индекса print("3 on the {} place in [1,2,2,3,4,5]".format(my_array1.index(3))) @@ -88,3 +92,4 @@ def count(self, item): print("count of 2 in [1,2,2,3,4,5] is {}".format(my_array1.count(2))) print("count of 1.1 in [1.5, 7.7, 123.1] is {}".format(my_array2.count(1.1))) print("count of n in ['s','t','r','i','n','g'] is {}".format(my_array3.count('n'))) + From 74aedc0f301bf79697f9738e0aed658695328078 Mon Sep 17 00:00:00 2001 From: Alexey Chubukov Date: Tue, 24 Mar 2020 00:00:48 +0300 Subject: [PATCH 4/5] rename ArrayList to Arraylist_Sequnece 2 --- DZ1/ArrayList.py | 95 ------------------------------------------------ 1 file changed, 95 deletions(-) delete mode 100644 DZ1/ArrayList.py diff --git a/DZ1/ArrayList.py b/DZ1/ArrayList.py deleted file mode 100644 index 6f00557..0000000 --- a/DZ1/ArrayList.py +++ /dev/null @@ -1,95 +0,0 @@ - -from array import array - -class ArrayList: - def __init__(self, type, init_list=[]): - self.data=array(type, init_list) - - def __getitem__(self, item): - return self.data[item] - - def __len__(self): - return len(self.data) - - - def __contains__(self, item): - for i in range(len(self.data)): - if item==self.data[i]: - return True - return False - - def __iter__(self): - return (self.data[i] for i in range(len(self.data))) - - def __reversed__(self): - #for i in range(len(self.data)): - # yield self.data[len(self.data)-i] - return (self.data[len(self.data)-1-i] for i in range(len(self.data))) - - def index(self, item): - for i in range(len(self.data)): - if item == self.data[i]: - return i - raise ValueError - - def count(self, item): - counter = 0 - for i in range(len(self.data)): - if self.data[i]==item: - counter += 1 - return counter - - -if __name__=="__main__": - my_array1 = ArrayList('l',[1,2,2,3,4,5]) - my_array2 = ArrayList('d', [1.5, 7.7, 123.1]) - my_array3 = ArrayList('u', ['s','t','r','i','n','g']) - - # Проверка получения значения по индексу - print('my_array1[0] = {}'.format(my_array1[0])) - print('my_array2[1] = {}'.format(my_array2[1])) - print('my_array3[2] = {}'.format(my_array3[2])) - - # Проверка длины - print('len my_array1 = {}'.format(len(my_array1))) - print('len my_array2 = {}'.format(len(my_array2))) - print('len my_array3 = {}'.format(len(my_array3))) - - # Проверка вхождения элементов - print('10 in [1,2,2,3,4,5] -> {}'.format(10 in my_array1)) - print('1.5 in [1.5, 7.7, 123.1] -> {}'.format(1.5in my_array2)) - print("'g' in ['s','t','r','i','n','g'] -> {}".format('g' in my_array3)) - - # Проверка итераторов - for v in my_array1: - print(v, end=' ') - print() - for v in my_array2: - print(v, end=' ') - print() - for v in my_array3: - print(v, end=' ') - print() - itr = iter(my_array1) - while True: - try: - print(next(itr), end=' ') - except StopIteration: - break - print() - - # Переворот массивов - print("Reversed [1,2,2,3,4,5] is {}".format(list(reversed(my_array1)))) - print("Reversed [1.5, 7.7, 123.1] is {}".format(list(reversed(my_array2)))) - print("Reversed ['s','t','r','i','n','g'] is {}".format(list(reversed(my_array3)))) - - # Проверка вхождений индекса - print("3 on the {} place in [1,2,2,3,4,5]".format(my_array1.index(3))) - print("7.7 on the {} place in [1.5, 7.7, 123.1]".format(my_array2.index(7.7))) - print("i on the {} place in ['s','t','r','i','n','g']".format(my_array3.index('i'))) - - # Проверка количества элементов - print("count of 2 in [1,2,2,3,4,5] is {}".format(my_array1.count(2))) - print("count of 1.1 in [1.5, 7.7, 123.1] is {}".format(my_array2.count(1.1))) - print("count of n in ['s','t','r','i','n','g'] is {}".format(my_array3.count('n'))) - From db8726ae9c2cb6b7ea1be246ad442903bc332c23 Mon Sep 17 00:00:00 2001 From: Alexey Chubukov Date: Sun, 29 Mar 2020 20:44:35 +0300 Subject: [PATCH 5/5] Added me Iterator --- DZ1/ArrayList_Sequence.py | 59 ++++++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 16 deletions(-) diff --git a/DZ1/ArrayList_Sequence.py b/DZ1/ArrayList_Sequence.py index 6f00557..8856646 100644 --- a/DZ1/ArrayList_Sequence.py +++ b/DZ1/ArrayList_Sequence.py @@ -1,9 +1,36 @@ - from array import array + +class Iterator: + """Итератор для прохождения по коллекции""" + + def __init__(self, collection, start=0, end=-1, step=1): + self.collection = collection + if start < 0: + self.start = len(collection) + start + else: + self.start = start + if end < 0: + self.end = len(collection) + end + step + else: + self.end = end + step + self.step = step + self.current = self.start + + def __iter__(self): + return self + + def __next__(self): + if self.current == self.end: + raise StopIteration + current = self.collection[self.current] + self.current += self.step + return current + + class ArrayList: def __init__(self, type, init_list=[]): - self.data=array(type, init_list) + self.data = array(type, init_list) def __getitem__(self, item): return self.data[item] @@ -11,20 +38,21 @@ def __getitem__(self, item): def __len__(self): return len(self.data) - def __contains__(self, item): for i in range(len(self.data)): - if item==self.data[i]: + if item == self.data[i]: return True return False def __iter__(self): - return (self.data[i] for i in range(len(self.data))) + return Iterator(self.data) + # return (self.data[i] for i in range(len(self.data))) def __reversed__(self): - #for i in range(len(self.data)): + # for i in range(len(self.data)): # yield self.data[len(self.data)-i] - return (self.data[len(self.data)-1-i] for i in range(len(self.data))) + #return (self.data[len(self.data) - 1 - i] for i in range(len(self.data))) + return Iterator(self.data, -1, 0, -1) def index(self, item): for i in range(len(self.data)): @@ -35,15 +63,15 @@ def index(self, item): def count(self, item): counter = 0 for i in range(len(self.data)): - if self.data[i]==item: + if self.data[i] == item: counter += 1 return counter -if __name__=="__main__": - my_array1 = ArrayList('l',[1,2,2,3,4,5]) +if __name__ == "__main__": + my_array1 = ArrayList('l', [1, 2, 2, 3, 4, 5]) my_array2 = ArrayList('d', [1.5, 7.7, 123.1]) - my_array3 = ArrayList('u', ['s','t','r','i','n','g']) + my_array3 = ArrayList('u', ['s', 't', 'r', 'i', 'n', 'g']) # Проверка получения значения по индексу print('my_array1[0] = {}'.format(my_array1[0])) @@ -57,18 +85,18 @@ def count(self, item): # Проверка вхождения элементов print('10 in [1,2,2,3,4,5] -> {}'.format(10 in my_array1)) - print('1.5 in [1.5, 7.7, 123.1] -> {}'.format(1.5in my_array2)) + print('1.5 in [1.5, 7.7, 123.1] -> {}'.format(1.5 in my_array2)) print("'g' in ['s','t','r','i','n','g'] -> {}".format('g' in my_array3)) # Проверка итераторов for v in my_array1: - print(v, end=' ') + print(v, end=' ') print() for v in my_array2: - print(v, end=' ') + print(v, end=' ') print() for v in my_array3: - print(v, end=' ') + print(v, end=' ') print() itr = iter(my_array1) while True: @@ -92,4 +120,3 @@ def count(self, item): print("count of 2 in [1,2,2,3,4,5] is {}".format(my_array1.count(2))) print("count of 1.1 in [1.5, 7.7, 123.1] is {}".format(my_array2.count(1.1))) print("count of n in ['s','t','r','i','n','g'] is {}".format(my_array3.count('n'))) -