def isPalindrome(word): palindrome = False comp1 = StackADT.newStack() comp2 = QueueADT.newQueue() i = 0 letter = word[i] for letter in word: StackADT.push(comp1, letter) QueueADT.Enqueue(comp2, letter) i = i + 1 i = 0 letter = word[i] item1 = '' item2 = '' for letter in word: item1 = item1 + StackADT.pop(comp1) item2 = item2 + QueueADT.Dequeue(comp2) i = i + 1 if (item1 == item2): palindrome = True return palindrome else: palindrome = False return palindrome
def test_dequeue(self): q = QueueADT(3) q.enqueue(1) q.enqueue(2) q.enqueue(3) self.assertEqual(1, q.dequeue()) self.assertEqual(2, q.dequeue()) self.assertEqual(3, q.dequeue())
def reverseK(self, q, k): qMod = QueueADT(q.size()) qTemp = QueueADT(q.size()) sTemp = StackADT() i = 0 while not q.is_empty(): n = q.dequeue() if i < k: sTemp.push(n) else: qTemp.enqueue(n) i += 1 while not sTemp.is_empty(): qMod.enqueue(sTemp.pop()) while not qTemp.is_empty(): qMod.enqueue(qTemp.dequeue()) return qMod
i = 0 while not q.is_empty(): n = q.dequeue() if i < k: sTemp.push(n) else: qTemp.enqueue(n) i += 1 while not sTemp.is_empty(): qMod.enqueue(sTemp.pop()) while not qTemp.is_empty(): qMod.enqueue(qTemp.dequeue()) return qMod if __name__ == "__main__": data = [12, 34, 65, 76, 23, 12, 36, 90] qOri = QueueADT(len(data)) for d in data: qOri.enqueue(d) algo = QueueReverseK() qMod = algo.reverseK(qOri, 4) data_mod = [] while not qMod.is_empty(): data_mod.append(qMod.dequeue()) print("Original", data) print("Reversed", data_mod)
def test_is_full_and_is_empty(self): q = QueueADT(3) self.assertEqual(False, q.is_full()) self.assertEqual(True, q.is_empty()) q.enqueue(1) self.assertEqual(False, q.is_full()) self.assertEqual(False, q.is_empty()) q.enqueue(2) self.assertEqual(False, q.is_full()) self.assertEqual(False, q.is_empty()) q.enqueue(3) self.assertEqual(True, q.is_full()) self.assertEqual(False, q.is_empty())
def test_init(self): q = QueueADT(10)
def test_size(self): q = QueueADT(3) self.assertEqual(0, q.size()) q.enqueue(1) self.assertEqual(1, q.size()) q.enqueue(2) self.assertEqual(2, q.size()) q.enqueue(3) self.assertEqual(3, q.size()) q.dequeue() self.assertEqual(2, q.size()) q.dequeue() self.assertEqual(1, q.size()) q.dequeue() self.assertEqual(0, q.size())
def test_enqueue(self): q = QueueADT(3) q.enqueue(1) q.enqueue(2) q.enqueue(3)