def __init__(self): self.capacity = 2 self.__size = 0 self.queue = [0] * self.capacity self.front = 0 self.rear = 0 ## a monotonic dequeue to maintain the min value , the val in decreasing order in the dequeue self.__min_dequeue = Dequeue()
def test_add_rear(self): """Test add rear method.""" d = Dequeue() self.assertTrue(d._items == []) d.add_rear(1) d.add_rear(2) self.assertEqual(d._items, [1, 2])
def test_add_front(self): """Test add front method.""" d = Dequeue() self.assertTrue(d._items == []) d.add_front(1) d.add_front(2) self.assertEqual(d._items, [2, 1])
def palindrome_checker(word): """Return True if word reads the same forward and backward.""" d = Dequeue() for character in word: d.add_rear(character) backwards = "".join([d.remove_rear() for character in word]) if word == backwards: return True return False
def test_dequeue(self): dequeue = Dequeue() dequeue.push(1) dequeue.push(2) dequeue.push(3) dequeue.push(4) self.assertEqual(1, dequeue.pop()) self.assertEqual(4, dequeue.leftpop()) self.assertEqual(2, dequeue.pop()) self.assertEqual(3, dequeue.leftpop())
class PalindromeChecker: def __init__(self): self.dq = Dequeue() def check_if_palindrome(self, word): self.dq.items = list(word) is_palindrome = True while self.dq.size() > 1: first_letter = self.dq.remove_front() last_letter = self.dq.remove_rear() if first_letter != last_letter: is_palindrome = False return is_palindrome
def test_peek_front_rear(self): d = Dequeue() d.add_front("apple") d.add_front("banana") d.add_front("cherry") self.assertEqual(d.items, ["cherry", "banana", "apple"]) self.assertEqual(d.peek_front(), "cherry") self.assertEqual(d.peek_rear(), "apple")
def test_remove_front(self): d = Dequeue() d.add_front("apple") d.add_front("banana") d.add_front("cherry") self.assertEqual(d.remove_front(), "cherry") self.assertEqual(d.items, ["banana", "apple"])
def check_palindrome(text): D = Dequeue() for i in text: D.addBack(i) while D.size() > 1: if D.removeBack() == D.removeFront(): return False return True
def palchecker(str): d = Dequeue() for ch in str: d.addRear(ch) for _ in range(int(d.size() / 2)): if (d.removeFront() != d.removeRear()): return False return True
def PalindromeDetector(string): dq = Dequeue() for letter in string: dq.add_rear(letter) while dq.size() > 1: if dq.remove_front() != dq.remove_rear(): return False return True
def palChecker(word): dq = Dequeue() for char in word: dq.addRear(char) while dq.size() > 1: if dq.removeFront() != dq.removeRear(): return False return True
def check_palindrome(string_to_check): dq = Dequeue() dq.set_items(string_to_check) is_palindrome = True while dq.size() > 1: if dq.remove_front() != dq.remove_rear(): is_palindrome = False return is_palindrome
def test_remove_rear(self): """Test remove rear method.""" d = Dequeue() d.add_rear(1) d.add_rear(2) d.add_rear(3) self.assertEqual(d._items, [1, 2, 3]) rear = d.remove_rear() self.assertEqual(rear, 3) self.assertEqual(d._items, [1, 2])
def test_remove_front(self): """Test remove front method.""" d = Dequeue() d.add_rear(1) d.add_rear(2) d.add_rear(3) self.assertEqual(d._items, [1, 2, 3]) front = d.remove_front() self.assertEqual(front, 1) self.assertEqual(d._items, [2, 3])
def test_size(self): """Test size method.""" d = Dequeue() self.assertEqual(d.size(), 0) n = 10 for number in range(1, n): d.add_rear(1) self.assertEqual(d.size(), number)
def isPalindrome(string): dequeue_string = Dequeue() for i in string: dequeue_string.addFront(i) for i in dequeue_string.items: if dequeue_string.removeFront() != dequeue_string.removeRear(): return False return True
def checkPalindrome(input_string): dequeue = Dequeue() for character in input_string: dequeue.addRear(character) stillEqual = True while (dequeue.size() > 1 and stillEqual): first = dequeue.delFront() last = dequeue.delRear() if first != last: stillEqual = False return stillEqual
def check_palindrome(string_to_check): sanitised_string = re.sub("[^a-zA-Z]","", string_to_check).lower() dq = Dequeue() dq.set_items(sanitised_string) is_palindrome = True while dq.size() > 1: if dq.remove_front() != dq.remove_rear(): is_palindrome = False return is_palindrome
def palindromeChecker(str): charDequeue = Dequeue() for ch in str: charDequeue.addRear(ch) stillEqual = True while charDequeue.size() > 1 and stillEqual: first = charDequeue.removeFront() second = charDequeue.removeRear() if first != second: stillEqual = False return stillEqual
def pal_checker(mystr): """检查字符串是否为回文字符串,即正序等于逆序字符串""" dq = Dequeue() mylist = [i for i in mystr] indicator = True for item in mylist: dq.addRear(item) for i in range(dq.size() // 2): fornt = dq.removeFront() rear = dq.removeRear() if fornt != rear: indicator = False break return indicator
def pal_checker(word): chardequeue = Dequeue() for char in word: chardequeue.add_rear(char) still_equal = True while chardequeue.size() > 1 and still_equal: first = chardequeue.remove_front() last = chardequeue.remove_rear() if first != last: still_equal = False return still_equal
def palin_check(string): d=Dequeue() for ch in string: d.addRear(ch) equals=True while d.size()>1 and equals: if d.removeFront() is d.removeRear(): pass else: equals=False return equals
def test_is_empty(self): d = Dequeue() self.assertEqual(d.is_empty(), True) d.add_front("apple") self.assertEqual(d.is_empty(), False)
class MinQueue(): def __init__(self): self.capacity = 2 self.__size = 0 self.queue = [0] * self.capacity self.front = 0 self.rear = 0 ## a monotonic dequeue to maintain the min value , the val in decreasing order in the dequeue self.__min_dequeue = Dequeue() def append(self, e): if self.full(): self.__adjust_capacity() idx = self.rear self.queue[self.rear] = e self.rear = (self.rear + 1) % self.capacity self.__size += 1 self.__add_to_min_queue(idx) def pop(self): if self.empty(): return None e = self.queue[self.front] self.front = (self.front + 1) % self.capacity self.__size -= 1 while not self.__min_dequeue.empty() and self.__isvalid_index( self.__min_dequeue.peak()) == False: self.__min_dequeue.pop() return e def __isvalid_index(self, idx): if self.empty(): return False if self.full(): return True if self.rear > self.front: return True if self.front <= idx < self.rear else False if self.rear < self.front: return True if (idx >= self.front or idx < self.rear) else False return False def __add_to_min_queue(self, idx): while not self.__min_dequeue.empty() and self.queue[idx] <= self.queue[ self.__min_dequeue.peakRight()]: self.__min_dequeue.popRight() self.__min_dequeue.append(idx) def min(self): if self.empty(): return None return self.queue[self.__min_dequeue.peak()] def peek(self): return None if self.empty() else self.queue[self.rear] def empty(self): return self.__size == 0 def full(self): return self.__size == self.capacity def size(self): return self.__size def __adjust_capacity(self): if not self.full(): return new_capacity = self.capacity * 2 new_queue = [0] * new_capacity idx_in_queue = self.front idx_in_new_queue = 0 e = self.queue[idx_in_queue] new_queue[idx_in_new_queue] = e idx_in_queue = (idx_in_queue + 1) % self.capacity idx_in_new_queue += 1 while idx_in_queue != self.front: e = self.queue[idx_in_queue] new_queue[idx_in_new_queue] = e idx_in_queue = (idx_in_queue + 1) % self.capacity idx_in_new_queue += 1 self.queue = new_queue self.front = 0 self.rear = self.capacity self.capacity = new_capacity
def balanced_parentheses(s): deq = Dequeue() for c in s: deq.pushBack(c) if b(deq.peekFront()) != b(deq.peekBack()): return False while deq.size() > 0: front = deq.popFront() while b(front) != b(deq.peekBack()) and deq.peekBack() != None: back = deq.popBack() back = deq.popBack() return deq.size() == 0 and b(front) == b(back)
def test_is_empty(self): """Test is empty method.""" d = Dequeue() self.assertTrue(d.is_empty()) d.add_front(1) self.assertFalse(d.is_empty())
def create_dequeue(self, string): dequeue= Dequeue() for char in string: if not char.isspace() and char.isalnum(): dequeue.add_front(char) return dequeue
#dequeue-test-cases #Rebecca Brunner #29 August 2018 #Test dequeue class structure and methods from random import * from dequeue import Dequeue #Create deqeueue d1 = Dequeue() for i in range(5): d1.pushBack(randint(1, 5)) print("Dequeue 1 is: ", d1.items) d2 = Dequeue() for i in range(5): d2.pushBack(randint(1, 5)) print("Dequeue 2 is: ", d2.items) d1.insertion_sort() print("Dequeue 1, sorted with an insertion sort: ", d1.items) d2.merge_sort() print("Dequeue 2, sorted with a merge sort:", d2.items) if (d1.binary_search(4)) != None: print("Searching for 4 in d1 with a binary search, 4 is in position ", d1.binary_search(4)) else: print("Searching for 4 in d1 with a binary search, 4 was not found.") if (d2.binary_search(4)) != None: print("Searching for 4 in d2 with a binary search, 4 is in position ",
def __init__(self): self.dq = Dequeue()
__author__ = 'Priyadharshini' #application of Dequeue to check a palindrome from dequeue import Dequeue def palchecker(palstring, q): for element in palstring: q.insertRear(element) palflag = True while q.size() > 1 and palflag is True: if q.removeFront() == q.removeRear(): continue else: palflag = False return palflag if __name__ == "__main__": q = Dequeue() result = palchecker("radar", q) if result == True: print("palindrome") else: print("not a palindrome")