mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 14:56:27 -04:00
cleanup
This commit is contained in:
parent
4d9c04e61d
commit
1d476139a8
17 changed files with 250 additions and 1 deletions
22
real_interview_problems/balanced.py
Normal file
22
real_interview_problems/balanced.py
Normal file
|
@ -0,0 +1,22 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
def balance_par_str_with_stack(str1):
|
||||
i, stack = 0, []
|
||||
|
||||
while i < len(str1):
|
||||
symbol = str1[i]
|
||||
if symbol == "(":
|
||||
stack.append(symbol)
|
||||
elif symbol == ")":
|
||||
stack.pop()
|
||||
i += 1
|
||||
return not stack
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(balance_par_str_with_stack('((()))'))
|
||||
print(balance_par_str_with_stack('(()'))
|
50
real_interview_problems/binary_search.py
Normal file
50
real_interview_problems/binary_search.py
Normal file
|
@ -0,0 +1,50 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
def binary_search(array, value):
|
||||
last, first = len(array), 0
|
||||
|
||||
while first < last:
|
||||
mid = (last - first)//2
|
||||
item = array[mid]
|
||||
|
||||
if item == value:
|
||||
return True
|
||||
|
||||
elif item < value:
|
||||
last = mid
|
||||
|
||||
else:
|
||||
first = mid
|
||||
|
||||
return False
|
||||
|
||||
def binary_search_rec(array, value, first=0, last=None):
|
||||
last = last or len(array)
|
||||
if len(array[first:last]) < 1:
|
||||
return False
|
||||
|
||||
mid = (last - first)//2
|
||||
if array[mid] == value:
|
||||
return True
|
||||
elif array[mid] < value:
|
||||
return binary_search_rec(array, value, first=first, last=mid)
|
||||
else:
|
||||
return binary_search_rec(array, value, first=mid, last=last)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
array = [3, 4, 6, 7, 10, 11, 34, 67, 84]
|
||||
value = 6
|
||||
assert(binary_search(array, value) == True)
|
||||
assert(binary_search_rec(array, value) == True)
|
||||
value = 8
|
||||
assert(binary_search(array, value) == False)
|
||||
assert(binary_search_rec(array, value) == False)
|
||||
array = [8]
|
||||
assert(binary_search(array, value) == True)
|
||||
assert(binary_search_rec(array, value) == True)
|
||||
array = []
|
||||
assert(binary_search(array, value) == False)
|
||||
assert(binary_search_rec(array, value) == False)
|
165
real_interview_problems/bst.py
Normal file
165
real_interview_problems/bst.py
Normal file
|
@ -0,0 +1,165 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
from collections import deque
|
||||
|
||||
class Node(object):
|
||||
|
||||
def __init__(self, item=None):
|
||||
|
||||
self.item = item
|
||||
self.left = None
|
||||
self.right = None
|
||||
|
||||
|
||||
def _add(self, value):
|
||||
new_node = Node(value)
|
||||
if not self.item:
|
||||
self.item = new_node
|
||||
else:
|
||||
if value > self.item:
|
||||
self.right = self.right and self.right._add(value) or new_node
|
||||
elif value < self.item:
|
||||
self.left = self.left and self.left._add(value) or new_node
|
||||
else:
|
||||
print("BSTs do not support repeated items.")
|
||||
return self
|
||||
|
||||
|
||||
def _search(self, value):
|
||||
if self.item == value:
|
||||
return True
|
||||
elif self.left and value < self.item:
|
||||
return self.left._search(value)
|
||||
elif self.right and value > self.item:
|
||||
return self.right._search(value)
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def _isLeaf(self):
|
||||
return not self.right and not self.left
|
||||
|
||||
|
||||
def _printPreorder(self):
|
||||
print self.item
|
||||
|
||||
if self.left:
|
||||
self.left._printPreorder()
|
||||
|
||||
if self.right:
|
||||
self.right._printPreorder()
|
||||
|
||||
|
||||
def _preorder_array(self):
|
||||
nodes = []
|
||||
if self.item:
|
||||
nodes.append(self.item)
|
||||
if self.left:
|
||||
nodes.extend(self.left._preorder_array())
|
||||
if self.right:
|
||||
nodes.extend(self.right._preorder_array())
|
||||
return nodes
|
||||
|
||||
|
||||
|
||||
class BST(object):
|
||||
|
||||
def __init__(self):
|
||||
self.root = None
|
||||
|
||||
def add(self, value):
|
||||
if not self.root:
|
||||
self.root = Node(value)
|
||||
else:
|
||||
self.root._add(value)
|
||||
|
||||
def printPreorder(self):
|
||||
if self.root:
|
||||
self.root._printPreorder()
|
||||
|
||||
def search(self, value):
|
||||
if self.root:
|
||||
return self.root._search(value)
|
||||
|
||||
def preorder_array(self):
|
||||
if self.root:
|
||||
return self.root._preorder_array()
|
||||
else:
|
||||
return 'Tree is empty.'
|
||||
|
||||
|
||||
|
||||
|
||||
def BFT(tree):
|
||||
current = tree.root
|
||||
nodes = []
|
||||
queue = deque()
|
||||
queue.append(current)
|
||||
|
||||
while queue:
|
||||
current = queue.popleft()
|
||||
nodes.append(current.item)
|
||||
if current.left:
|
||||
queue.append(current.left)
|
||||
if current.right:
|
||||
queue.append(current.right)
|
||||
|
||||
return nodes
|
||||
|
||||
|
||||
def preorder(tree, nodes=None):
|
||||
nodes = nodes or []
|
||||
if tree:
|
||||
nodes.append(tree.item)
|
||||
if tree.left:
|
||||
preorder(tree.left, nodes)
|
||||
if tree.right:
|
||||
preorder(tree.right, nodes)
|
||||
return nodes
|
||||
|
||||
|
||||
def postorder(tree, nodes=None):
|
||||
nodes = nodes or []
|
||||
if tree:
|
||||
if tree.left:
|
||||
nodes = postorder(tree.left, nodes)
|
||||
if tree.right:
|
||||
nodes = postorder(tree.right, nodes)
|
||||
nodes.append(tree.item)
|
||||
|
||||
return nodes
|
||||
|
||||
|
||||
def inorder(tree, nodes=None):
|
||||
nodes = nodes or []
|
||||
if tree:
|
||||
if tree.left:
|
||||
nodes = inorder(tree.left, nodes)
|
||||
nodes.append(tree.item)
|
||||
if tree.right:
|
||||
nodes = inorder(tree.right, nodes)
|
||||
return nodes
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
bst = BST()
|
||||
l = [10, 5, 6, 3, 8, 2, 1, 11, 9, 4]
|
||||
for i in l:
|
||||
bst.add(i)
|
||||
|
||||
print
|
||||
print "Searching for nodes 16 and 6:"
|
||||
print bst.search(16)
|
||||
print bst.search(6)
|
||||
|
||||
print
|
||||
print 'Traversals:'
|
||||
print 'Original: ', l
|
||||
print 'Preorder: ', preorder(bst.root)
|
||||
print 'Postorder: ', postorder(bst.root)
|
||||
print 'Inorder: ', inorder(bst.root)
|
||||
print 'BSF: ', BFT(bst)
|
38
real_interview_problems/check_anagram.py
Normal file
38
real_interview_problems/check_anagram.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
from collections import Counter
|
||||
|
||||
def check_if_anagram(word1, word2):
|
||||
counter = Counter()
|
||||
|
||||
for c in word1:
|
||||
counter[c] += 1
|
||||
|
||||
for c in word2:
|
||||
counter[c] -= 1
|
||||
|
||||
for values in counter.values():
|
||||
if values != 0:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
word1 = 'abc'
|
||||
word2 = 'bca'
|
||||
assert(check_if_anagram(word1, word2) == True)
|
||||
|
||||
word2 = 'bcd'
|
||||
assert(check_if_anagram(word1, word2) == False)
|
||||
|
||||
word1 = ''
|
||||
word2 = ''
|
||||
assert(check_if_anagram(word1, word2) == True)
|
||||
|
||||
word1 = 'a'
|
||||
word2 = 'a'
|
||||
assert(check_if_anagram(word1, word2) == True)
|
32
real_interview_problems/combination.py
Normal file
32
real_interview_problems/combination.py
Normal file
|
@ -0,0 +1,32 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
def combination(array):
|
||||
if len(array) < 2:
|
||||
return set(array)
|
||||
|
||||
result = set()
|
||||
for index, item in enumerate(array):
|
||||
new_array = array[:index] + array[index+1:]
|
||||
result.add(item)
|
||||
for perm in combination(new_array):
|
||||
new_item = ''.join(sorted(item + perm))
|
||||
result.add(new_item)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
array = ['a', 'b', 'c']
|
||||
result = set(['a', 'ac', 'ab', 'abc', 'bc', 'c', 'b'])
|
||||
assert(combination(array) == result)
|
||||
|
||||
array = ['']
|
||||
result = set([''])
|
||||
assert(combination(array) == result)
|
||||
|
||||
array = ['a']
|
||||
result = set(['a'])
|
||||
assert(combination(array) == result)
|
45
real_interview_problems/hash_table.py
Normal file
45
real_interview_problems/hash_table.py
Normal file
|
@ -0,0 +1,45 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
class HashTable(object):
|
||||
def __init__(self, slots=10):
|
||||
self.slots = slots
|
||||
self.table = []
|
||||
self.create_table()
|
||||
|
||||
# Get the slot
|
||||
def hash_key(self, value):
|
||||
return hash(value)%self.slots
|
||||
|
||||
# When creating the table, add list struct
|
||||
# to each slot
|
||||
def create_table(self):
|
||||
for i in range(self.slots):
|
||||
self.table.append([])
|
||||
|
||||
# Method to add a item in the right slot
|
||||
def add_item(self, value):
|
||||
key = self.hash_key(value)
|
||||
self.table[key].append(value)
|
||||
|
||||
# Aux: print table
|
||||
def print_table(self):
|
||||
for key in range(self.slots):
|
||||
print "Key is {0}, value is {1}.".format(key, self.table[key])
|
||||
|
||||
# Aux: find item
|
||||
def find_item(self, item):
|
||||
item_hash = self.hash_key(item)
|
||||
return item in self.table[item_hash]
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
dic = HashTable(5)
|
||||
for i in range(1, 40, 2):
|
||||
dic.add_item(i)
|
||||
|
||||
dic.print_table()
|
||||
assert(dic.find_item(20) == False)
|
||||
assert(dic.find_item(21) == True)
|
60
real_interview_problems/linked_list.py
Normal file
60
real_interview_problems/linked_list.py
Normal file
|
@ -0,0 +1,60 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
class Node(object):
|
||||
def __init__(self, value, next=None):
|
||||
self.value = value
|
||||
self.next = next
|
||||
|
||||
|
||||
class LinkedList(object):
|
||||
def __init__(self):
|
||||
self.head = None
|
||||
|
||||
def _add(self, value):
|
||||
self.head = Node(value, self.head)
|
||||
|
||||
def _printList(self):
|
||||
node = self.head
|
||||
while node:
|
||||
print node.value
|
||||
node = node.next
|
||||
|
||||
def _find(self, index):
|
||||
prev = None
|
||||
node = self.head
|
||||
i = 0
|
||||
while node and i < index:
|
||||
prev = node
|
||||
node = node.next
|
||||
i += 1
|
||||
return node, prev, i
|
||||
|
||||
def _delete(self, prev, node):
|
||||
if not prev:
|
||||
self.head = node.next
|
||||
else:
|
||||
prev.next = node.next
|
||||
|
||||
def deleteNode(self, index):
|
||||
node, prev, i = self._find(index)
|
||||
if index == i:
|
||||
self._delete(prev, node)
|
||||
else:
|
||||
print('Node with index {} not found'.format(index))
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
ll = LinkedList()
|
||||
for i in range(1, 5):
|
||||
ll._add(i)
|
||||
|
||||
print('The list is:')
|
||||
ll._printList()
|
||||
|
||||
print('The list after deleting node with index 2:')
|
||||
ll.deleteNode(2)
|
||||
ll._printList()
|
32
real_interview_problems/longest_common_prefix.py
Normal file
32
real_interview_problems/longest_common_prefix.py
Normal file
|
@ -0,0 +1,32 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
def longest_common_substring(s1, s2):
|
||||
p1 = 0
|
||||
aux, lcp = '', ''
|
||||
string1 = max(s1, s2)
|
||||
string2 = min(s1, s2)
|
||||
|
||||
while p1 < len(string1):
|
||||
p2 = 0
|
||||
while p2 < len(string2) and p1+p2 < len(string1):
|
||||
if string1[p1+p2] == string2[p2]:
|
||||
aux += string1[p1+p2]
|
||||
else:
|
||||
if len(lcp) < len(aux):
|
||||
lcp = aux
|
||||
aux = ''
|
||||
p2 += 1
|
||||
p1 += 1
|
||||
|
||||
return lcp
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
str1 = 'hasfgeaae'
|
||||
str2 = 'bafgekk'
|
||||
result = 'fge'
|
||||
assert(longest_common_substring(str1, str2) == result)
|
31
real_interview_problems/longest_increasing_subsequence.py
Normal file
31
real_interview_problems/longest_increasing_subsequence.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
def longest_increasing_subsequence(seq):
|
||||
result, aux = [], []
|
||||
seq.append(-float('infinity'))
|
||||
|
||||
for i, value in enumerate(seq[:-1]):
|
||||
aux.append(value)
|
||||
if value > seq[i+1]:
|
||||
if len(result) < len(aux):
|
||||
result = aux[:]
|
||||
aux = []
|
||||
return result
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
seq = [10, -12, 2, 3, -3, 5, -1, 2, -10]
|
||||
result = [-12, 2, 3]
|
||||
assert(longest_increasing_subsequence(seq) == result)
|
||||
|
||||
seq = [2]
|
||||
result = [2]
|
||||
assert(longest_increasing_subsequence(seq) == result)
|
||||
|
||||
seq = []
|
||||
result = []
|
||||
assert(longest_increasing_subsequence(seq) == result)
|
47
real_interview_problems/merge_sort.py
Normal file
47
real_interview_problems/merge_sort.py
Normal file
|
@ -0,0 +1,47 @@
|
|||
#!/usr/bin/env python
|
||||
# AKA: do you believe in magic?
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
def merge_sort(array):
|
||||
if len(array) < 2:
|
||||
return array
|
||||
|
||||
# divide
|
||||
mid = len(array)//2
|
||||
left = merge_sort(array[:mid])
|
||||
right = merge_sort(array[mid:])
|
||||
|
||||
# merge
|
||||
result = []
|
||||
i, j = 0, 0
|
||||
|
||||
while i < len(left) and j < len(right):
|
||||
if left[i] < right[j]:
|
||||
result.append(left[i])
|
||||
i += 1
|
||||
else:
|
||||
result.append(right[j])
|
||||
j+= 1
|
||||
|
||||
# make sure nothing is left behind
|
||||
if left[i:]:
|
||||
result.extend(left[i:])
|
||||
if right[j:]:
|
||||
result.extend(right[j:])
|
||||
|
||||
return result
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
array = [3, 1, 6, 0, 7, 19, 7, 2, 22]
|
||||
sorted = [0, 1, 2, 3, 6, 7, 7, 19, 22]
|
||||
assert(merge_sort(array) == sorted)
|
||||
|
||||
array = []
|
||||
assert(merge_sort(array) == array)
|
||||
|
||||
array = [1]
|
||||
assert(merge_sort(array) == array)
|
58
real_interview_problems/palindome.py
Normal file
58
real_interview_problems/palindome.py
Normal file
|
@ -0,0 +1,58 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
import string
|
||||
|
||||
def sanitize(sentence):
|
||||
array = sentence.lower()
|
||||
array = array.strip()
|
||||
array = array.strip(string.punctuation)
|
||||
return array
|
||||
|
||||
def check_if_palindrome(array):
|
||||
if len(array) < 2:
|
||||
return True
|
||||
|
||||
if array[0] == array[-1]:
|
||||
return check_if_palindrome(array[1:-1])
|
||||
else:
|
||||
return False
|
||||
|
||||
def check_if_palindrome_iter(array):
|
||||
i, j = 0, len(array)-1
|
||||
|
||||
while i <= j:
|
||||
if array[i] != array[j]:
|
||||
return False
|
||||
i += 1
|
||||
j -= 1
|
||||
|
||||
return True
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sentence = 'hello there'
|
||||
array = sanitize(sentence)
|
||||
assert(check_if_palindrome(array) == False)
|
||||
assert(check_if_palindrome_iter(array) == False)
|
||||
|
||||
sentence = ''
|
||||
array = sanitize(sentence)
|
||||
assert(check_if_palindrome(array) == True)
|
||||
assert(check_if_palindrome_iter(array) == True)
|
||||
|
||||
sentence = 'h'
|
||||
array = sanitize(sentence)
|
||||
assert(check_if_palindrome(array) == True)
|
||||
assert(check_if_palindrome_iter(array) == True)
|
||||
|
||||
sentence = 'Noel sees Leon'
|
||||
array = sanitize(sentence)
|
||||
assert(check_if_palindrome(array) == True)
|
||||
assert(check_if_palindrome_iter(array) == True)
|
||||
|
||||
sentence = 'Noel sees Leon!'
|
||||
array = sanitize(sentence)
|
||||
assert(check_if_palindrome(array) == True)
|
||||
assert(check_if_palindrome_iter(array) == True)
|
31
real_interview_problems/permutation.py
Normal file
31
real_interview_problems/permutation.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
def permutation(array):
|
||||
if len(array) < 2:
|
||||
return [array]
|
||||
|
||||
result = []
|
||||
for index, letter in enumerate(array):
|
||||
new_array = array[:index] + array[index+1:]
|
||||
for perm in permutation(new_array):
|
||||
result.append(letter + perm)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
word = 'abc'
|
||||
result = ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
|
||||
assert(permutation(word) == result)
|
||||
|
||||
word = ''
|
||||
result = ['']
|
||||
assert(permutation(word) == result)
|
||||
|
||||
word = 'a'
|
||||
result = ['a']
|
||||
assert(permutation(word) == result)
|
47
real_interview_problems/queue.py
Normal file
47
real_interview_problems/queue.py
Normal file
|
@ -0,0 +1,47 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
class Queue(object):
|
||||
def __init__(self):
|
||||
self.enq = []
|
||||
self.deq = []
|
||||
|
||||
def enqueue(self, item):
|
||||
return self.enq.append(item)
|
||||
|
||||
def deque(self):
|
||||
if not self.deq:
|
||||
while self.enq:
|
||||
self.deq.append(self.enq.pop())
|
||||
return self.deq.pop()
|
||||
|
||||
def peak(self):
|
||||
if not self.deq:
|
||||
while self.enq:
|
||||
self.deq.append(self.enq.pop())
|
||||
if self.deq:
|
||||
return self.deq[-1]
|
||||
|
||||
def size(self):
|
||||
return len(self.enq) + len(self.deq)
|
||||
|
||||
def isempty(self):
|
||||
return not (self.enq + self.deq)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
q = Queue()
|
||||
for i in range(1,11):
|
||||
q.enqueue(i)
|
||||
print 'Size:', q.size()
|
||||
print 'Is empty?', q.isempty()
|
||||
print 'Peak: ', q.peak()
|
||||
print
|
||||
print 'Dequeuing...'
|
||||
for i in range(10):
|
||||
print q.deque()
|
||||
print 'Size:', q.size()
|
||||
print 'Is empty?', q.isempty()
|
||||
print 'Peak: ', q.peak()
|
31
real_interview_problems/quick_sort.py
Normal file
31
real_interview_problems/quick_sort.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
def quick_sort(array):
|
||||
if len(array) < 2:
|
||||
return array
|
||||
|
||||
# partition
|
||||
ipivot = len(array)//2
|
||||
pivot = array[ipivot]
|
||||
new_array = array[:ipivot] + array[ipivot+1:]
|
||||
|
||||
left = [x for x in new_array if x <= pivot]
|
||||
right = [x for x in new_array if x > pivot]
|
||||
|
||||
return quick_sort(left) + [pivot] + quick_sort(right)
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
array = [3, 1, 6, 0, 7, 19, 7, 2, 22]
|
||||
sorted = [0, 1, 2, 3, 6, 7, 7, 19, 22]
|
||||
assert(quick_sort(array) == sorted)
|
||||
|
||||
array = []
|
||||
assert(quick_sort(array) == array)
|
||||
|
||||
array = [1]
|
||||
assert(quick_sort(array) == array)
|
40
real_interview_problems/reverse_str.py
Normal file
40
real_interview_problems/reverse_str.py
Normal file
|
@ -0,0 +1,40 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
def reverse_str_inplace(_str):
|
||||
if len(_str) < 2:
|
||||
return _str
|
||||
return _str[-1] + reverse_str(_str[1:-1]) + _str[0]
|
||||
|
||||
|
||||
def reverse_str(_str):
|
||||
result = ''
|
||||
j = len(_str) - 1
|
||||
|
||||
while j >= 0:
|
||||
result += _str[j]
|
||||
|
||||
return result
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
_str = ''
|
||||
result = ''
|
||||
assert(reverse_str(_str) == result)
|
||||
assert(reverse_str_inplace(_str) == result)
|
||||
|
||||
_str = 'a'
|
||||
result = 'a'
|
||||
assert(reverse_str(_str) == result)
|
||||
assert(reverse_str_inplace(_str) == result)
|
||||
|
||||
_str = 'abcde'
|
||||
result = 'edcba'
|
||||
assert(reverse_str(_str) == result)
|
||||
assert(reverse_str_inplace(_str) == result)
|
||||
|
||||
_str = 'abcdef'
|
||||
result = 'fedcba'
|
||||
assert(reverse_str(_str) == result)
|
||||
assert(reverse_str_inplace(_str) == result)
|
40
real_interview_problems/stack.py
Normal file
40
real_interview_problems/stack.py
Normal file
|
@ -0,0 +1,40 @@
|
|||
|
||||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
class Stack(object):
|
||||
def __init__(self):
|
||||
self.content = []
|
||||
|
||||
def push(self, value):
|
||||
self.content.append(value)
|
||||
|
||||
def pop(self):
|
||||
if self.content:
|
||||
return self.content.pop()
|
||||
else:
|
||||
return 'Empty List. '
|
||||
|
||||
def size(self):
|
||||
return len(self.content)
|
||||
|
||||
def isEmpty(self):
|
||||
return not bool(self.content)
|
||||
|
||||
def peek(self):
|
||||
if self.content:
|
||||
return self.content[-1]
|
||||
else:
|
||||
print('Stack is empty.')
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
q = Stack()
|
||||
|
||||
for i in range(10):
|
||||
q.push(i)
|
||||
for i in range(11):
|
||||
print q.pop()
|
Loading…
Add table
Add a link
Reference in a new issue