def deleteInRangeMenu(linked_list: Linked_list):
    beforeList = copy.deepcopy(linked_list)
    print(f"From 0 to {len(linked_list) - 1}")
    l, r = v.int_validate_range()
    linked_list.remove_in_range(l, r)
    Event.do_some('deleteInRange', [beforeList, [l, r], linked_list])
    return linked_list
def main():
    """ Main function """
    Observer.attach('add', Logger.log)
    Observer.attach('delete', Logger.log)
    Observer.attach('deleteInRange', Logger.log)
    Observer.attach('taskMethod', Logger.log)

    linked_list1 = Linked_list()
    linked_list2 = Linked_list()
    options = ('🌝 🌝 🌝 🌝 🌝 🌝 🌝 🌝\n' + '🌜 1 - first list    🌛\n' +
               '🌜 2 - second list   🌛\n' + '🌜 3 - list method   🌛\n' +
               '🌜 4 - print lists   🌛\n' + '🌜 5 - exit          🌛\n' +
               '🌝 🌝 🌝 🌝 🌝 🌝 🌝 🌝\n')
    while True:
        try:
            print(options)
            choice = v.intValidateInRange('Enter choice ', 1, 5)
            if choice == 1: linked_list1 = generateList(linked_list1, 'l1')
            elif choice == 2: linked_list2 = generateList(linked_list2, 'l2')
            elif choice == 3:
                linked_list1, linked_list2 = listMethod(
                    linked_list1, linked_list2)
            elif choice == 4:
                print('list 1 - {}\nlist 2 - {}\n'.format(
                    linked_list1, linked_list2))
            elif choice == 5:
                break
        except Exception as e:
            print('Error ', '--' * 15, '  ', e)
def deleteAtMenu(linked_list: Linked_list, lname: str, position: int):
    print('Thread {}  : starting 🍺'.format(lname[1]))
    beforeList = copy.deepcopy(linked_list)
    linked_list.remove_at(position)
    Event.do_some('delete', [beforeList, position, linked_list, lname])
    print('Thread {}  : finishing 🍻'.format(lname[1]))
    return linked_list
def deleteInRangeMenu(linked_list: Linked_list, lname: str, pos):
    print('Thread {}  : starting 🍺'.format(lname[1]))
    beforeList = copy.deepcopy(linked_list)
    linked_list.remove_in_range(pos[0], pos[1])
    Event.do_some('deleteInRange',
                  [beforeList, [pos[0], pos[1]], linked_list, lname])
    print('Thread {}  : finishing 🍻'.format(lname[1]))
    return linked_list
    def do_algorithm(self, l: Linked_list, position: int):
        j = 0
        n = v.enterInteger("Enter n: ")
        itSm = iter(Smith())
        for i in range(n):
            data = next(itSm)
            l.insert(position+j, data)
            j += 1

        return l
Beispiel #6
0
 def do_algorithm(self, l: Linked_list, position: int):
     file_name = self.__enter_file_name()
     temp, j = self.__readFile(file_name), 0
     if len(l) == 0:
         l = temp
     else:
         for i in temp:
             l.insert(position + j, i)
             j += 1
     return l
Beispiel #7
0
 def do_algorithm(self, l: Linked_list, position: int):
     newList = copy.deepcopy(l)
     j = 0
     n = v.enterInteger("Enter n: ")
     itSm = iter(Smith())
     for i in range(n):
         data = next(itSm)
         l.insert(position + j, data)
         j += 1
     Event.do_some('add', [newList, position, l])
     return l
 def do_algorithm(self, l: Linked_list, position: int, lname: str):
     file_name = self.__enter_file_name()
     temp, j = self.__readFile(file_name), 0
     newList = copy.deepcopy(l)
     if len(l) == 0:
         l = temp
     else:
         for i in temp:
             l.insert(position + j, i)
             j += 1
     Event.do_some('add', [newList, position, l, lname])
     return l
def main():
    options = '1 - Strategy 1\n2 - Strategy 2\n3 - generate data\n4 - remove at\n5 - remove in range\n6 - list method\n7 - print list\n8 - exit\n'
    linked_list = Linked_list()
    context = Context(StrategyIterator)

    Observer.attach('add', Logger.log)
    Observer.attach('delete', Logger.log)
    Observer.attach('deleteInRange', Logger.log)
    Observer.attach('taskMethod', Logger.log)

    while True:
        try:
            print(options)
            choice = v.intValidateInRange('Enter choice ', 1, 8)

            if choice == 1: context.strategy = StrategyIterator()
            elif choice == 2: context.strategy = StrategyReadFile()
            elif choice == 3: linked_list = generateMenu(linked_list, context)
            elif choice == 4: linked_list = deleteAtMenu(linked_list)
            elif choice == 5: linked_list = deleteInRangeMenu(linked_list)
            elif choice == 6: linked_list = listMethodMenu(linked_list)
            elif choice == 7: print(linked_list)
            elif choice == 8: break

        except Exception as e:
            print('Error ', '--' * 15, '  ', e)
 def __readFile(fileName):
     try:
         with open(fileName) as f:
             for line in f:
                 l = Linked_list([int(x) for x in line.split()])
         return l
     except Exception as e:
         raise e
def listMethodMenu(linked_list: Linked_list):
    beforeList = copy.deepcopy(linked_list)
    l, r = linked_list.min_max(linked_list)
    j = 1
    if r < l:
        temp = l
        l = r
        r = temp

    print(f'left index - {l}, \nright index - {r}')
    for i in range((r - l) // 2):
        temp = linked_list.get_at(l + j)
        linked_list.set_at(l + j, linked_list.get_at(r - j))
        linked_list.set_at(r - j, temp)
        j += 1
    Event.do_some('taskMethod', [beforeList, [l, r], linked_list])
    return linked_list
def listMethodMenu(linked_list: Linked_list, lname: str, position=0):
    print('Thread {}  : starting 🍺'.format(lname[1]))
    beforeList = copy.deepcopy(linked_list)
    l, r = linked_list.min_max(linked_list)
    j = 1
    if r < l:
        temp = l
        l = r
        r = temp

    for i in range((r - l) // 2):
        temp = linked_list.get_at(l + j)
        linked_list.set_at(l + j, linked_list.get_at(r - j))
        linked_list.set_at(r - j, temp)
        j += 1
    Event.do_some('taskMethod', [beforeList, [l, r], linked_list, lname])
    print('Thread {}  : finishing 🍻'.format(lname[1]))
    return linked_list
Beispiel #13
0
def menu6(linked_list: Linked_list):
    l, r = linked_list.min_max(linked_list)
    j = 0

    if r < l:
        temp = l
        l = r
        r = temp
    if r - l == 1:
        size = 1
    else:
        size = (r - l) // 2

    print(f'left index - {l}, \nright index - {r}')

    for i in range(size):
        temp = linked_list.get_at(l + j)
        linked_list.set_at(l + j, linked_list.get_at(r - j))
        linked_list.set_at(r - j, temp)
        j += 1

    return linked_list
Beispiel #14
0
def main():
    options = '1 - Strategy 1\n2 - Strategy 2\n3 - generate data\n4 - remove at\n5 - remove in range\n6 - list method\n7 - print list\n8 - exit\n'
    linked_list = Linked_list()
    context = Context(StrategyIterator)
    while True:
        try:
            print(options)
            choice = v.intValidateInRange('Enter choice ', 1, 8)

            if choice == 1:
                context.strategy = StrategyIterator()

            elif choice == 2:
                context.strategy = StrategyReadFile()

            elif choice == 3:
                position = v.intValidateInRange('Enter position ', 0,
                                                len(linked_list))
                linked_list = context.do_some_business_logic(
                    linked_list, position)

            elif choice == 4:
                position = v.intValidateInRange('Enter position', 0,
                                                len(linked_list) - 1)
                linked_list.remove_at(position)

            elif choice == 5:
                print(f"From 0 to {len(linked_list) - 1}")
                l, r = v.int_validate_range()
                linked_list.remove_in_range(l, r)

            elif choice == 6:
                linked_list = menu6(linked_list)

            elif choice == 7:
                print(linked_list)

            elif choice == 8:
                break

        except Exception as e:
            print('Error ', '--' * 15, '  ', e)
def deleteAtMenu(linked_list: Linked_list):
    beforeList = copy.deepcopy(linked_list)
    position = v.intValidateInRange('Enter position', 0, len(linked_list) - 1)
    linked_list.remove_at(position)
    Event.do_some('delete', [beforeList, position, linked_list])
    return linked_list