Untitled

 avatar
unknown
python
4 years ago
3.3 kB
5
Indexable
class Deque:
    def __init__(self, n):
        self.queue = [None] * n
        self.max_size = n
        self.head = 0
        self.tail = 0
        self.size_q = 0
        self.was_push = False

    def is_empty(self):
        return self.size() == 0

    def is_full(self):
        return self.size() == self.max_size

    def push_front(self, x):
        if self.is_full():
            raise IndexError
        self.queue[self.head] = x
        self.head = (self.head - 1) % self.max_size
        if not self.was_push:
            self.tail = (self.tail + 1) % self.max_size
            self.was_push = True
        self.size_q += 1

    def push_back(self, x):
        if self.is_full():
            raise IndexError
        self.queue[self.tail] = x
        self.tail = (self.tail + 1) % self.max_size
        if not self.was_push:
            self.head = (self.head - 1) % self.max_size
            self.was_push = True
        self.size_q += 1

    def pop_front(self):
        if self.is_empty():
            raise IndexError
        head = (self.head + 1) % self.max_size
        x = self.queue[head]
        self.queue[head] = None
        self.size_q -= 1
        self.head = head
        return x

    def pop_back(self):
        if self.is_empty():
            raise IndexError
        tail = (self.tail - 1) % self.max_size
        x = self.queue[tail]
        self.queue[tail] = None
        self.size_q -= 1
        self.tail = tail
        return x

    def size(self):
        return self.size_q


def result(num_of_commands, max_q, commands_list):
    i = 0
    queue = Deque(max_q)
    while i < num_of_commands:
        # if len(commands_list[i]) > 1:
        #     print(commands_list[i][0], commands_list[i][1])
        # else:
        #     print(commands_list[i][0])
        if commands_list[i][0] == 'push_front':
            try:
                queue.push_front(commands_list[i][1])
            except IndexError:
                print('error')
        if commands_list[i][0] == 'push_back':
            try:
                queue.push_back(commands_list[i][1])
            except IndexError:
                print('error')
        if commands_list[i][0] == 'pop_front':
            try:
                print(queue.pop_front())
            except IndexError:
                print('error')
        if commands_list[i][0] == 'pop_back':
            try:
                print(queue.pop_back())
            except IndexError:
                print('error')
        # print(queue.queue, queue.head, queue.tail)
        i += 1


def file_reader(file_address):
    commands_list = []
    with open(file_address, 'r') as reader:
        first_line, second_line, *commands_ = reader.readlines()
        num_of_commands = int(first_line)
        max_q = int(second_line)
        for command in commands_:
            cmd = command.strip().split(' ')
            if len(cmd) > 1:
                commands_list.append([cmd[0], int(cmd[1])])
            else:
                commands_list.append([cmd[0]])
    return num_of_commands, max_q, commands_list


if __name__ == '__main__':
    number_of_commands, max_queue, commands = file_reader('input.txt')
    result(number_of_commands, max_queue, commands)
Editor is loading...