You are viewing a single comment's thread. Return to all comments →
I have Solved It in other way. Tell me please if it is optimal approach or not for this sollution.
from collections import deque import sys class Node: def __init__(self, data,next=None): self.data = data self.next = next class LinkedList: def __init__(self): self.head = None self.tail = None self.length = 0 def isempty(self): return self.head is None def get_head(self): return self.head def get_tail(self): return self.tail def top(self): return self.head.data if not self.isempty() else None def end(self): return self.tail.data if not self.isempty() else None def appendleft(self, data, type = 0): if type == 1 or self.isempty(): d = deque() d.append(data) node = Node(d, self.head) self.head = node self.length += 1 if self.length == 3: self.head.next.next = None self.length -= 1 if self.length == 1: self.tail = self.head return self.tail = self.head.next elif type == 0: self.head.data.append(data) elif type == 2: node = Node(data) self.head = node def append(self, data = None, type = 0): if type == 1 or self.isempty() or type == 2: d = deque() if type ==2: d.append(data) node = Node(data = d) self.length += 1 if self.isempty(): self.head = node self.tail = node return self.tail.next = node self.tail = node elif type == 0: self.tail.data.append(data) def sprint(self): itr = self.head s = "" while itr: s += str(itr.data) + " " itr = itr.next sys.__stdout__.write(s+"\n") if __name__ == '__main__': prime = [2,3,5,7,11,13,....9721,9733] # list of all the first 1200 prime numbers n, q = map(int, sys.stdin.readline().strip().split()) A = LinkedList() B = LinkedList() h = map(int, sys.stdin.readline().strip().split()[:n]) for i in h: A.appendleft(i) for i in range(q): ai = deque() B.append(type = 1) while A.top(): if A.top()[0] % prime[i] == 0: data = A.top().popleft() B.append(data) # B.sprint() else: ai.appendleft(A.top().popleft()) A.appendleft(ai, type = 2) result = deque() # print("B:", end=" "); B.sprint() # print("A:", end=" "); A.sprint() while B.get_head(): while B.get_head().data: result.append(B.get_head().data.popleft()) B.head = B.get_head().next while A.get_head().data: result.append(A.get_head().data.pop()) print("\n".join(map(str, result)))
Seems like cookies are disabled on this browser, please enable them to open this website
Waiter
You are viewing a single comment's thread. Return to all comments →
I have Solved It in other way. Tell me please if it is optimal approach or not for this sollution.