-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathdeque.py
50 lines (33 loc) · 1.06 KB
/
deque.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from typing import Any, List
class Deque:
def __init__(self) -> None:
self._deque: List[Any] = []
def is_empty(self) -> bool:
return len(self._deque) == 0
def size(self) -> int:
return len(self._deque)
def add_front(self, item: Any):
self._deque.insert(0, item)
def add_rear(self, item: Any):
self._deque.append(item)
def remove_front(self) -> Any:
if self.is_empty():
raise Exception('Deque is empty')
return self._deque.pop(0)
def remove_rear(self) -> Any:
if self.is_empty():
raise Exception('Deque is empty')
return self._deque.pop()
def is_palindrome(string:str='') -> bool:
d: Deque = Deque()
for character in string:
d.add_rear(character)
palindrome: bool = True
while d.size() > 1 and palindrome:
if d.remove_front() != d.remove_rear():
palindrome = False
return palindrome
print(is_palindrome('radar'))
print(is_palindrome('radr'))
print(is_palindrome('r'))
print(is_palindrome(''))