mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-30 04:36:08 -04:00
add boilerplate for second edition
This commit is contained in:
parent
dc3ebf3173
commit
5fafebba15
15
first_edition/README.md
Normal file
15
first_edition/README.md
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
## Python, Algorithms and Data Structures (2014)
|
||||||
|
|
||||||
|
[Download PDF (first edition, published by Hanbit Media)](https://github.com/bt3gl/Python-and-Algorithms-and-Data-Structures/blob/master/First_edition_2014/ebook_pdf/book_second_edition.pdf).
|
||||||
|
|
||||||
|
|
||||||
|
## To run the snippet:
|
||||||
|
|
||||||
|
Install dependencies in a [virtual environment](https://coderwall.com/p/8-aeka):
|
||||||
|
|
||||||
|
```
|
||||||
|
virtualenv venv
|
||||||
|
source venv/bin/activate
|
||||||
|
pip install -r requirements.txt
|
||||||
|
```
|
||||||
|
|
BIN
first_edition/ebook_pdf/book_second_edition.pdf
Normal file
BIN
first_edition/ebook_pdf/book_second_edition.pdf
Normal file
Binary file not shown.
41
first_edition/ebook_src/abstract_structures/HashTableClass.py
Executable file
41
first_edition/ebook_src/abstract_structures/HashTableClass.py
Executable file
@ -0,0 +1,41 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
class HashTable(object):
|
||||||
|
def __init__(self, slots=10):
|
||||||
|
self.slots = slots
|
||||||
|
self.table = []
|
||||||
|
self.create_table()
|
||||||
|
|
||||||
|
def hash_key(self, value):
|
||||||
|
return hash(value)%self.slots
|
||||||
|
|
||||||
|
def create_table(self):
|
||||||
|
for i in range(self.slots):
|
||||||
|
self.table.append([])
|
||||||
|
|
||||||
|
def add_item(self, value):
|
||||||
|
key = self.hash_key(value)
|
||||||
|
self.table[key].append(value)
|
||||||
|
|
||||||
|
def print_table(self):
|
||||||
|
for key in range(len(self.table)):
|
||||||
|
print "Key is %s, value is %s." %(key, self.table[key])
|
||||||
|
|
||||||
|
def find_item(self, item):
|
||||||
|
pos = self.hash_key(item)
|
||||||
|
if item in self.table[pos]:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
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)
|
47
first_edition/ebook_src/abstract_structures/QueueClass.py
Executable file
47
first_edition/ebook_src/abstract_structures/QueueClass.py
Executable file
@ -0,0 +1,47 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# Time: 5 min
|
||||||
|
|
||||||
|
__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()
|
63
first_edition/ebook_src/abstract_structures/Stack.py
Executable file
63
first_edition/ebook_src/abstract_structures/Stack.py
Executable file
@ -0,0 +1,63 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
class Stack(object):
|
||||||
|
def __init__(self):
|
||||||
|
self.content = []
|
||||||
|
self.min_array = []
|
||||||
|
self.min = float('inf')
|
||||||
|
|
||||||
|
def push(self, value):
|
||||||
|
if value < self.min:
|
||||||
|
self.min = value
|
||||||
|
|
||||||
|
self.content.append(value)
|
||||||
|
self.min_array.append(self.min)
|
||||||
|
|
||||||
|
def pop(self):
|
||||||
|
if self.content:
|
||||||
|
value = self.content.pop()
|
||||||
|
self.min_array.pop()
|
||||||
|
if self.min_array:
|
||||||
|
self.min = self.min_array[-1]
|
||||||
|
return value
|
||||||
|
else:
|
||||||
|
return 'Empty List. '
|
||||||
|
|
||||||
|
def find_min(self):
|
||||||
|
if self.min_array:
|
||||||
|
return self.min_array[-1]
|
||||||
|
else:
|
||||||
|
return 'No min value for 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.')
|
||||||
|
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return '{}'.format(self.content)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
q = Stack()
|
||||||
|
|
||||||
|
for i in range(15,20):
|
||||||
|
q.push(i)
|
||||||
|
for i in range(10,5,-1):
|
||||||
|
q.push(i)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
for i in range(1, 13):
|
||||||
|
print q.pop(), q.find_min()
|
1
first_edition/ebook_src/abstract_structures/__init__.py
Executable file
1
first_edition/ebook_src/abstract_structures/__init__.py
Executable file
@ -0,0 +1 @@
|
|||||||
|
__all__=["hash_tables", "heap", "linked_list", "queues", "stacks"]
|
@ -0,0 +1,51 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
import heapq
|
||||||
|
|
||||||
|
def find_N_largest_items_seq(seq, N):
|
||||||
|
''' find the N largest items in a sequence '''
|
||||||
|
return heapq.nlargest(N,seq)
|
||||||
|
|
||||||
|
def find_N_smallest_items_seq(seq, N):
|
||||||
|
''' find the N smallest items in a sequence '''
|
||||||
|
return heapq.nsmallest(N, seq)
|
||||||
|
|
||||||
|
def find_smallest_items_seq_heap(seq):
|
||||||
|
''' find the smallest items in a sequence using heapify first'''
|
||||||
|
''' heap[0] is always the smallest item '''
|
||||||
|
''' pops the smallest item, O(logN) '''
|
||||||
|
heapq.heapify(seq)
|
||||||
|
return heapq.heappop(seq)
|
||||||
|
|
||||||
|
def find_smallest_items_seq(seq):
|
||||||
|
''' if it is only one item, min() is faster '''
|
||||||
|
return min(seq)
|
||||||
|
|
||||||
|
def find_N_smallest_items_seq_sorted(seq, N):
|
||||||
|
''' N ~ len(seq), better sort instead'''
|
||||||
|
return sorted(seq)[:N]
|
||||||
|
|
||||||
|
def find_N_largest_items_seq_sorted(seq, N):
|
||||||
|
''' N ~ len(seq), better sort instead'''
|
||||||
|
return sorted(seq)[len(seq)-N:]
|
||||||
|
|
||||||
|
|
||||||
|
def test_find_N_largest_smallest_items_seq(module_name='this module'):
|
||||||
|
seq = [1, 3, 2, 8, 6, 10, 9]
|
||||||
|
N = 3
|
||||||
|
assert(find_N_largest_items_seq(seq, N) == [10, 9, 8])
|
||||||
|
assert(find_N_largest_items_seq_sorted(seq, N) == [8, 9, 10])
|
||||||
|
assert(find_N_smallest_items_seq(seq, N) == [1,2,3])
|
||||||
|
assert(find_N_smallest_items_seq_sorted(seq, N) == [1,2,3])
|
||||||
|
assert(find_smallest_items_seq(seq) == 1)
|
||||||
|
assert(find_smallest_items_seq_heap(seq) == 1)
|
||||||
|
|
||||||
|
s = 'Tests in {name} have {con}!'
|
||||||
|
print(s.format(name=module_name, con='passed'))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
test_find_N_largest_smallest_items_seq()
|
||||||
|
|
49
first_edition/ebook_src/abstract_structures/heap/heapify.py
Normal file
49
first_edition/ebook_src/abstract_structures/heap/heapify.py
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
class Heapify(object):
|
||||||
|
def __init__(self, data=None):
|
||||||
|
self.data = data or []
|
||||||
|
for i in range(len(data)//2, -1, -1):
|
||||||
|
self.__max_heapify__(i)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return '{}'.format(self.data)
|
||||||
|
|
||||||
|
def parent(self, i):
|
||||||
|
return i >> 1
|
||||||
|
|
||||||
|
def left_child(self, i):
|
||||||
|
return (i << 1) + 1
|
||||||
|
|
||||||
|
def right_child(self, i):
|
||||||
|
return (i << 1) + 2 # +2 instead of +1 because it's 0-indexed.
|
||||||
|
|
||||||
|
def __max_heapify__(self, i):
|
||||||
|
largest = i
|
||||||
|
left = self.left_child(i)
|
||||||
|
right = self.right_child(i)
|
||||||
|
n = len(self.data)
|
||||||
|
largest = (left < n and self.data[left] > self.data[i]) and left or i
|
||||||
|
largest = (right < n and self.data[right] > self.data[largest]) and right or largest
|
||||||
|
if i is not largest:
|
||||||
|
self.data[i], self.data[largest] = self.data[largest], self.data[i]
|
||||||
|
self.__max_heapify__(largest)
|
||||||
|
|
||||||
|
def extract_max(self):
|
||||||
|
n = len(self.data)
|
||||||
|
max_element = self.data[0]
|
||||||
|
self.data[0] = self.data[n - 1]
|
||||||
|
self.data = self.data[:n - 1]
|
||||||
|
self.__max_heapify__(0)
|
||||||
|
return max_element
|
||||||
|
|
||||||
|
|
||||||
|
def test_Heapify():
|
||||||
|
l1 = [3, 2, 5, 1, 7, 8, 2]
|
||||||
|
h = Heapify(l1)
|
||||||
|
assert(h.extract_max() == 8)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
test_Heapify()
|
@ -0,0 +1,27 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
import heapq
|
||||||
|
|
||||||
|
|
||||||
|
def merge_sorted_seq(seq1, seq2):
|
||||||
|
''' merge two sorted sequences with little ovehead. the result
|
||||||
|
will be sorted, which is different of doing just +'''
|
||||||
|
result = []
|
||||||
|
for c in heapq.merge(seq1, seq2):
|
||||||
|
result.append(c)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def test_merge_sorted_seq(module_name='this module'):
|
||||||
|
seq1 = [1, 2, 3, 8, 9, 10]
|
||||||
|
seq2 = [2, 3, 4, 5, 6, 7, 9]
|
||||||
|
seq3 = seq1 + seq2
|
||||||
|
assert(merge_sorted_seqseq1, seq2) == sorted(seq3))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
test_merge_sorted_seq()
|
||||||
|
|
@ -0,0 +1,40 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
import heapq
|
||||||
|
|
||||||
|
class PriorityQueue(object):
|
||||||
|
''' implements a priority queue class '''
|
||||||
|
def __init__(self):
|
||||||
|
self._queue = []
|
||||||
|
self._index = 0 # comparying same priority level
|
||||||
|
|
||||||
|
def push(self, item, priority):
|
||||||
|
heapq.heappush(self._queue, (-priority, self._index, item))
|
||||||
|
self._index += 1
|
||||||
|
|
||||||
|
def pop(self):
|
||||||
|
return heapq.heappop(self._queue)[-1]
|
||||||
|
|
||||||
|
|
||||||
|
class Item:
|
||||||
|
def __init__(self, name):
|
||||||
|
self.name = name
|
||||||
|
def __repr__(self):
|
||||||
|
return "Item({!r})".format(self.name)
|
||||||
|
|
||||||
|
|
||||||
|
def test_PriorityQueue():
|
||||||
|
''' push and pop are all O(logN) '''
|
||||||
|
q = PriorityQueue()
|
||||||
|
q.push(Item('test1'), 1)
|
||||||
|
q.push(Item('test2'), 4)
|
||||||
|
q.push(Item('test3'), 3)
|
||||||
|
assert(str(q.pop()) == "Item('test2')")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
test_PriorityQueue()
|
||||||
|
|
||||||
|
|
@ -0,0 +1,55 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
''' implement a function to see whether a linked list is circular.
|
||||||
|
To implement this, we just need two pointers with different
|
||||||
|
paces (for example, one goes twice faster)'''
|
||||||
|
|
||||||
|
from linked_list_fifo import LinkedListFIFO
|
||||||
|
from node import Node
|
||||||
|
|
||||||
|
|
||||||
|
class cicularLinkedListFIFO(LinkedListFIFO):
|
||||||
|
def _add(self, value):
|
||||||
|
self.length += 1
|
||||||
|
node = Node(value, self.head)
|
||||||
|
if self.tail:
|
||||||
|
self.tail.pointer = node
|
||||||
|
self.tail = node
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def isCircularll(ll):
|
||||||
|
p1 = ll.head
|
||||||
|
p2 = ll.head
|
||||||
|
|
||||||
|
while p2:
|
||||||
|
try:
|
||||||
|
p1 = p1.pointer
|
||||||
|
p2 = p2.pointer.pointer
|
||||||
|
except:
|
||||||
|
break
|
||||||
|
|
||||||
|
if p1 == p2:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
ll = LinkedListFIFO()
|
||||||
|
for i in range(10):
|
||||||
|
ll.addNode(i)
|
||||||
|
ll._printList()
|
||||||
|
|
||||||
|
print(isCircularll(ll))
|
||||||
|
|
||||||
|
lcirc = cicularLinkedListFIFO()
|
||||||
|
for i in range(10):
|
||||||
|
lcirc.addNode(i)
|
||||||
|
print(isCircularll(lcirc))
|
||||||
|
|
@ -0,0 +1,81 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
''' Implement a double-linked list, which is very simple, we just need inherits
|
||||||
|
from a Linked List Class and add an attribute for previous.'''
|
||||||
|
|
||||||
|
from linked_list_fifo import LinkedListFIFO
|
||||||
|
|
||||||
|
|
||||||
|
class dNode(object):
|
||||||
|
def __init__(self, value=None, pointer=None, previous=None):
|
||||||
|
self.value = value
|
||||||
|
self.pointer = pointer
|
||||||
|
self.previous = previous
|
||||||
|
|
||||||
|
|
||||||
|
class dLinkList(LinkedListFIFO):
|
||||||
|
|
||||||
|
def printListInverse(self):
|
||||||
|
node = self.tail
|
||||||
|
while node:
|
||||||
|
print(node.value)
|
||||||
|
try:
|
||||||
|
node = node.previous
|
||||||
|
except:
|
||||||
|
break
|
||||||
|
|
||||||
|
def _add(self, value):
|
||||||
|
self.length += 1
|
||||||
|
node = dNode(value)
|
||||||
|
if self.tail:
|
||||||
|
self.tail.pointer = node
|
||||||
|
node.previous = self.tail
|
||||||
|
self.tail = node
|
||||||
|
|
||||||
|
def _delete(self, node):
|
||||||
|
self.length -= 1
|
||||||
|
node.previous.pointer = node.pointer
|
||||||
|
if not node.pointer:
|
||||||
|
self.tail = node.previous
|
||||||
|
|
||||||
|
def _find(self, index):
|
||||||
|
node = self.head
|
||||||
|
i = 0
|
||||||
|
while node and i < index:
|
||||||
|
node = node.pointer
|
||||||
|
i += 1
|
||||||
|
return node, i
|
||||||
|
|
||||||
|
# delete nodes in general
|
||||||
|
def deleteNode(self, index):
|
||||||
|
if not self.head or not self.head.pointer:
|
||||||
|
self._deleteFirst()
|
||||||
|
else:
|
||||||
|
node, i = self._find(index)
|
||||||
|
if i == index:
|
||||||
|
self._delete(node)
|
||||||
|
else:
|
||||||
|
print('Node with index {} not found'.format(index))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
from collections import Counter
|
||||||
|
|
||||||
|
ll = dLinkList()
|
||||||
|
for i in range(1, 5):
|
||||||
|
ll.addNode(i)
|
||||||
|
print('Printing the list...')
|
||||||
|
ll._printList()
|
||||||
|
print('Now, printing the list inversely...')
|
||||||
|
ll.printListInverse()
|
||||||
|
print('The list after adding node with value 15')
|
||||||
|
ll._add(15)
|
||||||
|
ll._printList()
|
||||||
|
print("The list after deleting everything...")
|
||||||
|
for i in range(ll.length-1, -1, -1):
|
||||||
|
ll.deleteNode(i)
|
||||||
|
ll._printList()
|
||||||
|
|
@ -0,0 +1,42 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
''' Find the mth-to-last element of a linked list.
|
||||||
|
One option is having two pointers, separated by m. P1 start at the roots
|
||||||
|
(p1 = self.root) and p2 is m-behinf pointer, which is created when p1 is at m.
|
||||||
|
When p1 reach the end, p2 is the node. '''
|
||||||
|
|
||||||
|
from linked_list_fifo import LinkedListFIFO
|
||||||
|
from node import Node
|
||||||
|
|
||||||
|
|
||||||
|
class LinkedListFIFO_find_kth(LinkedListFIFO):
|
||||||
|
|
||||||
|
|
||||||
|
def find_kth_to_last(self, k):
|
||||||
|
p1, p2 = self.head, self.head
|
||||||
|
i = 0
|
||||||
|
while p1:
|
||||||
|
if i > k:
|
||||||
|
try:
|
||||||
|
p2 = p2.pointer
|
||||||
|
except:
|
||||||
|
break
|
||||||
|
p1 = p1.pointer
|
||||||
|
i += 1
|
||||||
|
return p2.value
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
ll = LinkedListFIFO_find_kth()
|
||||||
|
for i in range(1, 11):
|
||||||
|
ll.addNode(i)
|
||||||
|
print('The Linked List:')
|
||||||
|
print(ll._printList())
|
||||||
|
k = 3
|
||||||
|
k_from_last = ll.find_kth_to_last(k)
|
||||||
|
print("The %dth element to the last of the LL of size %d is %d" %(k, ll.length, k_from_last))
|
||||||
|
|
@ -0,0 +1,90 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
''' A class for a linked list that has the nodes in a FIFO order (such as a queue)'''
|
||||||
|
|
||||||
|
|
||||||
|
from node import Node
|
||||||
|
|
||||||
|
class LinkedListFIFO(object):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.head = None
|
||||||
|
self.length = 0
|
||||||
|
self.tail = None # this is different from ll lifo
|
||||||
|
|
||||||
|
def _printList(self):
|
||||||
|
node = self.head
|
||||||
|
while node:
|
||||||
|
print(node.value)
|
||||||
|
node = node.pointer
|
||||||
|
|
||||||
|
def _addFirst(self, value):
|
||||||
|
self.length = 1
|
||||||
|
node = Node(value)
|
||||||
|
self.head = node
|
||||||
|
self.tail = node
|
||||||
|
|
||||||
|
def _deleteFirst(self):
|
||||||
|
self.length = 0
|
||||||
|
self.head = None
|
||||||
|
self.tail = None
|
||||||
|
print('The list is empty.')
|
||||||
|
|
||||||
|
def _add(self, value):
|
||||||
|
self.length += 1
|
||||||
|
node = Node(value)
|
||||||
|
if self.tail:
|
||||||
|
self.tail.pointer = node
|
||||||
|
self.tail = node
|
||||||
|
|
||||||
|
def addNode(self, value):
|
||||||
|
if not self.head:
|
||||||
|
self._addFirst(value)
|
||||||
|
else:
|
||||||
|
self._add(value)
|
||||||
|
|
||||||
|
def _find(self, index):
|
||||||
|
prev = None
|
||||||
|
node = self.head
|
||||||
|
i = 0
|
||||||
|
while node and i < index:
|
||||||
|
prev = node
|
||||||
|
node = node.pointer
|
||||||
|
i += 1
|
||||||
|
return node, prev, i
|
||||||
|
|
||||||
|
def deleteNode(self, index):
|
||||||
|
if not self.head or not self.head.pointer:
|
||||||
|
self._deleteFirst()
|
||||||
|
else:
|
||||||
|
node, prev, i = self._find(index)
|
||||||
|
if i == index and node:
|
||||||
|
self.length -= 1
|
||||||
|
if i == 0 or not prev :
|
||||||
|
self.head = node.pointer
|
||||||
|
else:
|
||||||
|
prev.pointer = node.pointer
|
||||||
|
if not self.tail == node:
|
||||||
|
self.tail = prev
|
||||||
|
else:
|
||||||
|
print('Node with index {} not found'.format(index))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
ll = LinkedListFIFO()
|
||||||
|
for i in range(1, 5):
|
||||||
|
ll.addNode(i)
|
||||||
|
print('The list is:')
|
||||||
|
ll._printList()
|
||||||
|
print('The list after deleting node with index 2:')
|
||||||
|
ll.deleteNode(2)
|
||||||
|
ll._printList()
|
||||||
|
print('The list after adding node with value 15')
|
||||||
|
ll._add(15)
|
||||||
|
ll._printList()
|
||||||
|
print("The list after deleting everything...")
|
||||||
|
for i in range(ll.length-1, -1, -1):
|
||||||
|
ll.deleteNode(i)
|
||||||
|
ll._printList()
|
@ -0,0 +1,90 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
''' Implement a unordered linked list, i.e. a LIFO linked list (like a stack) '''
|
||||||
|
|
||||||
|
from node import Node
|
||||||
|
|
||||||
|
class LinkedListLIFO(object):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.head = None
|
||||||
|
self.length = 0
|
||||||
|
|
||||||
|
def _printList(self):
|
||||||
|
node = self.head
|
||||||
|
while node:
|
||||||
|
print(node.value)
|
||||||
|
node = node.pointer
|
||||||
|
|
||||||
|
def _delete(self, prev, node):
|
||||||
|
self.length -= 1
|
||||||
|
if not prev:
|
||||||
|
self.head = node.pointer
|
||||||
|
else:
|
||||||
|
prev.pointer = node.pointer
|
||||||
|
|
||||||
|
def _add(self, value):
|
||||||
|
self.length += 1
|
||||||
|
self.head = Node(value, self.head)
|
||||||
|
|
||||||
|
def _find(self, index):
|
||||||
|
prev = None
|
||||||
|
node = self.head
|
||||||
|
i = 0
|
||||||
|
while node and i < index:
|
||||||
|
prev = node
|
||||||
|
node = node.pointer
|
||||||
|
i += 1
|
||||||
|
return node, prev, i
|
||||||
|
|
||||||
|
def _find_by_value(self, value):
|
||||||
|
prev = None
|
||||||
|
node = self.head
|
||||||
|
found = 0
|
||||||
|
while node and not found:
|
||||||
|
if node.value == value:
|
||||||
|
found = True
|
||||||
|
else:
|
||||||
|
prev = node
|
||||||
|
node = node.pointer
|
||||||
|
return node, prev, found
|
||||||
|
|
||||||
|
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))
|
||||||
|
|
||||||
|
def deleteNodeByValue(self, value):
|
||||||
|
node, prev, found = self._find_by_value(value)
|
||||||
|
if found:
|
||||||
|
self._delete(prev, node)
|
||||||
|
else:
|
||||||
|
print('Node with value {} not found'.format(value))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
ll = LinkedListLIFO()
|
||||||
|
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()
|
||||||
|
print('The list after deleting node with value 3:')
|
||||||
|
ll.deleteNodeByValue(2)
|
||||||
|
ll._printList()
|
||||||
|
print('The list after adding node with value 15')
|
||||||
|
ll._add(15)
|
||||||
|
ll._printList()
|
||||||
|
print("The list after deleting everything...")
|
||||||
|
for i in range(ll.length-1, -1, -1):
|
||||||
|
ll.deleteNode(i)
|
||||||
|
ll._printList()
|
@ -0,0 +1,33 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
class Node(object):
|
||||||
|
def __init__(self, value=None, pointer=None):
|
||||||
|
self.value = value
|
||||||
|
self.pointer = pointer
|
||||||
|
|
||||||
|
def getData(self):
|
||||||
|
return self.value
|
||||||
|
|
||||||
|
def getNext(self):
|
||||||
|
return self.pointer
|
||||||
|
|
||||||
|
def setData(self, newdata):
|
||||||
|
self.value = newdata
|
||||||
|
|
||||||
|
def setNext(self, newpointer):
|
||||||
|
self.pointer = newpointer
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
L = Node("a", Node("b", Node("c", Node("d"))))
|
||||||
|
assert(L.pointer.pointer.value=='c')
|
||||||
|
|
||||||
|
print(L.getData())
|
||||||
|
print(L.getNext().getData())
|
||||||
|
L.setData('aa')
|
||||||
|
L.setNext(Node('e'))
|
||||||
|
print(L.getData())
|
||||||
|
print(L.getNext().getData())
|
@ -0,0 +1,57 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
''' This function divides a linked list in a value, where everything smaller than this value
|
||||||
|
goes to the front, and everything large goes to the back:'''
|
||||||
|
|
||||||
|
|
||||||
|
from linked_list_fifo import LinkedListFIFO
|
||||||
|
from node import Node
|
||||||
|
|
||||||
|
|
||||||
|
def partList(ll, n):
|
||||||
|
|
||||||
|
more = LinkedListFIFO()
|
||||||
|
less = LinkedListFIFO()
|
||||||
|
|
||||||
|
node = ll.head
|
||||||
|
|
||||||
|
while node:
|
||||||
|
item = node.value
|
||||||
|
|
||||||
|
if item < n:
|
||||||
|
less.addNode(item)
|
||||||
|
|
||||||
|
elif item > n:
|
||||||
|
more.addNode(item)
|
||||||
|
|
||||||
|
node = node.pointer
|
||||||
|
|
||||||
|
less.addNode(n)
|
||||||
|
nodemore = more.head
|
||||||
|
|
||||||
|
while nodemore:
|
||||||
|
less.addNode(nodemore.value)
|
||||||
|
nodemore = nodemore.pointer
|
||||||
|
|
||||||
|
return less
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
ll = LinkedListFIFO()
|
||||||
|
l = [6, 7, 3, 4, 9, 5, 1, 2, 8]
|
||||||
|
for i in l:
|
||||||
|
ll.addNode(i)
|
||||||
|
|
||||||
|
print('Before Part')
|
||||||
|
ll._printList()
|
||||||
|
|
||||||
|
print('After Part')
|
||||||
|
newll = partList(ll, 6)
|
||||||
|
newll._printList()
|
||||||
|
|
||||||
|
|
@ -0,0 +1,85 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
''' Supposing two linked lists representing numbers, such that in each of their
|
||||||
|
nodes they carry one digit. This function sums the two numbers that these
|
||||||
|
two linked lists represent, returning a third list representing the sum:'''
|
||||||
|
|
||||||
|
|
||||||
|
from linked_list_fifo import LinkedListFIFO
|
||||||
|
from node import Node
|
||||||
|
|
||||||
|
|
||||||
|
class LinkedListFIFOYield(LinkedListFIFO):
|
||||||
|
|
||||||
|
# print each node's value, starting from the head
|
||||||
|
def _printList(self):
|
||||||
|
node = self.head
|
||||||
|
while node:
|
||||||
|
yield(node.value)
|
||||||
|
node = node.pointer
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def sumlls(l1, l2):
|
||||||
|
|
||||||
|
lsum = LinkedListFIFOYield()
|
||||||
|
dig1 = l1.head
|
||||||
|
dig2 = l2.head
|
||||||
|
pointer = 0
|
||||||
|
|
||||||
|
while dig1 and dig2:
|
||||||
|
d1 = dig1.value
|
||||||
|
d2 = dig2.value
|
||||||
|
sum_d = d1 + d2 + pointer
|
||||||
|
if sum_d > 9:
|
||||||
|
pointer = sum_d//10
|
||||||
|
lsum.addNode(sum_d%10)
|
||||||
|
|
||||||
|
else:
|
||||||
|
lsum.addNode(sum_d)
|
||||||
|
pointer = 0
|
||||||
|
|
||||||
|
dig1 = dig1.pointer
|
||||||
|
dig2 = dig2.pointer
|
||||||
|
|
||||||
|
if dig1:
|
||||||
|
sum_d = pointer + dig1.value
|
||||||
|
if sum_d > 9:
|
||||||
|
lsum.addNode(sum_d%10)
|
||||||
|
else:
|
||||||
|
lsum.addNode(sum_d)
|
||||||
|
dig1 = dig1.pointer
|
||||||
|
|
||||||
|
if dig2:
|
||||||
|
sum_d = pointer + dig2.value
|
||||||
|
if sum_d > 9:
|
||||||
|
lsum.addNode(sum_d%10)
|
||||||
|
else:
|
||||||
|
lsum.addNode(sum_d)
|
||||||
|
dig2 = dig2.pointer
|
||||||
|
|
||||||
|
return lsum
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
l1 = LinkedListFIFOYield() # 2671
|
||||||
|
l1.addNode(1)
|
||||||
|
l1.addNode(7)
|
||||||
|
l1.addNode(6)
|
||||||
|
l1.addNode(2)
|
||||||
|
|
||||||
|
l2 = LinkedListFIFOYield() # 455
|
||||||
|
l2.addNode(5)
|
||||||
|
l2.addNode(5)
|
||||||
|
l2.addNode(4)
|
||||||
|
|
||||||
|
lsum = sumlls(l1, l2)
|
||||||
|
l = list(lsum._printList())
|
||||||
|
for i in reversed(l):
|
||||||
|
print i
|
@ -0,0 +1,109 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
""" A class for an animal shelter with two queues"""
|
||||||
|
|
||||||
|
|
||||||
|
class Node(object):
|
||||||
|
def __init__(self, animalName=None, animalKind=None, pointer=None):
|
||||||
|
self.animalName = animalName
|
||||||
|
self.animalKind = animalKind
|
||||||
|
self.pointer = pointer
|
||||||
|
self.timestamp = 0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class AnimalShelter(object):
|
||||||
|
def __init__(self):
|
||||||
|
self.headCat = None
|
||||||
|
self.headDog = None
|
||||||
|
self.tailCat = None
|
||||||
|
self.tailDog = None
|
||||||
|
self.animalNumber = 0
|
||||||
|
|
||||||
|
|
||||||
|
# Queue any animal
|
||||||
|
|
||||||
|
def enqueue(self, animalName, animalKind):
|
||||||
|
self.animalNumber += 1
|
||||||
|
newAnimal = Node(animalName, animalKind)
|
||||||
|
newAnimal.timestamp = self.animalNumber
|
||||||
|
|
||||||
|
if animalKind == 'cat':
|
||||||
|
if not self.headCat:
|
||||||
|
self.headCat = newAnimal
|
||||||
|
if self.tailCat:
|
||||||
|
self.tailCat.pointer = newAnimal
|
||||||
|
self.tailCat = newAnimal
|
||||||
|
|
||||||
|
elif animalKind == 'dog':
|
||||||
|
if not self.headDog:
|
||||||
|
self.headDog = newAnimal
|
||||||
|
if self.tailDog:
|
||||||
|
self.tailDog.pointer = newAnimal
|
||||||
|
self.tailDog = newAnimal
|
||||||
|
|
||||||
|
|
||||||
|
# Dequeue methods
|
||||||
|
|
||||||
|
def dequeueDog(self):
|
||||||
|
if self.headDog:
|
||||||
|
newAnimal = self.headDog
|
||||||
|
self.headDog = newAnimal.pointer
|
||||||
|
return str(newAnimal.animalName)
|
||||||
|
else:
|
||||||
|
return 'No Dogs!'
|
||||||
|
|
||||||
|
|
||||||
|
def dequeueCat(self):
|
||||||
|
if self.headCat:
|
||||||
|
newAnimal = self.headCat
|
||||||
|
self.headCat = newAnimal.pointer
|
||||||
|
return str(newAnimal.animalName)
|
||||||
|
else:
|
||||||
|
return 'No Cats!'
|
||||||
|
|
||||||
|
|
||||||
|
def dequeueAny(self):
|
||||||
|
if self.headCat and not self.headDog:
|
||||||
|
return self.dequeueCat()
|
||||||
|
elif self.headDog and not self.headCat:
|
||||||
|
return self.dequeueDog()
|
||||||
|
elif self.headDog and self.headCat:
|
||||||
|
if self.headDog.timestamp < self.headCat.timestamp:
|
||||||
|
return self.dequeueDog()
|
||||||
|
else:
|
||||||
|
return self.dequeueCat()
|
||||||
|
else:
|
||||||
|
return ('No Animals!')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def _print(self):
|
||||||
|
print("Cats:")
|
||||||
|
cats = self.headCat
|
||||||
|
while cats:
|
||||||
|
print(cats.animalName, cats.animalKind)
|
||||||
|
cats = cats.pointer
|
||||||
|
print("Dogs:")
|
||||||
|
dogs = self.headDog
|
||||||
|
while dogs:
|
||||||
|
print(dogs.animalName, dogs.animalKind)
|
||||||
|
dogs = dogs.pointer
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
qs = AnimalShelter()
|
||||||
|
qs.enqueue('bob', 'cat')
|
||||||
|
qs.enqueue('mia', 'cat')
|
||||||
|
qs.enqueue('yoda', 'dog')
|
||||||
|
qs.enqueue('wolf', 'dog')
|
||||||
|
qs._print()
|
||||||
|
|
||||||
|
print("Deque one dog and one cat...")
|
||||||
|
qs.dequeueDog()
|
||||||
|
qs.dequeueCat()
|
||||||
|
qs._print()
|
39
first_edition/ebook_src/abstract_structures/queues/deque.py
Normal file
39
first_edition/ebook_src/abstract_structures/queues/deque.py
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
''' a class for a double ended queue (also inefficient) '''
|
||||||
|
|
||||||
|
from queue import Queue
|
||||||
|
|
||||||
|
class Deque(Queue):
|
||||||
|
|
||||||
|
def enqueue_back(self, item):
|
||||||
|
self.items.append(item)
|
||||||
|
|
||||||
|
def dequeue_front(self):
|
||||||
|
return self.items.pop(0)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
queue = Deque()
|
||||||
|
print("Is the queue empty? ", queue.isEmpty())
|
||||||
|
print("Adding 0 to 10 in the queue...")
|
||||||
|
for i in range(10):
|
||||||
|
queue.enqueue(i)
|
||||||
|
print("Queue size: ", queue.size())
|
||||||
|
print("Queue peek : ", queue.peek())
|
||||||
|
print("Dequeue...", queue.dequeue())
|
||||||
|
print("Queue peek: ", queue.peek())
|
||||||
|
print("Is the queue empty? ", queue.isEmpty())
|
||||||
|
print(queue)
|
||||||
|
|
||||||
|
print("\nNow using the dequeue methods...")
|
||||||
|
print("Dequeue from front...", queue.dequeue_front())
|
||||||
|
print("Queue peek: ", queue.peek())
|
||||||
|
print(queue)
|
||||||
|
print("Queue from back...")
|
||||||
|
queue.enqueue_back(50)
|
||||||
|
print("Queue peek: ", queue.peek())
|
||||||
|
print(queue)
|
@ -0,0 +1,80 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
''' Queue acts as a container for nodes (objects) that are inserted and removed according FIFO'''
|
||||||
|
|
||||||
|
|
||||||
|
class Node(object):
|
||||||
|
def __init__(self, value=None, pointer=None):
|
||||||
|
self.value = value
|
||||||
|
self.pointer = None
|
||||||
|
|
||||||
|
|
||||||
|
class LinkedQueue(object):
|
||||||
|
def __init__(self):
|
||||||
|
self.head = None
|
||||||
|
self.tail = None
|
||||||
|
|
||||||
|
|
||||||
|
def isEmpty(self):
|
||||||
|
return not bool(self.head)
|
||||||
|
|
||||||
|
|
||||||
|
def dequeue(self):
|
||||||
|
if self.head:
|
||||||
|
value = self.head.value
|
||||||
|
self.head = self.head.pointer
|
||||||
|
return value
|
||||||
|
else:
|
||||||
|
print('Queue is empty, cannot dequeue.')
|
||||||
|
|
||||||
|
|
||||||
|
def enqueue(self, value):
|
||||||
|
node = Node(value)
|
||||||
|
if not self.head:
|
||||||
|
self.head = node
|
||||||
|
self.tail = node
|
||||||
|
else:
|
||||||
|
if self.tail:
|
||||||
|
self.tail.pointer = node
|
||||||
|
self.tail = node
|
||||||
|
|
||||||
|
|
||||||
|
def size(self):
|
||||||
|
node = self.head
|
||||||
|
num_nodes = 0
|
||||||
|
while node:
|
||||||
|
num_nodes += 1
|
||||||
|
node = node.pointer
|
||||||
|
return num_nodes
|
||||||
|
|
||||||
|
|
||||||
|
def peek(self):
|
||||||
|
return self.head.value
|
||||||
|
|
||||||
|
|
||||||
|
def _print(self):
|
||||||
|
node = self.head
|
||||||
|
while node:
|
||||||
|
print(node.value)
|
||||||
|
node = node.pointer
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
queue = LinkedQueue()
|
||||||
|
print("Is the queue empty? ", queue.isEmpty())
|
||||||
|
print("Adding 0 to 10 in the queue...")
|
||||||
|
for i in range(10):
|
||||||
|
queue.enqueue(i)
|
||||||
|
print("Is the queue empty? ", queue.isEmpty())
|
||||||
|
queue._print()
|
||||||
|
|
||||||
|
print("Queue size: ", queue.size())
|
||||||
|
print("Queue peek : ", queue.peek())
|
||||||
|
print("Dequeue...", queue.dequeue())
|
||||||
|
print("Queue peek: ", queue.peek())
|
||||||
|
queue._print()
|
||||||
|
|
@ -0,0 +1,47 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
""" Using our deque class and Python's deque class """
|
||||||
|
|
||||||
|
|
||||||
|
import string
|
||||||
|
import collections
|
||||||
|
|
||||||
|
from deque import Deque
|
||||||
|
|
||||||
|
|
||||||
|
STRIP = string.whitespace + string.punctuation + "\"'"
|
||||||
|
|
||||||
|
def palindrome_checker_with_deque(str1):
|
||||||
|
|
||||||
|
d1 = Deque()
|
||||||
|
d2 = collections.deque()
|
||||||
|
|
||||||
|
for s in str1.lower():
|
||||||
|
if s not in STRIP:
|
||||||
|
d2.append(s)
|
||||||
|
d1.enqueue(s)
|
||||||
|
|
||||||
|
|
||||||
|
eq1 = True
|
||||||
|
while d1.size() > 1 and eq1:
|
||||||
|
if d1.dequeue_front() != d1.dequeue():
|
||||||
|
eq1 = False
|
||||||
|
|
||||||
|
eq2 = True
|
||||||
|
while len(d2) > 1 and eq2:
|
||||||
|
if d2.pop() != d2.popleft():
|
||||||
|
eq2 = False
|
||||||
|
|
||||||
|
return eq1, eq2
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
str1 = 'Madam Im Adam'
|
||||||
|
str2 = 'Buffy is a Slayer'
|
||||||
|
print(palindrome_checker_with_deque(str1))
|
||||||
|
print(palindrome_checker_with_deque(str2))
|
63
first_edition/ebook_src/abstract_structures/queues/queue.py
Normal file
63
first_edition/ebook_src/abstract_structures/queues/queue.py
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
class Queue(object):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.in_stack = []
|
||||||
|
self.out_stack = []
|
||||||
|
|
||||||
|
def _transfer(self):
|
||||||
|
while self.in_stack:
|
||||||
|
self.out_stack.append(self.in_stack.pop())
|
||||||
|
|
||||||
|
def enqueue(self, item):
|
||||||
|
return self.in_stack.append(item)
|
||||||
|
|
||||||
|
def dequeue(self):
|
||||||
|
if not self.out_stack:
|
||||||
|
self._transfer()
|
||||||
|
if self.out_stack:
|
||||||
|
return self.out_stack.pop()
|
||||||
|
else:
|
||||||
|
return "Queue empty!"
|
||||||
|
|
||||||
|
def size(self):
|
||||||
|
return len(self.in_stack) + len(self.out_stack)
|
||||||
|
|
||||||
|
def peek(self):
|
||||||
|
if not self.out_stack:
|
||||||
|
self._transfer()
|
||||||
|
if self.out_stack:
|
||||||
|
return self.out_stack[-1]
|
||||||
|
else:
|
||||||
|
return "Queue empty!"
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
if not self.out_stack:
|
||||||
|
self._transfer()
|
||||||
|
if self.out_stack:
|
||||||
|
return '{}'.format(self.out_stack)
|
||||||
|
else:
|
||||||
|
return "Queue empty!"
|
||||||
|
|
||||||
|
def isEmpty(self):
|
||||||
|
return not (bool(self.in_stack) or bool(self.out_stack))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
queue = Queue()
|
||||||
|
print("Is the queue empty? ", queue.isEmpty())
|
||||||
|
print("Adding 0 to 10 in the queue...")
|
||||||
|
for i in range(10):
|
||||||
|
queue.enqueue(i)
|
||||||
|
print("Queue size: ", queue.size())
|
||||||
|
print("Queue peek : ", queue.peek())
|
||||||
|
print("Dequeue...", queue.dequeue())
|
||||||
|
print("Queue peek: ", queue.peek())
|
||||||
|
print("Is the queue empty? ", queue.isEmpty())
|
||||||
|
|
||||||
|
print("Printing the queue...")
|
||||||
|
print(queue)
|
@ -0,0 +1,30 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
'''transform a decimal number to a binary number with a stack '''
|
||||||
|
|
||||||
|
|
||||||
|
from stack import Stack
|
||||||
|
|
||||||
|
def dec2bin_with_stack(decnum):
|
||||||
|
|
||||||
|
s = Stack()
|
||||||
|
str_aux = ''
|
||||||
|
|
||||||
|
while decnum > 0:
|
||||||
|
dig = decnum % 2
|
||||||
|
decnum = decnum//2
|
||||||
|
s.push(dig)
|
||||||
|
|
||||||
|
while not s.isEmpty():
|
||||||
|
str_aux += str(s.pop())
|
||||||
|
|
||||||
|
return str_aux
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
decnum = 9
|
||||||
|
assert(dec2bin_with_stack(decnum) == '1001')
|
@ -0,0 +1,71 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
""" A stack made of linked list"""
|
||||||
|
|
||||||
|
|
||||||
|
class Node(object):
|
||||||
|
def __init__(self, value=None, pointer=None):
|
||||||
|
self.value = value
|
||||||
|
self.pointer = pointer
|
||||||
|
|
||||||
|
|
||||||
|
class Stack(object):
|
||||||
|
def __init__(self):
|
||||||
|
self.head = None
|
||||||
|
|
||||||
|
def isEmpty(self):
|
||||||
|
return not bool(self.head)
|
||||||
|
|
||||||
|
def push(self, item):
|
||||||
|
self.head = Node(item, self.head)
|
||||||
|
|
||||||
|
|
||||||
|
def pop(self):
|
||||||
|
if self.head:
|
||||||
|
node = self.head
|
||||||
|
self.head = node.pointer
|
||||||
|
return node.value
|
||||||
|
else:
|
||||||
|
print('Stack is empty.')
|
||||||
|
|
||||||
|
|
||||||
|
def peek(self):
|
||||||
|
if self.head:
|
||||||
|
return self.head.value
|
||||||
|
else:
|
||||||
|
print('Stack is empty.')
|
||||||
|
|
||||||
|
|
||||||
|
def size(self):
|
||||||
|
node = self.head
|
||||||
|
count = 0
|
||||||
|
while node:
|
||||||
|
count +=1
|
||||||
|
node = node.pointer
|
||||||
|
return count
|
||||||
|
|
||||||
|
|
||||||
|
def _printList(self):
|
||||||
|
node = self.head
|
||||||
|
while node:
|
||||||
|
print(node.value)
|
||||||
|
node = node.pointer
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
stack = Stack()
|
||||||
|
print("Is the stack empty? ", stack.isEmpty())
|
||||||
|
print("Adding 0 to 10 in the stack...")
|
||||||
|
for i in range(10):
|
||||||
|
stack.push(i)
|
||||||
|
stack._printList()
|
||||||
|
print("Stack size: ", stack.size())
|
||||||
|
print("Stack peek : ", stack.peek())
|
||||||
|
print("Pop...", stack.pop())
|
||||||
|
print("Stack peek: ", stack.peek())
|
||||||
|
print("Is the stack empty? ", stack.isEmpty())
|
||||||
|
stack._printList()
|
@ -0,0 +1,31 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
''' Uses a stack to reverse a string '''
|
||||||
|
|
||||||
|
from stack import Stack
|
||||||
|
|
||||||
|
def reverse_string_with_stack(str1):
|
||||||
|
|
||||||
|
s = Stack()
|
||||||
|
revStr = ''
|
||||||
|
|
||||||
|
for c in str1:
|
||||||
|
s.push(c)
|
||||||
|
|
||||||
|
while not s.isEmpty():
|
||||||
|
revStr += s.pop()
|
||||||
|
|
||||||
|
return revStr
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
str1 = 'Buffy is a Slayer!'
|
||||||
|
print(str1)
|
||||||
|
print(reverse_string_with_stack(str1))
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,56 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
""" define a class for a set of stacks """
|
||||||
|
|
||||||
|
from stack import Stack
|
||||||
|
|
||||||
|
class SetOfStacks(Stack):
|
||||||
|
def __init__(self, capacity=4):
|
||||||
|
self.setofstacks = []
|
||||||
|
self.items = []
|
||||||
|
self.capacity = capacity
|
||||||
|
|
||||||
|
|
||||||
|
def push(self, value):
|
||||||
|
if self.size() >= self.capacity:
|
||||||
|
self.setofstacks.append(self.items)
|
||||||
|
self.items = []
|
||||||
|
self.items.append(value)
|
||||||
|
|
||||||
|
|
||||||
|
def pop(self):
|
||||||
|
value = self.items.pop()
|
||||||
|
if self.isEmpty() and self.setofstacks:
|
||||||
|
self.items = self.setofstacks.pop()
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def sizeStack(self):
|
||||||
|
return len(self.setofstacks)*self.capacity + self.size()
|
||||||
|
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
aux = []
|
||||||
|
for s in self.setofstacks:
|
||||||
|
aux.extend(s)
|
||||||
|
aux.extend(self.items)
|
||||||
|
return '{}'.format(aux)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
capacity = 5
|
||||||
|
stack = SetOfStacks(capacity)
|
||||||
|
print("Is the stack empty? ", stack.isEmpty())
|
||||||
|
print("Adding 0 to 10 in the stack...")
|
||||||
|
for i in range(10):
|
||||||
|
stack.push(i)
|
||||||
|
print(stack)
|
||||||
|
print("Stack size: ", stack.sizeStack())
|
||||||
|
print("Stack peek : ", stack.peek())
|
||||||
|
print("Pop...", stack.pop())
|
||||||
|
print("Stack peek: ", stack.peek())
|
||||||
|
print("Is the stack empty? ", stack.isEmpty())
|
||||||
|
print(stack)
|
65
first_edition/ebook_src/abstract_structures/stacks/stack.py
Executable file
65
first_edition/ebook_src/abstract_structures/stacks/stack.py
Executable file
@ -0,0 +1,65 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
# copy of the class ../Stack.py
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
class Stack(object):
|
||||||
|
def __init__(self):
|
||||||
|
self.content = []
|
||||||
|
self.min_array = []
|
||||||
|
self.min = float('inf')
|
||||||
|
|
||||||
|
def push(self, value):
|
||||||
|
if value < self.min:
|
||||||
|
self.min = value
|
||||||
|
|
||||||
|
self.content.append(value)
|
||||||
|
self.min_array.append(self.min)
|
||||||
|
|
||||||
|
def pop(self):
|
||||||
|
if self.content:
|
||||||
|
value = self.content.pop()
|
||||||
|
self.min_array.pop()
|
||||||
|
if self.min_array:
|
||||||
|
self.min = self.min_array[-1]
|
||||||
|
return value
|
||||||
|
else:
|
||||||
|
return 'Empty List. '
|
||||||
|
|
||||||
|
def find_min(self):
|
||||||
|
if self.min_array:
|
||||||
|
return self.min_array[-1]
|
||||||
|
else:
|
||||||
|
return 'No min value for 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.')
|
||||||
|
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return '{}'.format(self.content)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
q = Stack()
|
||||||
|
|
||||||
|
for i in range(15,20):
|
||||||
|
q.push(i)
|
||||||
|
for i in range(10,5,-1):
|
||||||
|
q.push(i)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
for i in range(1, 13):
|
||||||
|
print q.pop(), q.find_min()
|
@ -0,0 +1,77 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
''' A stack with a minimum lookup '''
|
||||||
|
|
||||||
|
from stack import Stack
|
||||||
|
|
||||||
|
|
||||||
|
class NodeWithMin(object):
|
||||||
|
def __init__(self, value=None, minimum=None):
|
||||||
|
self.value = value
|
||||||
|
self.minimum = minimum
|
||||||
|
|
||||||
|
|
||||||
|
class StackMin(Stack):
|
||||||
|
def __init__(self):
|
||||||
|
self.items = []
|
||||||
|
self.minimum = None
|
||||||
|
|
||||||
|
|
||||||
|
def push(self, value):
|
||||||
|
if self.isEmpty() or self.minimum > value:
|
||||||
|
self.minimum = value
|
||||||
|
self.items.append(NodeWithMin(value, self.minimum))
|
||||||
|
|
||||||
|
|
||||||
|
def peek(self):
|
||||||
|
return self.items[-1].value
|
||||||
|
|
||||||
|
|
||||||
|
def peekMinimum(self):
|
||||||
|
return self.items[-1].minimum
|
||||||
|
|
||||||
|
|
||||||
|
def pop(self):
|
||||||
|
item = self.items.pop()
|
||||||
|
if item:
|
||||||
|
if item.value == self.minimum:
|
||||||
|
self.minimum = self.peekMinimum()
|
||||||
|
return item.value
|
||||||
|
else:
|
||||||
|
print("Stack is empty.")
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
aux = []
|
||||||
|
for i in self.items:
|
||||||
|
aux.append(i.value)
|
||||||
|
return '{}'.format(aux)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
stack = StackMin()
|
||||||
|
print("Is the stack empty? ", stack.isEmpty())
|
||||||
|
print("Adding 0 to 10 in the stack...")
|
||||||
|
for i in range(10,3, -1):
|
||||||
|
stack.push(i)
|
||||||
|
print(stack)
|
||||||
|
|
||||||
|
print("Stack size: ", stack.size())
|
||||||
|
print("Stack peek and peekMinimum : ", stack.peek(), stack.peekMinimum())
|
||||||
|
print("Pop...", stack.pop())
|
||||||
|
print("Stack peek and peekMinimum : ", stack.peek(), stack.peekMinimum())
|
||||||
|
print("Is the stack empty? ", stack.isEmpty())
|
||||||
|
print(stack)
|
||||||
|
|
||||||
|
for i in range(5, 1, -1):
|
||||||
|
stack.push(i)
|
||||||
|
print(stack)
|
||||||
|
|
||||||
|
print("Stack size: ", stack.size())
|
||||||
|
print("Stack peek and peekMinimum : ", stack.peek(), stack.peekMinimum())
|
||||||
|
print("Pop...", stack.pop())
|
||||||
|
print("Stack peek and peekMinimum : ", stack.peek(), stack.peekMinimum())
|
||||||
|
print("Is the stack empty? ", stack.isEmpty())
|
||||||
|
print(stack)
|
@ -0,0 +1,41 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
""" Implement the 'towers of hanoi'"""
|
||||||
|
|
||||||
|
from linked_stack import Stack, Node
|
||||||
|
|
||||||
|
|
||||||
|
def moveTop(s1, s3):
|
||||||
|
|
||||||
|
s3.append(s1.pop())
|
||||||
|
|
||||||
|
|
||||||
|
def moveDisks(n, s1, s3, s2):
|
||||||
|
|
||||||
|
if n < 1: return
|
||||||
|
moveDisks(n - 1, s1, s2, s3)
|
||||||
|
moveTop(s1, s3)
|
||||||
|
moveDisks(n -1, s2, s3, s1)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def towersOfHanoi(n):
|
||||||
|
s1 = [x+1 for x in range(n)]
|
||||||
|
s2 = []
|
||||||
|
s3 = []
|
||||||
|
print('The first stick is {0} and the third stick has {1}'.format(s1, s3))
|
||||||
|
|
||||||
|
moveDisks(n, s1, s3, s2)
|
||||||
|
|
||||||
|
print('The first stick is {0} and the third stick has {1}'.format(s1, s3))
|
||||||
|
|
||||||
|
return s3
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
towersOfHanoi(6)
|
28
first_edition/ebook_src/bitwise_operations/bit_array.py
Executable file
28
first_edition/ebook_src/bitwise_operations/bit_array.py
Executable file
@ -0,0 +1,28 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
''' Example of how to use a bit array in python as a "counter" dict'''
|
||||||
|
|
||||||
|
def print_dupl_ba(l1):
|
||||||
|
'''
|
||||||
|
>>> l1 = [0, 1, 2, 3, 4, 2, 6, 7, 8, 9]
|
||||||
|
>>> print_dupl_ba(l1)
|
||||||
|
2
|
||||||
|
'''
|
||||||
|
|
||||||
|
bs = bytearray(10)
|
||||||
|
for i in range(len(l1)):
|
||||||
|
if i == l1[i]:
|
||||||
|
bs[i] = 1
|
||||||
|
for index, bit in enumerate(bs):
|
||||||
|
if bit == 0:
|
||||||
|
return l1[index]
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|
33
first_edition/ebook_src/bitwise_operations/bitwise.txt
Executable file
33
first_edition/ebook_src/bitwise_operations/bitwise.txt
Executable file
@ -0,0 +1,33 @@
|
|||||||
|
BIT-WISE
|
||||||
|
----------------------
|
||||||
|
|
||||||
|
1. To find a number:
|
||||||
|
11000101 is 2^0+2^2+2^6+2^7 = 197
|
||||||
|
|
||||||
|
|
||||||
|
2. Left shifting:
|
||||||
|
0010 1011 << 4 ---> 1011 000
|
||||||
|
|
||||||
|
|
||||||
|
3. Right shifting:
|
||||||
|
0010 1011 >> 4 ---> 0000 0010
|
||||||
|
or it can be filled with the copy of the first bit, instead of 0:
|
||||||
|
1011 0010 >> 4 ---> 1111 1011
|
||||||
|
|
||||||
|
|
||||||
|
4. XOR can cancels out:
|
||||||
|
15 ^ 12 ^ 15 = 12
|
||||||
|
|
||||||
|
|
||||||
|
5. 2^x:
|
||||||
|
left-shift 1 by x:
|
||||||
|
0000 0001 << x
|
||||||
|
|
||||||
|
so if x = 2, 2^2 = 4 -> 100
|
||||||
|
|
||||||
|
0000 0001 << 2 ---> 0000 0100
|
||||||
|
|
||||||
|
|
||||||
|
6. Is power of 2?
|
||||||
|
just do x&(x-1).
|
||||||
|
if 0 --> yes!
|
37
first_edition/ebook_src/bitwise_operations/clear_bits.py
Executable file
37
first_edition/ebook_src/bitwise_operations/clear_bits.py
Executable file
@ -0,0 +1,37 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
''' Clear a bit in a binary number.
|
||||||
|
Like the reverse of set bit:
|
||||||
|
1) first create a number filled of 1s,
|
||||||
|
with 0 at i (can create 0001000 and ~)
|
||||||
|
2) AND the number so it clears the ith bit
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def clear_bit(num, i):
|
||||||
|
mask = ~ (1 << i) # -0b10001
|
||||||
|
return bin(num & mask)
|
||||||
|
|
||||||
|
|
||||||
|
def clear_all_bits_from_i_to_0(num, i):
|
||||||
|
mask = ~ ( (1 << (i+1)) - 1)
|
||||||
|
return bin(num & mask)
|
||||||
|
|
||||||
|
|
||||||
|
def clear_all_bits_from_most_sig_to_1(num, i):
|
||||||
|
mask = ( 1 << i) -1
|
||||||
|
return bin(num & mask)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
num = int('10010000', 2)
|
||||||
|
print clear_bit(num, 4) # '0b10000000'
|
||||||
|
|
||||||
|
num = int('10010011', 2)
|
||||||
|
print clear_all_bits_from_i_to_0(num, 2) # '0b10010000'
|
||||||
|
|
||||||
|
num = int('1110011', 2)
|
||||||
|
print clear_all_bits_from_most_sig_to_1(num, 2) #'0b11'
|
25
first_edition/ebook_src/bitwise_operations/find_bit_len.py
Executable file
25
first_edition/ebook_src/bitwise_operations/find_bit_len.py
Executable file
@ -0,0 +1,25 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
''' Find how many bits a int has:
|
||||||
|
1) Start with a mask of 1
|
||||||
|
2) Mask with AND
|
||||||
|
3) if result (if true): count += 1
|
||||||
|
(obs: to find the int of a bin do int('1001', 2)) and to show in bin
|
||||||
|
do bin(int))
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
def find_bit_len(int_num):
|
||||||
|
lenght = 0
|
||||||
|
while int_num:
|
||||||
|
int_num >>= 1
|
||||||
|
lenght += 1
|
||||||
|
return lenght
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
for i in range(17):
|
||||||
|
print(find_bit_len(i))
|
||||||
|
print i.bit_length()
|
32
first_edition/ebook_src/bitwise_operations/find_how_many_1_binary.py
Executable file
32
first_edition/ebook_src/bitwise_operations/find_how_many_1_binary.py
Executable file
@ -0,0 +1,32 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
''' Find how many 1s in the binary:
|
||||||
|
1) Start with a mask of 1
|
||||||
|
2) Mask with AND
|
||||||
|
3) if result (if true): count += 1
|
||||||
|
(obs: to find the int of a bin do int('1001',
|
||||||
|
2)) and to show in bin do bin(int))
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
def find_how_many_1_in_a_binary(n):
|
||||||
|
'''
|
||||||
|
>>> find_how_many_1_in_a_binary(9)
|
||||||
|
2
|
||||||
|
'''
|
||||||
|
|
||||||
|
counter = 0
|
||||||
|
while n:
|
||||||
|
if n & 1:
|
||||||
|
counter += 1
|
||||||
|
n >>= 1
|
||||||
|
return counter
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|
27
first_edition/ebook_src/bitwise_operations/get_bit.py
Executable file
27
first_edition/ebook_src/bitwise_operations/get_bit.py
Executable file
@ -0,0 +1,27 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
''' Get a bit in a binary number:
|
||||||
|
1) Shifts 1 over by i bits
|
||||||
|
2) make an AND with the number
|
||||||
|
3) all the other than the bit at i are clean, now compare to 0
|
||||||
|
4) if the new value is not 0, bit i is 1
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
def get_bit(num, i):
|
||||||
|
mask = 1 << i
|
||||||
|
return num & mask != 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
num = int('0100100', 2)
|
||||||
|
get_bit(num, 0) # 0
|
||||||
|
get_bit(num, 1) # 0
|
||||||
|
get_bit(num, 2) # 1
|
||||||
|
get_bit(num, 3) # 0
|
||||||
|
get_bit(num, 4) # 0
|
||||||
|
get_bit(num, 5) # 1
|
||||||
|
get_bit(num, 6) # 0
|
||||||
|
|
37
first_edition/ebook_src/bitwise_operations/get_float_rep_bin.py
Executable file
37
first_edition/ebook_src/bitwise_operations/get_float_rep_bin.py
Executable file
@ -0,0 +1,37 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
''' Given a real number between 0 and 1 (eg: 0.72), this method print the binary
|
||||||
|
representation. If the Number cannot be represented accurately in binary, with at
|
||||||
|
most 32 chars, print error:
|
||||||
|
'''
|
||||||
|
|
||||||
|
def get_float_rep(num):
|
||||||
|
'''
|
||||||
|
>>> get_float_rep(0.72)
|
||||||
|
('Error 2', '.1011100001010001111010111000010')
|
||||||
|
>>> get_float_rep(0.1)
|
||||||
|
('Error 2', '.0001100110011001100110011001100')
|
||||||
|
>>> get_float_rep(0.5)
|
||||||
|
'.1'
|
||||||
|
'''
|
||||||
|
|
||||||
|
if num >= 1 or num <= 0: return 'Error 1'
|
||||||
|
result = '.'
|
||||||
|
while num:
|
||||||
|
if len(result) >= 32: return 'Error 2', result
|
||||||
|
r = num*2
|
||||||
|
if r >= 1:
|
||||||
|
result += '1'
|
||||||
|
num = r - 1
|
||||||
|
else:
|
||||||
|
result += '0'
|
||||||
|
num = r
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|
36
first_edition/ebook_src/bitwise_operations/insert_small_bin_into_big_bin.py
Executable file
36
first_edition/ebook_src/bitwise_operations/insert_small_bin_into_big_bin.py
Executable file
@ -0,0 +1,36 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
''' Given two 32-bit numbers, N and M, and two bit positions, i and j, this
|
||||||
|
method insert M into N such that M starts at bit j and ends at bit i:
|
||||||
|
1) clear the bits j thru i in N'
|
||||||
|
2) shift M so that it lines up with bits j thru i
|
||||||
|
3) merge M and N
|
||||||
|
'''
|
||||||
|
|
||||||
|
def insert_small_bin_into_big_bin(M, N, i, j):
|
||||||
|
'''
|
||||||
|
>>> N = 0b10000000000
|
||||||
|
>>> M = 0b10011
|
||||||
|
>>> j = 6
|
||||||
|
>>> i = 2
|
||||||
|
>>> insert_small_bin_into_big_bin(M, N, i, j)
|
||||||
|
'0b10001001100'
|
||||||
|
'''
|
||||||
|
|
||||||
|
allOnes = ~0
|
||||||
|
left = allOnes << (j+1) # 1110000
|
||||||
|
right = ( (1 << i) - 1) # 0000111
|
||||||
|
mask = left | right # 1110111
|
||||||
|
N_cleared = N & mask
|
||||||
|
M_shifted = M << i
|
||||||
|
|
||||||
|
return bin( N_cleared | M_shifted)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|
59
first_edition/ebook_src/bitwise_operations/next_with_same_num_1s.py
Executable file
59
first_edition/ebook_src/bitwise_operations/next_with_same_num_1s.py
Executable file
@ -0,0 +1,59 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
''' Give a positive int, print the next smallest and next largest ints with
|
||||||
|
same number of 1 bits.
|
||||||
|
The brute force is:
|
||||||
|
1) find number of 1 bits
|
||||||
|
2) loop above and down until find same, checking for each
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def print_prev_same_1s(num):
|
||||||
|
n1s = find_num_1s(num)
|
||||||
|
# find prev
|
||||||
|
i = num-1
|
||||||
|
while True:
|
||||||
|
n1s_here = find_num_1s(i)
|
||||||
|
if n1s_here == n1s:
|
||||||
|
return bin(i)
|
||||||
|
i -= 1
|
||||||
|
if i < 0:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def print_next_same_1s(num):
|
||||||
|
n1s = find_num_1s(num)
|
||||||
|
# find next
|
||||||
|
i = num+1
|
||||||
|
while True:
|
||||||
|
n1s_here = find_num_1s(i)
|
||||||
|
if n1s_here == n1s:
|
||||||
|
return bin(i)
|
||||||
|
i += 1
|
||||||
|
if i < 0:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def find_num_1s(num):
|
||||||
|
counter = 0
|
||||||
|
while num:
|
||||||
|
if num & 1:
|
||||||
|
counter += 1
|
||||||
|
num >>= 1
|
||||||
|
return counter
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
num = 0b1001
|
||||||
|
n = '0b1010'
|
||||||
|
p = '0b110'
|
||||||
|
print_prev_same_1s(num) == p
|
||||||
|
print_next_same_1s(num) == n
|
||||||
|
|
40
first_edition/ebook_src/bitwise_operations/num_bits_to_convert_2_nums.py
Executable file
40
first_edition/ebook_src/bitwise_operations/num_bits_to_convert_2_nums.py
Executable file
@ -0,0 +1,40 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
''' This method returns the number of bits that are necessary to change to convert two
|
||||||
|
numbers A and B:
|
||||||
|
1) XOR
|
||||||
|
2) count 1s
|
||||||
|
'''
|
||||||
|
|
||||||
|
def count_bits_swap2(a, b):
|
||||||
|
count = 0
|
||||||
|
m = a^b
|
||||||
|
while m:
|
||||||
|
count +=1
|
||||||
|
m = m & (m-1)
|
||||||
|
return count
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def count_bits_swap(a, b):
|
||||||
|
m = a^b
|
||||||
|
return count_1s(m)
|
||||||
|
|
||||||
|
|
||||||
|
def count_1s(m):
|
||||||
|
count = 0
|
||||||
|
while m:
|
||||||
|
if m& 1 :
|
||||||
|
count +=1
|
||||||
|
m >>= 1
|
||||||
|
return count
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
a = int('10010000', 2)
|
||||||
|
b = int('01011010', 2)
|
||||||
|
print count_bits_swap(a, b) #4
|
||||||
|
print count_bits_swap2(a, b) #4
|
26
first_edition/ebook_src/bitwise_operations/set_bit.py
Executable file
26
first_edition/ebook_src/bitwise_operations/set_bit.py
Executable file
@ -0,0 +1,26 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
''' Set a bit in a binary number:
|
||||||
|
1) Shifts 1 over by i bits
|
||||||
|
2) make an OR with the number, only the value at bit i will change and all the others bit
|
||||||
|
of the mask are zero so will not affect the num
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
def set_bit(num, i):
|
||||||
|
mask = 1 << i
|
||||||
|
return bin( num | mask )
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
num = int('0100100', 2)
|
||||||
|
print set_bit(num, 0) #'0b100101'
|
||||||
|
print set_bit(num, 1) #'0b100110'
|
||||||
|
print set_bit(num, 2) # nothing change '0b100100'
|
||||||
|
print set_bit(num, 3) #'0b101100'
|
||||||
|
print set_bit(num, 4) #'0b110100'
|
||||||
|
print set_bit(num, 5) # nothing change '0b100100'
|
23
first_edition/ebook_src/bitwise_operations/swap_in_place.py
Executable file
23
first_edition/ebook_src/bitwise_operations/swap_in_place.py
Executable file
@ -0,0 +1,23 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
'''
|
||||||
|
swapping values in place without extra memory
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
def swap_bit(a, b):
|
||||||
|
'''
|
||||||
|
>>> swap_bit(14, 73)
|
||||||
|
(73, 14)
|
||||||
|
'''
|
||||||
|
a = a^b
|
||||||
|
b = a^b
|
||||||
|
a = a^b
|
||||||
|
return a, b
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
37
first_edition/ebook_src/bitwise_operations/swap_odd_even.py
Executable file
37
first_edition/ebook_src/bitwise_operations/swap_odd_even.py
Executable file
@ -0,0 +1,37 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
''' Swap odd and even bits in a smart way in a binary:
|
||||||
|
1) first for odds, take n and move the odd:
|
||||||
|
(a) Mask all odd bits with 10101010 (0xAA)
|
||||||
|
(b) shift by right by 1
|
||||||
|
2) do the same to ints with 01010101
|
||||||
|
3) merge
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def swap_odd_even(num):
|
||||||
|
'''
|
||||||
|
>>> num = 0b11011101
|
||||||
|
>>> result = '0b1101110'
|
||||||
|
>>> swap_odd_even(num) == result
|
||||||
|
True
|
||||||
|
'''
|
||||||
|
|
||||||
|
mask_odd = 0xAA # 0b10101010
|
||||||
|
mask_even = 0x55 # 0b1010101
|
||||||
|
odd = num & mask_odd
|
||||||
|
odd >>= 1
|
||||||
|
even = num & mask_even
|
||||||
|
even >>= 1
|
||||||
|
return bin(odd | even)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|
22
first_edition/ebook_src/bitwise_operations/update_bit.py
Executable file
22
first_edition/ebook_src/bitwise_operations/update_bit.py
Executable file
@ -0,0 +1,22 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
''' This method merges set bit and clean bit:
|
||||||
|
1) first clear the bit at i using a mask such as 1110111
|
||||||
|
2) then shift the intended value v by i bits
|
||||||
|
3) this will create a number with bit i to v and all other to 0
|
||||||
|
4) finally update the ith bit with or
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def update_bit(num, i, v):
|
||||||
|
mask = ~ (1 << i)
|
||||||
|
return bin( (num & mask) | (v << i) )
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
num = int('10010000', 2)
|
||||||
|
print update_bit(num, 2, 1) # '0b10010100'
|
||||||
|
|
0
first_edition/ebook_src/builtin_structures/__init__.py
Executable file
0
first_edition/ebook_src/builtin_structures/__init__.py
Executable file
51
first_edition/ebook_src/builtin_structures/anagram.py
Executable file
51
first_edition/ebook_src/builtin_structures/anagram.py
Executable file
@ -0,0 +1,51 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
from collections import Counter
|
||||||
|
|
||||||
|
def is_anagram(s1, s2):
|
||||||
|
'''
|
||||||
|
>>> is_anagram('cat', 'tac')
|
||||||
|
True
|
||||||
|
>>> is_anagram('cat', 'hat')
|
||||||
|
False
|
||||||
|
'''
|
||||||
|
counter = Counter()
|
||||||
|
for c in s1:
|
||||||
|
counter[c] += 1
|
||||||
|
|
||||||
|
for c in s2:
|
||||||
|
counter[c] -= 1
|
||||||
|
|
||||||
|
for i in counter.values():
|
||||||
|
if i:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
''' verify if words are anagrams by comparing a sum of Unicode code
|
||||||
|
point of the character'''
|
||||||
|
|
||||||
|
def get_unicode_sum(word):
|
||||||
|
s = 0
|
||||||
|
for p in word:
|
||||||
|
s += ord(p)
|
||||||
|
return s
|
||||||
|
|
||||||
|
|
||||||
|
def is_anagram2(word1, word2):
|
||||||
|
'''
|
||||||
|
>>> is_anagram2('cat', 'tac')
|
||||||
|
True
|
||||||
|
>>> is_anagram2('cat', 'hat')
|
||||||
|
False
|
||||||
|
'''
|
||||||
|
return get_unicode_sum(word1) == get_unicode_sum(word2)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|
9
first_edition/ebook_src/builtin_structures/balance.txt
Executable file
9
first_edition/ebook_src/builtin_structures/balance.txt
Executable file
@ -0,0 +1,9 @@
|
|||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
This is the classic "you have 8 balls/coins, which are the same weight, except for one which is slightly heavier than the others. You also have an old-style balance. What is the fewest number of weighings to find the heavy coin/ball?
|
||||||
|
|
||||||
|
Answer: 2! You need to use every information available:
|
||||||
|
Weight 3 x 3 balls/coins.
|
||||||
|
If they weight the same: weight the 2 balls/coins left outside.
|
||||||
|
Else, measure 2 of the 3 heavier balls/coins.
|
40
first_edition/ebook_src/builtin_structures/balance_symbols.py
Executable file
40
first_edition/ebook_src/builtin_structures/balance_symbols.py
Executable file
@ -0,0 +1,40 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
'''
|
||||||
|
Given a N different open and close braces in a string "( { [ } ] )".
|
||||||
|
How do you check whether the string has matching braces.
|
||||||
|
'''
|
||||||
|
|
||||||
|
from collections import Counter
|
||||||
|
def check_if_balance(string):
|
||||||
|
'''
|
||||||
|
>>> check_if_balance('{[[(])}]')
|
||||||
|
True
|
||||||
|
>>> check_if_balance('{[[()}]')
|
||||||
|
False
|
||||||
|
>>> check_if_balance('')
|
||||||
|
True
|
||||||
|
'''
|
||||||
|
table = Counter()
|
||||||
|
for i in string:
|
||||||
|
|
||||||
|
index = str(ord(i))[0]
|
||||||
|
if i in '{[(':
|
||||||
|
table[index] += 1
|
||||||
|
|
||||||
|
elif i in ')}]':
|
||||||
|
table[index] -= 1
|
||||||
|
|
||||||
|
for i in table.values():
|
||||||
|
if i !=-0:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|
71
first_edition/ebook_src/builtin_structures/check_if_2_numbers_sum_to_k.py
Executable file
71
first_edition/ebook_src/builtin_structures/check_if_2_numbers_sum_to_k.py
Executable file
@ -0,0 +1,71 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
"""
|
||||||
|
Given an integer x and an unsorted array of integers, describe an
|
||||||
|
algorithm to determine whether two of the numbers add up to x.
|
||||||
|
|
||||||
|
1. Using hash tables.
|
||||||
|
2. Sorting the array and keeping two pointers in the array, one in
|
||||||
|
the beginning and one in the end. Whenever the sum of the current
|
||||||
|
two integers is less than x, move the first pointer forwards, and
|
||||||
|
whenever the sum is greater than x, move the second pointer
|
||||||
|
backwards. O(nln n).
|
||||||
|
3. Create a BST with x minus each element in the array.
|
||||||
|
Check whether any element of the array appears in the BST.
|
||||||
|
It takes O(nlog n) times two.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from collections import defaultdict, Counter
|
||||||
|
|
||||||
|
def check_sum(array, k):
|
||||||
|
'''
|
||||||
|
>>> check_sum([3, 2, 6, 7, 9, 1], 8)
|
||||||
|
[(6, 2), (1, 7)]
|
||||||
|
>>> check_sum([5, 2, 6, 7, 9, 1], 4)
|
||||||
|
[]
|
||||||
|
>>>
|
||||||
|
'''
|
||||||
|
|
||||||
|
dict = defaultdict()
|
||||||
|
res = []
|
||||||
|
|
||||||
|
for i in array:
|
||||||
|
if k-i in dict:
|
||||||
|
res.append((i, k-i))
|
||||||
|
del dict[k-i]
|
||||||
|
else:
|
||||||
|
dict[i] = 1
|
||||||
|
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
def check_sum2(array, k):
|
||||||
|
'''
|
||||||
|
>>> check_sum2([1, 4, 2, 7, 1, 3, 10, 15, 3, 1], 6)
|
||||||
|
set([(3, 3)])
|
||||||
|
>>> check_sum2([1, 4, 2, 7, 1, 3, 10, 15, 3, 1], 0)
|
||||||
|
set([])
|
||||||
|
'''
|
||||||
|
|
||||||
|
dict = Counter()
|
||||||
|
res = set()
|
||||||
|
|
||||||
|
for i in array:
|
||||||
|
dict[i] += 1
|
||||||
|
|
||||||
|
for i in array:
|
||||||
|
if dict[k-i] > 0:
|
||||||
|
if i == k-i and dict[k-i] > 1:
|
||||||
|
res.add((i, k-i))
|
||||||
|
dict[k-i] -= 2
|
||||||
|
elif i == k-i:
|
||||||
|
res.add((i, k-i))
|
||||||
|
dict[k-i] -= 1
|
||||||
|
|
||||||
|
return res
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
36
first_edition/ebook_src/builtin_structures/check_if_3_numbers_sum_to_zero.py
Executable file
36
first_edition/ebook_src/builtin_structures/check_if_3_numbers_sum_to_zero.py
Executable file
@ -0,0 +1,36 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
''' Determine if an Array of integers contains 3 numbers that sum to 0 '''
|
||||||
|
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
|
def find_3_number(array):
|
||||||
|
'''
|
||||||
|
>>> find_3_number([1,5,56,11,-3,-12])
|
||||||
|
1 11 -12
|
||||||
|
True
|
||||||
|
>>> find_3_number([] )
|
||||||
|
False
|
||||||
|
'''
|
||||||
|
hash_ = defaultdict()
|
||||||
|
for i in array:
|
||||||
|
hash_[i] = 1
|
||||||
|
|
||||||
|
for i, x in enumerate(array):
|
||||||
|
for y in array[i+1:]:
|
||||||
|
if -(x+y) in hash_:
|
||||||
|
print x, y, -(x+y)
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|
@ -0,0 +1,35 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
'''
|
||||||
|
given an array of intervals, return max number of non-overlapping intervals
|
||||||
|
'''
|
||||||
|
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
|
def non_overlapping(array):
|
||||||
|
'''
|
||||||
|
>>> non_overlapping([(1,2), (2,5), (1, 6)])
|
||||||
|
[[(1, 2), (2, 5)]]
|
||||||
|
>>> non_overlapping([(1,4), (2,5), (3, 6)])
|
||||||
|
[]
|
||||||
|
'''
|
||||||
|
total = []
|
||||||
|
|
||||||
|
for i, t in enumerate(array):
|
||||||
|
start = t[0]
|
||||||
|
end = t[1]
|
||||||
|
for tt in array[i+1:] :
|
||||||
|
if end <= tt[0]:
|
||||||
|
total.append([(start, end), (tt[0], tt[1])])
|
||||||
|
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|
64
first_edition/ebook_src/builtin_structures/convert_numerical_bases.py
Executable file
64
first_edition/ebook_src/builtin_structures/convert_numerical_bases.py
Executable file
@ -0,0 +1,64 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
''' convert an integer to a string in any base'''
|
||||||
|
|
||||||
|
def convert_from_dec_to_any_base(number, base):
|
||||||
|
'''
|
||||||
|
>>> number, base = 9, 2
|
||||||
|
>>> convert_from_dec_to_any_base(number, base)
|
||||||
|
'1001'
|
||||||
|
'''
|
||||||
|
|
||||||
|
convertString = '012345679ABCDEF'
|
||||||
|
|
||||||
|
if number < base:
|
||||||
|
return convertString[number]
|
||||||
|
|
||||||
|
else:
|
||||||
|
return convert_from_dec_to_any_base(number//base, base) + \
|
||||||
|
convertString[number%base]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def convert_from_decimal_to_binary(number, base):
|
||||||
|
'''
|
||||||
|
>>> number, base = 9, 2
|
||||||
|
>>> convert_from_decimal_to_binary(number, base)
|
||||||
|
1001
|
||||||
|
'''
|
||||||
|
|
||||||
|
multiplier, result = 1, 0
|
||||||
|
|
||||||
|
while number > 0:
|
||||||
|
result += number%base*multiplier
|
||||||
|
multiplier *= 10
|
||||||
|
number = number//base
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def convert_from_decimal_larger_bases(number, base):
|
||||||
|
'''
|
||||||
|
>>> number, base = 31, 16
|
||||||
|
>>> convert_from_decimal_larger_bases(number, base)
|
||||||
|
'1F'
|
||||||
|
'''
|
||||||
|
strings = "0123456789ABCDEFGHIJ"
|
||||||
|
result = ""
|
||||||
|
|
||||||
|
while number > 0:
|
||||||
|
digit = number%base
|
||||||
|
result = strings[digit] + result
|
||||||
|
number = number//base
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|
83
first_edition/ebook_src/builtin_structures/convert_str_2_int.py
Executable file
83
first_edition/ebook_src/builtin_structures/convert_str_2_int.py
Executable file
@ -0,0 +1,83 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
def conv_int2str(int1):
|
||||||
|
'''
|
||||||
|
>>> conv_str2int('367')
|
||||||
|
367
|
||||||
|
>>> conv_str2int('0')
|
||||||
|
0
|
||||||
|
>>> conv_str2int('-10')
|
||||||
|
-10
|
||||||
|
>>> conv_str2int('1e5')
|
||||||
|
100000
|
||||||
|
'''
|
||||||
|
|
||||||
|
aux_dict = {key:value for key in range(10) for value in '0123456789'[key]}
|
||||||
|
|
||||||
|
if int1 == 0:
|
||||||
|
return '0'
|
||||||
|
elif int1 < 0:
|
||||||
|
sign = '-'
|
||||||
|
int1 = int1*(-1)
|
||||||
|
else:
|
||||||
|
sign = ''
|
||||||
|
|
||||||
|
|
||||||
|
aux_ls = []
|
||||||
|
|
||||||
|
while int1 > 0:
|
||||||
|
c = int1%10
|
||||||
|
int1 = int1//10
|
||||||
|
cadd = aux_dict[c]
|
||||||
|
aux_ls.append(cadd)
|
||||||
|
|
||||||
|
aux_ls.reverse()
|
||||||
|
|
||||||
|
return sign + ''.join(aux_ls)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def conv_str2int(str1):
|
||||||
|
'''
|
||||||
|
>>> conv_int2str(0)
|
||||||
|
'0'
|
||||||
|
>>> conv_int2str(1e5)
|
||||||
|
'100000'
|
||||||
|
>>> conv_int2str(367)
|
||||||
|
'367'
|
||||||
|
>>> conv_int2str(-10)
|
||||||
|
'-10'
|
||||||
|
'''
|
||||||
|
if not str1:
|
||||||
|
return None
|
||||||
|
|
||||||
|
aux_dict = {key:value for value in range(10) for key in '0123456789'[value]}
|
||||||
|
|
||||||
|
if str1[0] == '-':
|
||||||
|
sign = -1
|
||||||
|
str1 = str1[1:]
|
||||||
|
else:
|
||||||
|
sign = 1
|
||||||
|
|
||||||
|
dec, result = 1, 0
|
||||||
|
|
||||||
|
for i in range(len(str1)-1, -1, -1):
|
||||||
|
aux = str1[i]
|
||||||
|
if aux == 'e':
|
||||||
|
exp_num = conv_str2int(str1[i+1:])
|
||||||
|
number = conv_str2int(str1[:i])
|
||||||
|
result = number*10**exp_num
|
||||||
|
break
|
||||||
|
result += aux_dict[aux]*dec
|
||||||
|
dec *= 10
|
||||||
|
|
||||||
|
return result*sign
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|
26
first_edition/ebook_src/builtin_structures/count_unique_words_On2.py
Executable file
26
first_edition/ebook_src/builtin_structures/count_unique_words_On2.py
Executable file
@ -0,0 +1,26 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
import collections
|
||||||
|
import string
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
def count_unique_word():
|
||||||
|
|
||||||
|
words = collections.defaultdict(int)
|
||||||
|
|
||||||
|
strip = string.whitespace + string.punctuation + string.digits + "\"'"
|
||||||
|
|
||||||
|
for filename in sys.argv[1:]:
|
||||||
|
with open(filename) as file:
|
||||||
|
for line in file:
|
||||||
|
for word in line.lower().split():
|
||||||
|
word = word.strip(strip)
|
||||||
|
if len(word) > 2:
|
||||||
|
words[word] = +1
|
||||||
|
|
||||||
|
for word in sorted(words):
|
||||||
|
print("'{0}' occurs {1} times.".format(word, words[word]))
|
||||||
|
|
49
first_edition/ebook_src/builtin_structures/delete_duplicate_char_str.py
Executable file
49
first_edition/ebook_src/builtin_structures/delete_duplicate_char_str.py
Executable file
@ -0,0 +1,49 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
''' find and delete all the duplicate characters in a string '''
|
||||||
|
|
||||||
|
from collections import Counter
|
||||||
|
|
||||||
|
def delete_unique(str1):
|
||||||
|
'''
|
||||||
|
>>> delete_unique("Trust no one")
|
||||||
|
'on'
|
||||||
|
>>> delete_unique("Mulder?")
|
||||||
|
''
|
||||||
|
'''
|
||||||
|
|
||||||
|
str_strip = ''.join(str1.split())
|
||||||
|
repeat = Counter()
|
||||||
|
|
||||||
|
for c in str_strip:
|
||||||
|
repeat[c] += 1
|
||||||
|
|
||||||
|
result = ''
|
||||||
|
for c, count in repeat.items():
|
||||||
|
if count > 1:
|
||||||
|
result += c
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def removing_duplicates_seq(str1):
|
||||||
|
'''
|
||||||
|
>>> delete_unique("Trust no one")
|
||||||
|
'on'
|
||||||
|
>>> delete_unique("Mulder?")
|
||||||
|
''
|
||||||
|
'''
|
||||||
|
seq = str1.split()
|
||||||
|
result = set()
|
||||||
|
for item in seq:
|
||||||
|
if item not in result:
|
||||||
|
#yield item
|
||||||
|
result.add(item)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
60
first_edition/ebook_src/builtin_structures/fibonacci.py
Executable file
60
first_edition/ebook_src/builtin_structures/fibonacci.py
Executable file
@ -0,0 +1,60 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
def fib_generator():
|
||||||
|
a, b = 0, 1
|
||||||
|
|
||||||
|
while True:
|
||||||
|
yield b
|
||||||
|
a, b = b, a+b
|
||||||
|
|
||||||
|
|
||||||
|
def fib(n):
|
||||||
|
'''
|
||||||
|
>>> fib(2)
|
||||||
|
1
|
||||||
|
>>> fib(5)
|
||||||
|
5
|
||||||
|
>>> fib(7)
|
||||||
|
13
|
||||||
|
'''
|
||||||
|
if n < 3:
|
||||||
|
return 1
|
||||||
|
|
||||||
|
a, b = 0, 1
|
||||||
|
count = 1
|
||||||
|
|
||||||
|
while count < n:
|
||||||
|
count += 1
|
||||||
|
a, b = b, a+b
|
||||||
|
|
||||||
|
return b
|
||||||
|
|
||||||
|
|
||||||
|
def fib_rec(n):
|
||||||
|
'''
|
||||||
|
>>> fib_rec(2)
|
||||||
|
1
|
||||||
|
>>> fib_rec(5)
|
||||||
|
5
|
||||||
|
>>> fib_rec(7)
|
||||||
|
13
|
||||||
|
'''
|
||||||
|
if n < 3:
|
||||||
|
return 1
|
||||||
|
return fib_rec(n - 1) + fib_rec(n - 2)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|
||||||
|
fib = fib_generator()
|
||||||
|
print(next(fib))
|
||||||
|
print(next(fib))
|
||||||
|
print(next(fib))
|
||||||
|
print(next(fib))
|
35
first_edition/ebook_src/builtin_structures/find_0_MxN_replace_cols_rows.py
Executable file
35
first_edition/ebook_src/builtin_structures/find_0_MxN_replace_cols_rows.py
Executable file
@ -0,0 +1,35 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
def find_0_MxN(m):
|
||||||
|
''' find 0s in a matrix and replace the col and row to 0s:
|
||||||
|
>>> m1 = [[1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16]]
|
||||||
|
>>> find_0_MxN(m1)
|
||||||
|
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
|
||||||
|
>>> m2 = [[1, 2, 3, 4], [0, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
|
||||||
|
>>> find_0_MxN(m2)
|
||||||
|
[[0, 2, 3, 4], [0, 0, 0, 0], [0, 10, 11, 12], [0, 14, 15, 16]]
|
||||||
|
'''
|
||||||
|
index = []
|
||||||
|
|
||||||
|
for row in range(len(m)):
|
||||||
|
for col in range(len(m[0])):
|
||||||
|
if m[row][col] == 0:
|
||||||
|
index.append((row, col))
|
||||||
|
for i in index:
|
||||||
|
row = i[0]
|
||||||
|
col = i[1]
|
||||||
|
for i in range(len(m)):
|
||||||
|
m[row][i] = 0
|
||||||
|
for i in range(len(m[0])):
|
||||||
|
m[i][col] = 0
|
||||||
|
|
||||||
|
return m
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|
32
first_edition/ebook_src/builtin_structures/find_dice_probabilities.py
Executable file
32
first_edition/ebook_src/builtin_structures/find_dice_probabilities.py
Executable file
@ -0,0 +1,32 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
'''
|
||||||
|
given 2 dice, determine number of ways to sum S if all dice are rolled
|
||||||
|
'''
|
||||||
|
|
||||||
|
from collections import Counter, defaultdict
|
||||||
|
|
||||||
|
def find_dice_probabilities(S=5, n_faces=6):
|
||||||
|
if S > 2*n_faces or S < 2:
|
||||||
|
return None
|
||||||
|
|
||||||
|
cdict = Counter()
|
||||||
|
ddict = defaultdict(list)
|
||||||
|
|
||||||
|
for dice1 in range(1, n_faces+1):
|
||||||
|
for dice2 in range(1, n_faces+1):
|
||||||
|
t = [dice1, dice2]
|
||||||
|
cdict[dice1+dice2] += 1
|
||||||
|
ddict[dice1+dice2].append( t)
|
||||||
|
|
||||||
|
return [cdict[S], ddict[S]]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
results = find_dice_probabilities()
|
||||||
|
assert(results[0] == len(results[1]))
|
43
first_edition/ebook_src/builtin_structures/find_edit_distance.py
Executable file
43
first_edition/ebook_src/builtin_structures/find_edit_distance.py
Executable file
@ -0,0 +1,43 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
''' computes the edit distance between two strings '''
|
||||||
|
|
||||||
|
|
||||||
|
def find_edit_distance(str1, str2):
|
||||||
|
'''
|
||||||
|
>>> s = 'sunday'
|
||||||
|
>>> t = 'saturday'
|
||||||
|
>>> find_edit_distance(s, t)
|
||||||
|
3
|
||||||
|
'''
|
||||||
|
|
||||||
|
m = len(str1)
|
||||||
|
n = len(str2)
|
||||||
|
diff = lambda c1, c2: 0 if c1 == c2 else 1
|
||||||
|
|
||||||
|
E = [[0] * (n + 1) for i in range(m + 1)]
|
||||||
|
|
||||||
|
for i in range(m + 1):
|
||||||
|
E[i][0] = i
|
||||||
|
|
||||||
|
for j in range(1, n + 1):
|
||||||
|
E[0][j] = j
|
||||||
|
|
||||||
|
for i in range(1, m + 1):
|
||||||
|
for j in range(1, n + 1):
|
||||||
|
E[i][j] = min(E[i-1][j] + 1, E[i][j-1] + 1, E[i-1][j-1] + diff(str1[i-1], str2[j-1]))
|
||||||
|
|
||||||
|
return E[m][n]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
29
first_edition/ebook_src/builtin_structures/find_first_non_repetead_char.py
Executable file
29
first_edition/ebook_src/builtin_structures/find_first_non_repetead_char.py
Executable file
@ -0,0 +1,29 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
from collections import Counter
|
||||||
|
|
||||||
|
def find_non_rep_char(s1):
|
||||||
|
'''
|
||||||
|
>>> s1 = 'aabbcceff'
|
||||||
|
>>> find_non_rep_char(s1)
|
||||||
|
e
|
||||||
|
>>> find_non_rep_char('ccc')
|
||||||
|
'''
|
||||||
|
|
||||||
|
aux_dict = Counter()
|
||||||
|
|
||||||
|
for i in s1:
|
||||||
|
aux_dict[i] += 1
|
||||||
|
|
||||||
|
for k, v in aux_dict.items():
|
||||||
|
if v < 2:
|
||||||
|
print k
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|
29
first_edition/ebook_src/builtin_structures/find_gcd.py
Executable file
29
first_edition/ebook_src/builtin_structures/find_gcd.py
Executable file
@ -0,0 +1,29 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def finding_gcd(a, b):
|
||||||
|
''' implements the greatest common divider algorithm '''
|
||||||
|
while(b != 0):
|
||||||
|
result = b
|
||||||
|
a, b = b, a % b
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def test_finding_gcd():
|
||||||
|
number1 = 21
|
||||||
|
number2 = 12
|
||||||
|
assert(finding_gcd(number1, number2) == 3)
|
||||||
|
print('Tests passed!')
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
test_finding_gcd()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
36
first_edition/ebook_src/builtin_structures/find_if_substr.py
Executable file
36
first_edition/ebook_src/builtin_structures/find_if_substr.py
Executable file
@ -0,0 +1,36 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
def find_substr(s1, s2):
|
||||||
|
|
||||||
|
if len(s1) < len(s2):
|
||||||
|
bs = s2
|
||||||
|
ss = s1
|
||||||
|
else:
|
||||||
|
bs = s1
|
||||||
|
ss = s2
|
||||||
|
|
||||||
|
ps = 0
|
||||||
|
|
||||||
|
for c in bs:
|
||||||
|
|
||||||
|
if ss[ps] == c:
|
||||||
|
ps += 1
|
||||||
|
else:
|
||||||
|
ps = 0
|
||||||
|
|
||||||
|
if ps == len(ss)-1:
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
s1 = 'buffy is a vampire slayer'
|
||||||
|
s2 = 'vampire'
|
||||||
|
s3 = 'angel'
|
||||||
|
assert(find_substr(s2, s1) == True)
|
||||||
|
assert(find_substr(s3, s1) == False)
|
32
first_edition/ebook_src/builtin_structures/find_if_unique_char.py
Executable file
32
first_edition/ebook_src/builtin_structures/find_if_unique_char.py
Executable file
@ -0,0 +1,32 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import collections
|
||||||
|
|
||||||
|
def find_if_unique_chars(word):
|
||||||
|
"""
|
||||||
|
>>> find_if_unique_chars('abcde')
|
||||||
|
True
|
||||||
|
>>> find_if_unique_chars('abcae')
|
||||||
|
False
|
||||||
|
"""
|
||||||
|
|
||||||
|
unique = True
|
||||||
|
|
||||||
|
counter = collections.Counter()
|
||||||
|
|
||||||
|
for c in word:
|
||||||
|
if not counter[c]:
|
||||||
|
counter[c] += 1
|
||||||
|
else:
|
||||||
|
unique = False
|
||||||
|
|
||||||
|
return unique
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
42
first_edition/ebook_src/builtin_structures/find_largest_sum.py
Executable file
42
first_edition/ebook_src/builtin_structures/find_largest_sum.py
Executable file
@ -0,0 +1,42 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
'''
|
||||||
|
You are given an array of integers (both positive and negative).
|
||||||
|
Find the contiguous sequence with the largest sum.
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
def find_largest_sum(array):
|
||||||
|
'''
|
||||||
|
>>> find_largest_sum([-1, 2, -3, 5, 3, 1, -16, 7, 1, -13, 1])
|
||||||
|
9
|
||||||
|
>>> find_largest_sum([])
|
||||||
|
0
|
||||||
|
>>> find_largest_sum([1])
|
||||||
|
1
|
||||||
|
'''
|
||||||
|
|
||||||
|
sum_ = 0
|
||||||
|
sum_here = 0
|
||||||
|
|
||||||
|
for i in array:
|
||||||
|
|
||||||
|
sum_here += i
|
||||||
|
|
||||||
|
if sum_here < 0:
|
||||||
|
sum_here = 0
|
||||||
|
|
||||||
|
if sum_ < sum_here:
|
||||||
|
sum_ = sum_here
|
||||||
|
|
||||||
|
return sum_
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
34
first_edition/ebook_src/builtin_structures/find_longest_inc_subseq.py
Executable file
34
first_edition/ebook_src/builtin_structures/find_longest_inc_subseq.py
Executable file
@ -0,0 +1,34 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
''' find the longest continuous increasing subsequence'''
|
||||||
|
|
||||||
|
|
||||||
|
def find_long_con_inc(seq):
|
||||||
|
'''
|
||||||
|
>>> find_long_con_inc([1, -2, 3, 5, 1, -1, 4, -1, 6])
|
||||||
|
[-2, 3, 5]
|
||||||
|
>>> find_long_con_inc([1, 3, -2, 3, 5, 6])
|
||||||
|
[-2, 3, 5, 6]
|
||||||
|
>>> find_long_con_inc([1, 3, 4, -13, 2, 5, 8, -1, 2,-17])
|
||||||
|
[-13, 2, 5, 8]
|
||||||
|
'''
|
||||||
|
|
||||||
|
res, aux = [], []
|
||||||
|
seq.append(-float('infinity'))
|
||||||
|
|
||||||
|
for i, n in enumerate(seq[:-1]):
|
||||||
|
aux.append(n)
|
||||||
|
if n > seq[i+1]:
|
||||||
|
if len(res) < len(aux):
|
||||||
|
res = aux[:]
|
||||||
|
aux = []
|
||||||
|
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
38
first_edition/ebook_src/builtin_structures/find_longest_str_unique_chars.py
Executable file
38
first_edition/ebook_src/builtin_structures/find_longest_str_unique_chars.py
Executable file
@ -0,0 +1,38 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
'''
|
||||||
|
given a string, find longest string with unique characters
|
||||||
|
'''
|
||||||
|
|
||||||
|
def find_longest(string):
|
||||||
|
'''
|
||||||
|
>>> find_longest('abfgrhgtrdsandwejfhdasjcbdsjvrejwghireeej')
|
||||||
|
'wejfhdas'
|
||||||
|
>>> find_longest('abcabcabcabcdefabcccc')
|
||||||
|
'defabc'
|
||||||
|
'''
|
||||||
|
maxs = ''
|
||||||
|
result = ''
|
||||||
|
|
||||||
|
for c in string:
|
||||||
|
if c in result:
|
||||||
|
if len(maxs) < len(result):
|
||||||
|
maxs = result
|
||||||
|
result = ''
|
||||||
|
else:
|
||||||
|
result += c
|
||||||
|
|
||||||
|
if result and len(maxs) < len(result):
|
||||||
|
maxs = result
|
||||||
|
|
||||||
|
return maxs
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|
33
first_edition/ebook_src/builtin_structures/find_non_repeating_number.py
Executable file
33
first_edition/ebook_src/builtin_structures/find_non_repeating_number.py
Executable file
@ -0,0 +1,33 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
|
|
||||||
|
def find_unique_number(array):
|
||||||
|
'''
|
||||||
|
>>> find_unique_number([1, 3, 6, 1, 5, 6, 9, 3, 7])
|
||||||
|
[1, 6, 3]
|
||||||
|
>>> find_unique_number([1, 3, 5, 6, 9, 7])
|
||||||
|
[]
|
||||||
|
'''
|
||||||
|
|
||||||
|
table = defaultdict()
|
||||||
|
total = []
|
||||||
|
|
||||||
|
for i in array:
|
||||||
|
if i in table:
|
||||||
|
total.append(i)
|
||||||
|
else:
|
||||||
|
table[i] = 1
|
||||||
|
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
42
first_edition/ebook_src/builtin_structures/find_product_without_division.py
Executable file
42
first_edition/ebook_src/builtin_structures/find_product_without_division.py
Executable file
@ -0,0 +1,42 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
'''Given an array of numbers, replace each number with the product of all
|
||||||
|
the numbers in the array except the number itself *without* using division
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
def find_product_without_division(seq):
|
||||||
|
'''
|
||||||
|
>>> seq = [2,3,4]
|
||||||
|
>>> find_product_without_division(seq)
|
||||||
|
[12, 8, 6]
|
||||||
|
'''
|
||||||
|
|
||||||
|
forw = []
|
||||||
|
bacw = []
|
||||||
|
|
||||||
|
for i in range(len(seq)):
|
||||||
|
|
||||||
|
prod_f, prod_b = 1, 1
|
||||||
|
|
||||||
|
for next in range(i+1, len(seq)):
|
||||||
|
prod_f *= seq[next]
|
||||||
|
|
||||||
|
for before in range(0, i):
|
||||||
|
prod_b *= seq[before]
|
||||||
|
|
||||||
|
forw.append(prod_f)
|
||||||
|
bacw.append(prod_b)
|
||||||
|
|
||||||
|
for i in range(len(seq)):
|
||||||
|
seq[i] = forw[i] * bacw[i]
|
||||||
|
|
||||||
|
return seq
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
27
first_edition/ebook_src/builtin_structures/find_top_N_recurring_words.py
Executable file
27
first_edition/ebook_src/builtin_structures/find_top_N_recurring_words.py
Executable file
@ -0,0 +1,27 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
from collections import Counter
|
||||||
|
|
||||||
|
def find_top_N_recurring_words(seq, N):
|
||||||
|
''' find the top N recurring words in a file:
|
||||||
|
1) use a hash table to find counts
|
||||||
|
2) sort the list on base of the maximum counts
|
||||||
|
3) return the last N words
|
||||||
|
'''
|
||||||
|
|
||||||
|
dcounter = Counter()
|
||||||
|
for word in seq.split():
|
||||||
|
dcounter[word] += 1
|
||||||
|
|
||||||
|
return dcounter.most_common(N)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
seq = 'buffy angel monster xander a willow gg buffy the monster super buffy angel'
|
||||||
|
N = 3
|
||||||
|
assert(find_top_N_recurring_words(seq, N) == [('buffy', 3), ('monster', 2), ('angel', 2)])
|
||||||
|
|
||||||
|
|
@ -0,0 +1,56 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
"""
|
||||||
|
Two numbers out of n numbers from 1 to n are missing.
|
||||||
|
The remaining n-2 numbers are in the array but not sorted.
|
||||||
|
Find the missing numbers The sum1 is the sum of all the elements in n.
|
||||||
|
The sum2 is the sum of all the elements in n-2. sum1 - sum2 = num1 + num2 = s.
|
||||||
|
The prod1 is the prod of all the elements in n. The prod2 is the prod of all
|
||||||
|
the elements in n-2. prod1/prod2 = num1*num2 =p.
|
||||||
|
Runtime is O(n), because it scan 3 times. Space is O(1)
|
||||||
|
|
||||||
|
In case of finding one integer, Let the missing number be M. We know that
|
||||||
|
the sum of first N natural numbers is N*(N+1)/2. Traverse through the array
|
||||||
|
once and calculate the sum. This is the sum of first N natural numbers -
|
||||||
|
M or S=N*(N+1)/2 - M. Therefore M = N*(N+1)/2 - S.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import math
|
||||||
|
|
||||||
|
def find_two_missing_numbers(l1):
|
||||||
|
'''
|
||||||
|
>>> l1 = [1, 3, 5]
|
||||||
|
>>> find_two_missing_numbers(l1)
|
||||||
|
(4, 2)
|
||||||
|
'''
|
||||||
|
|
||||||
|
n_min_2 = len(l1)
|
||||||
|
n = n_min_2 + 2
|
||||||
|
sum1, sum2, prod1, prod2 = 0, 0, 1, 1
|
||||||
|
sum2 = sum(l1[:])
|
||||||
|
sum1 = sum(range(1,n+1))
|
||||||
|
s = sum1 - sum2
|
||||||
|
|
||||||
|
for i in range(1, n-1):
|
||||||
|
prod1 = prod1*i
|
||||||
|
prod2 = prod2*l1[i-1]
|
||||||
|
|
||||||
|
prod1 = prod1*n*(n-1)
|
||||||
|
p = prod1/prod2
|
||||||
|
num1 = (s + math.sqrt(s*s - 4*p))/2
|
||||||
|
num2 = (s - math.sqrt(s*s - 4*p))/2
|
||||||
|
|
||||||
|
return int(num1), int(num2)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
29
first_edition/ebook_src/builtin_structures/get_float_rep_bin.py
Executable file
29
first_edition/ebook_src/builtin_structures/get_float_rep_bin.py
Executable file
@ -0,0 +1,29 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
''' Given a real number between 0 and 1 (eg: 0.72), this method print the
|
||||||
|
binary representation. If the Number cannot be represented accurately
|
||||||
|
in binary, with at exit most 32 chars, print error:
|
||||||
|
'''
|
||||||
|
|
||||||
|
def get_float_rep(num):
|
||||||
|
if num >= 1 or num <= 0: return 'Error 1'
|
||||||
|
result = '.'
|
||||||
|
while num:
|
||||||
|
if len(result) >= 32: return 'Error 2', result
|
||||||
|
r = num*2
|
||||||
|
if r >= 1:
|
||||||
|
result += '1'
|
||||||
|
num = r - 1
|
||||||
|
else:
|
||||||
|
result += '0'
|
||||||
|
num = r
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print get_float_rep(0.72) #('Error 2', '.1011100001010001111010111000010')
|
||||||
|
print get_float_rep(0.1) # ('Error 2', '.0001100110011001100110011001100')
|
||||||
|
print get_float_rep(0.5) #'.1'
|
63
first_edition/ebook_src/builtin_structures/interserction_two_arrays.py
Executable file
63
first_edition/ebook_src/builtin_structures/interserction_two_arrays.py
Executable file
@ -0,0 +1,63 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
def intersection_two_arrays_sets(seq1, seq2):
|
||||||
|
'''
|
||||||
|
>>> intersection_two_arrays_sets([1,2,3,5,7,8], [3,5,6])
|
||||||
|
set([3, 5])
|
||||||
|
>>> intersection_two_arrays_sets([1,2,7,8], [3,5,6])
|
||||||
|
set([])
|
||||||
|
'''
|
||||||
|
# O(n)
|
||||||
|
set1 = set(seq1)
|
||||||
|
set2 = set(seq2)
|
||||||
|
|
||||||
|
return set1.intersection(set2) #same as list(set1 & set2)
|
||||||
|
|
||||||
|
|
||||||
|
def intersection_two_arrays_On2(seq1, seq2):
|
||||||
|
'''
|
||||||
|
>>> intersection_two_arrays_On2([1,2,3,5,7,8], [3,5,6])
|
||||||
|
[3, 5]
|
||||||
|
>>> intersection_two_arrays_On2([1,2,7,8], [3,5,6])
|
||||||
|
[]
|
||||||
|
'''
|
||||||
|
|
||||||
|
final = []
|
||||||
|
|
||||||
|
for i in seq1:
|
||||||
|
for j in seq2:
|
||||||
|
if i == j:
|
||||||
|
final.append(i)
|
||||||
|
|
||||||
|
return final
|
||||||
|
|
||||||
|
|
||||||
|
def intersection_two_arrays_On(seq1, seq2):
|
||||||
|
'''
|
||||||
|
>>> intersection_two_arrays_On([1,2,3,5,7,8], [3,5,6])
|
||||||
|
[5, 3]
|
||||||
|
>>> intersection_two_arrays_On([1,2,7,8], [3,5,6])
|
||||||
|
[]
|
||||||
|
'''
|
||||||
|
|
||||||
|
final = []
|
||||||
|
|
||||||
|
while seq1 and seq2:
|
||||||
|
|
||||||
|
if seq1[-1] == seq2[-1]:
|
||||||
|
final.append(seq1.pop())
|
||||||
|
seq2.pop()
|
||||||
|
elif seq1[-1] > seq2[-1]:
|
||||||
|
seq1.pop()
|
||||||
|
else:
|
||||||
|
seq2.pop()
|
||||||
|
|
||||||
|
return final
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
54
first_edition/ebook_src/builtin_structures/max_subarray_stocks.py
Executable file
54
first_edition/ebook_src/builtin_structures/max_subarray_stocks.py
Executable file
@ -0,0 +1,54 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def beating_stock(array):
|
||||||
|
|
||||||
|
imin = 0
|
||||||
|
|
||||||
|
# first deal is just buying in the next day (1)
|
||||||
|
deal = [array[1] - array[imin], imin, 1]
|
||||||
|
|
||||||
|
for i, d in enumerate(array):
|
||||||
|
|
||||||
|
deal_here = d - array[imin]
|
||||||
|
|
||||||
|
if deal_here > deal[0]:
|
||||||
|
deal = [deal_here, imin, i]
|
||||||
|
|
||||||
|
elif d < array[imin]:
|
||||||
|
imin = i
|
||||||
|
|
||||||
|
return deal[0], array[deal[1]], array[deal[2]]
|
||||||
|
|
||||||
|
|
||||||
|
def beating_stock2(array):
|
||||||
|
|
||||||
|
deal = 0
|
||||||
|
min_value = array[0]
|
||||||
|
|
||||||
|
for i, d in enumerate(array):
|
||||||
|
|
||||||
|
deal_here = d - min_value
|
||||||
|
|
||||||
|
if deal_here > deal:
|
||||||
|
deal = deal_here
|
||||||
|
|
||||||
|
else:
|
||||||
|
if min_value > array[i]:
|
||||||
|
min_value = array[i]
|
||||||
|
|
||||||
|
return deal
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
array = [7, 2, 3, 6, 5, 8, 5, 3, 4]
|
||||||
|
|
||||||
|
deal = beating_stock(array)
|
||||||
|
print("Profit: %d, buying at %d, selling at %d." %(deal[0], deal[1], deal[2]))
|
||||||
|
|
||||||
|
deal = beating_stock2(array)
|
||||||
|
print "Profit: " + str(deal)
|
15
first_edition/ebook_src/builtin_structures/number_of_zeros_factorial.txt
Executable file
15
first_edition/ebook_src/builtin_structures/number_of_zeros_factorial.txt
Executable file
@ -0,0 +1,15 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
How to know how many 0 are in 100!
|
||||||
|
|
||||||
|
You look for the primes that multiply to 10, i.e. 2 and 5.
|
||||||
|
|
||||||
|
There are more 5 than 2s so you can count the fives.
|
||||||
|
|
||||||
|
there is 100/5 = 20, so 20 5s. However, there are two 5s in 25, 50, 75 and 100.
|
||||||
|
|
||||||
|
result: 20+4 = 24
|
33
first_edition/ebook_src/builtin_structures/palindrome.py
Executable file
33
first_edition/ebook_src/builtin_structures/palindrome.py
Executable file
@ -0,0 +1,33 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
|
def is_palindrome(array):
|
||||||
|
'''
|
||||||
|
>>> is_palindrome('subi no onibus')
|
||||||
|
True
|
||||||
|
>>> is_palindrome('helllo there')
|
||||||
|
False
|
||||||
|
>>> is_palindrome('h')
|
||||||
|
True
|
||||||
|
>>> is_palindrome('')
|
||||||
|
True
|
||||||
|
'''
|
||||||
|
array = array.strip(' ')
|
||||||
|
if len(array) < 2:
|
||||||
|
return True
|
||||||
|
|
||||||
|
if array[0] == array[-1]:
|
||||||
|
return is_palindrome(array[1:-1])
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|
70
first_edition/ebook_src/builtin_structures/permutations.py
Executable file
70
first_edition/ebook_src/builtin_structures/permutations.py
Executable file
@ -0,0 +1,70 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def perm(str1):
|
||||||
|
'''
|
||||||
|
>>> perm('123')
|
||||||
|
['123', '132', '231', '213', '312', '321']
|
||||||
|
'''
|
||||||
|
|
||||||
|
if len(str1) < 2:
|
||||||
|
return str1
|
||||||
|
|
||||||
|
res = []
|
||||||
|
for i, c in enumerate(str1):
|
||||||
|
for cc in perm(str1[i+1:] + str1[:i]):
|
||||||
|
res.append(c + cc)
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
def perm2(str1):
|
||||||
|
'''
|
||||||
|
>>> perm2('123')
|
||||||
|
['123', '132', '213', '231', '312', '321']
|
||||||
|
'''
|
||||||
|
from itertools import permutations
|
||||||
|
return [''.join(p) for p in permutations(str1)]
|
||||||
|
|
||||||
|
|
||||||
|
def ispermutation(s1, s2):
|
||||||
|
'''
|
||||||
|
>>> ispermutation('231', '123')
|
||||||
|
True
|
||||||
|
>>> ispermutation('231', '153')
|
||||||
|
False
|
||||||
|
'''
|
||||||
|
|
||||||
|
from collections import Counter
|
||||||
|
aux = Counter()
|
||||||
|
for i in s1:
|
||||||
|
aux[i] += 1
|
||||||
|
for i in s2:
|
||||||
|
aux[i] -= 1
|
||||||
|
for v in aux.values():
|
||||||
|
if v != 0:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def ispermutation2(s1, s2):
|
||||||
|
'''
|
||||||
|
>>> ispermutation2('231', '123')
|
||||||
|
True
|
||||||
|
>>> ispermutation2('231', '153')
|
||||||
|
False
|
||||||
|
'''
|
||||||
|
if sorted(s1) == sorted(s2):
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
37
first_edition/ebook_src/builtin_structures/permutations_alphanumeric.py
Executable file
37
first_edition/ebook_src/builtin_structures/permutations_alphanumeric.py
Executable file
@ -0,0 +1,37 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
''' Write code to generate all possible case permutations of a given
|
||||||
|
lower-cased string
|
||||||
|
'''
|
||||||
|
|
||||||
|
def alpha_permutation(string):
|
||||||
|
'''
|
||||||
|
>>> alpha_permutation('0ab')
|
||||||
|
['0Ab', '0Ab', '0ab', '0ab', '0Ba', '0Ba', '0ba', '0ba', 'ab0', 'a0b', 'a0b', 'b0a', 'b0a', 'ba0']
|
||||||
|
>>> alpha_permutation('')
|
||||||
|
''
|
||||||
|
'''
|
||||||
|
|
||||||
|
if len(string) < 2:
|
||||||
|
return string
|
||||||
|
|
||||||
|
result = []
|
||||||
|
|
||||||
|
for i, c in enumerate(string):
|
||||||
|
rest = string[i+1:] + string[:i]
|
||||||
|
for cc in alpha_permutation(rest):
|
||||||
|
if cc.isalpha():
|
||||||
|
result.append(c.upper() + cc)
|
||||||
|
result.append(c + cc)
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|
47
first_edition/ebook_src/builtin_structures/primes.py
Executable file
47
first_edition/ebook_src/builtin_structures/primes.py
Executable file
@ -0,0 +1,47 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
'''
|
||||||
|
find prime factors of a number.
|
||||||
|
'''
|
||||||
|
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
|
||||||
|
def find_prime_factors(n):
|
||||||
|
'''
|
||||||
|
>>> find_prime_factors(14)
|
||||||
|
[2, 7]
|
||||||
|
>>> find_prime_factors(19)
|
||||||
|
[]
|
||||||
|
'''
|
||||||
|
|
||||||
|
divisors = [d for d in range(2, n//2 + 1) if n % d == 0]
|
||||||
|
primes = [d for d in divisors if is_prime(d)]
|
||||||
|
|
||||||
|
return primes
|
||||||
|
|
||||||
|
|
||||||
|
def is_prime(n):
|
||||||
|
for j in range(2, int(math.sqrt(n))):
|
||||||
|
if (n % j) == 0:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
''' return a n-bit prime '''
|
||||||
|
def generate_prime(number=3):
|
||||||
|
while 1:
|
||||||
|
p = random.randint(pow(2, number-2), pow(2, number-1)-1)
|
||||||
|
p = 2 * p + 1
|
||||||
|
if find_prime_factors(p):
|
||||||
|
return p
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|
28
first_edition/ebook_src/builtin_structures/prod_other_ints.py
Executable file
28
first_edition/ebook_src/builtin_structures/prod_other_ints.py
Executable file
@ -0,0 +1,28 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
def get_products_of_all_except_at_index(array):
|
||||||
|
'''
|
||||||
|
>>> a = [1, 7, 3, 4]
|
||||||
|
>>> get_products_of_all_except_at_index(a)
|
||||||
|
[84, 12, 28, 21]
|
||||||
|
'''
|
||||||
|
total = 1
|
||||||
|
for n in array:
|
||||||
|
total *= n
|
||||||
|
|
||||||
|
new_array = []
|
||||||
|
for n in array:
|
||||||
|
if n is not 0:
|
||||||
|
item = total/n
|
||||||
|
new_array.append(item)
|
||||||
|
else:
|
||||||
|
new_array.append(n)
|
||||||
|
|
||||||
|
return new_array
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
34
first_edition/ebook_src/builtin_structures/ransom_note.py
Executable file
34
first_edition/ebook_src/builtin_structures/ransom_note.py
Executable file
@ -0,0 +1,34 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
from collections import Counter
|
||||||
|
|
||||||
|
def check_if_ransom_note(magazines, note):
|
||||||
|
count = Counter()
|
||||||
|
pm, pn = 0, 0
|
||||||
|
|
||||||
|
while pn < len(note) and pm < len(magazines):
|
||||||
|
char_note = note[pn]
|
||||||
|
if count[char_note]>0:
|
||||||
|
count[char_note] -= 1
|
||||||
|
pn += 1
|
||||||
|
else:
|
||||||
|
char_magazine = magazines[pm]
|
||||||
|
count[char_magazine] += 1
|
||||||
|
pm +=1
|
||||||
|
|
||||||
|
return pn == len(note)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
magazines1 = "avfegthhgrebvkdsvnijnvyijfdmckdsmovkmmfvskumvl;cdkmioswckofjbkreenyukjemjgnmkmvkmnvdkmvkr g gmvdvmldm vldfkmbldkmlvdkm"
|
||||||
|
magazines2 = "adfsfa"
|
||||||
|
note = "you should disobey"
|
||||||
|
|
||||||
|
print(check_if_ransom_note(magazines1, note))
|
||||||
|
print(check_if_ransom_note(magazines2, note))
|
37
first_edition/ebook_src/builtin_structures/reverse_string.py
Executable file
37
first_edition/ebook_src/builtin_structures/reverse_string.py
Executable file
@ -0,0 +1,37 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
def revert(string):
|
||||||
|
'''
|
||||||
|
>>> s = 'hello'
|
||||||
|
>>> revert(s)
|
||||||
|
'olleh'
|
||||||
|
>>> revert('')
|
||||||
|
''
|
||||||
|
'''
|
||||||
|
return string[::-1]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def reverse_string_inplace(s):
|
||||||
|
'''
|
||||||
|
>>> s = 'hello'
|
||||||
|
>>> reverse_string_inplace(s)
|
||||||
|
'olleh'
|
||||||
|
>>> reverse_string_inplace('')
|
||||||
|
''
|
||||||
|
'''
|
||||||
|
if s:
|
||||||
|
s = s[-1] + reverse_string_inplace(s[:-1])
|
||||||
|
return s
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
45
first_edition/ebook_src/builtin_structures/reverse_words.py
Executable file
45
first_edition/ebook_src/builtin_structures/reverse_words.py
Executable file
@ -0,0 +1,45 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def reversing_words(word):
|
||||||
|
"""
|
||||||
|
>>> reversing_words('buffy is awesome')
|
||||||
|
'awesome is buffy'
|
||||||
|
"""
|
||||||
|
new_word = []
|
||||||
|
|
||||||
|
words = word.split(' ')
|
||||||
|
for word in words[::-1]:
|
||||||
|
new_word.append(word)
|
||||||
|
|
||||||
|
return " ".join(new_word)
|
||||||
|
|
||||||
|
|
||||||
|
def reversing_words2(s):
|
||||||
|
"""
|
||||||
|
>>> reversing_words2('buffy is awesome')
|
||||||
|
'awesome is buffy'
|
||||||
|
"""
|
||||||
|
words = s.split()
|
||||||
|
return ' '.join(reversed(words))
|
||||||
|
|
||||||
|
|
||||||
|
def reversing_words3(s):
|
||||||
|
"""
|
||||||
|
>>> reversing_words('buffy is awesome')
|
||||||
|
'awesome is buffy'
|
||||||
|
"""
|
||||||
|
words = s.split(' ')
|
||||||
|
words.reverse()
|
||||||
|
return ' '.join(words)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|
30
first_edition/ebook_src/builtin_structures/rotate_NxN.py
Executable file
30
first_edition/ebook_src/builtin_structures/rotate_NxN.py
Executable file
@ -0,0 +1,30 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
def rotate_NxN(m):
|
||||||
|
n = len(m)
|
||||||
|
for layer in range(n//2):
|
||||||
|
first = layer
|
||||||
|
last = n - 1 - layer
|
||||||
|
for i in range(first, last):
|
||||||
|
offset = i - first
|
||||||
|
top = m[first][i]
|
||||||
|
m[first][i] = m[last-offset][first]
|
||||||
|
m[last-offset][first] = m[last][last-offset]
|
||||||
|
m[last][last-offset] = m[i][last]
|
||||||
|
m[i][last] = top
|
||||||
|
return m
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
m = [[1,2],[3,4]]
|
||||||
|
mr = [[3,1],[4,2]]
|
||||||
|
assert(rotate_NxN(m) == mr)
|
||||||
|
m2 = [[1,2,3],[4,5,6],[7,8,9]]
|
||||||
|
print(rotate_NxN(m2))
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
|
@ -0,0 +1,61 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
'''To use timeit you create a Timer object whose parameters are two Python
|
||||||
|
statements. The first parameter is a Python statement that you want to time;
|
||||||
|
the second parameter is a statement that will run once to set up the test.
|
||||||
|
The timeit module will then time how long it takes to execute the statement
|
||||||
|
some number of times. By default timeit will try to run the statement one
|
||||||
|
million times. When its done it returns the time as a floating point value
|
||||||
|
representing the total number of seconds. However, since it executes the
|
||||||
|
statement a million times you can read the result as the number of
|
||||||
|
microseconds to execute the test one time. You can also pass timeit a
|
||||||
|
named parameter called number that allows you to specify how many times the
|
||||||
|
test statement is executed. The following session shows how long it takes to
|
||||||
|
run each of our test functions 1000 times. '''
|
||||||
|
|
||||||
|
|
||||||
|
import timeit
|
||||||
|
import random
|
||||||
|
|
||||||
|
for i in range(10000,1000001,20000):
|
||||||
|
''' this experiment confirm that the contains operator for lists is O(n) and for dict is O(1) '''
|
||||||
|
t = timeit.Timer("random.randrange(%d) in x"%i, "from __main__ import random,x")
|
||||||
|
x = list(range(i))
|
||||||
|
lst_time = t.timeit(number=1000)
|
||||||
|
x = {j:None for j in range(i)}
|
||||||
|
d_time = t.timeit(number=1000)
|
||||||
|
print("%d,%10.3f,%10.3f" % (i, lst_time, d_time))
|
||||||
|
|
||||||
|
|
||||||
|
""" There results are:
|
||||||
|
10000, 0.192, 0.002
|
||||||
|
30000, 0.600, 0.002
|
||||||
|
50000, 1.000, 0.002
|
||||||
|
70000, 1.348, 0.002
|
||||||
|
90000, 1.755, 0.002
|
||||||
|
110000, 2.194, 0.002
|
||||||
|
130000, 2.635, 0.002
|
||||||
|
150000, 2.951, 0.002
|
||||||
|
170000, 3.405, 0.002
|
||||||
|
190000, 3.743, 0.002
|
||||||
|
210000, 4.142, 0.002
|
||||||
|
230000, 4.577, 0.002
|
||||||
|
250000, 4.797, 0.002
|
||||||
|
270000, 5.371, 0.002
|
||||||
|
290000, 5.690, 0.002
|
||||||
|
310000, 5.977, 0.002
|
||||||
|
|
||||||
|
so we can see the linear tile for lists, and constant for dict!
|
||||||
|
|
||||||
|
Big-O Efficiency of Python Dictionary Operations
|
||||||
|
Operation Big-O Efficiency
|
||||||
|
copy O(n)
|
||||||
|
get item O(1)
|
||||||
|
set item O(1)
|
||||||
|
delete item O(1)
|
||||||
|
contains (in) O(1)
|
||||||
|
iteration O(n)
|
||||||
|
"""
|
||||||
|
|
@ -0,0 +1,66 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
'''To use timeit you create a Timer object whose parameters are two Python statements. The first parameter is a Python statement that you want to time; the second parameter is a statement that will run once to set up the test. The timeit module will then time how long it takes to execute the statement some number of times. By default timeit will try to run the statement one million times. When its done it returns the time as a floating point value representing the total number of seconds. However, since it executes the statement a million times you can read the result as the number of microseconds to execute the test one time. You can also pass timeit a named parameter called number that allows you to specify how many times the test statement is executed. The following session shows how long it takes to run each of our test functions 1000 times. '''
|
||||||
|
|
||||||
|
|
||||||
|
def test1():
|
||||||
|
l = []
|
||||||
|
for i in range(1000):
|
||||||
|
l = l + [i]
|
||||||
|
|
||||||
|
def test2():
|
||||||
|
l = []
|
||||||
|
for i in range(1000):
|
||||||
|
l.append(i)
|
||||||
|
|
||||||
|
def test3():
|
||||||
|
l = [i for i in range(1000)]
|
||||||
|
|
||||||
|
def test4():
|
||||||
|
l = list(range(1000))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import timeit
|
||||||
|
t1 = timeit.Timer("test1()", "from __main__ import test1")
|
||||||
|
print("concat ",t1.timeit(number=1000), "milliseconds")
|
||||||
|
t2 = timeit.Timer("test2()", "from __main__ import test2")
|
||||||
|
print("append ",t2.timeit(number=1000), "milliseconds")
|
||||||
|
t3 = timeit.Timer("test3()", "from __main__ import test3")
|
||||||
|
print("comprehension ",t3.timeit(number=1000), "milliseconds")
|
||||||
|
t4 = timeit.Timer("test4()", "from __main__ import test4")
|
||||||
|
print("list range ",t4.timeit(number=1000), "milliseconds")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
""" The results are:
|
||||||
|
('concat ', 2.366791009902954, 'milliseconds')
|
||||||
|
('append ', 0.16743111610412598, 'milliseconds')
|
||||||
|
('comprehension ', 0.06446194648742676, 'milliseconds')
|
||||||
|
('list range ', 0.021029949188232422, 'milliseconds')
|
||||||
|
|
||||||
|
|
||||||
|
So we see the following pattern for lists:
|
||||||
|
|
||||||
|
Operation Big-O Efficiency
|
||||||
|
index [] O(1)
|
||||||
|
index assignment O(1)
|
||||||
|
append O(1)
|
||||||
|
pop() O(1)
|
||||||
|
pop(i) O(n)
|
||||||
|
insert(i,item) O(n)
|
||||||
|
del operator O(n)
|
||||||
|
iteration O(n)
|
||||||
|
contains (in) O(n)
|
||||||
|
get slice [x:y] O(k)
|
||||||
|
del slice O(n)
|
||||||
|
set slice O(n+k)
|
||||||
|
reverse O(n)
|
||||||
|
concatenate O(k)
|
||||||
|
sort O(n log n)
|
||||||
|
multiply O(nk)
|
||||||
|
"""
|
||||||
|
|
42
first_edition/ebook_src/builtin_structures/search_entry_matrix.py
Executable file
42
first_edition/ebook_src/builtin_structures/search_entry_matrix.py
Executable file
@ -0,0 +1,42 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
''' Search an Entry in a Matrix where the Rows and columns are Sorted
|
||||||
|
In this 2D matrix, every row is increasingly sorted from left to right,
|
||||||
|
and every column is increasingly sorted from top to bottom.
|
||||||
|
The runtime is O(m+n).
|
||||||
|
'''
|
||||||
|
|
||||||
|
def find_elem_matrix_bool(m1, value):
|
||||||
|
|
||||||
|
found = False
|
||||||
|
row = 0
|
||||||
|
col = len(m1[0]) - 1
|
||||||
|
|
||||||
|
while row < len(m1) and col >= 0:
|
||||||
|
|
||||||
|
if m1[row][col] == value:
|
||||||
|
found = True
|
||||||
|
break
|
||||||
|
elif m1[row][col] > value:
|
||||||
|
col-=1
|
||||||
|
else:
|
||||||
|
row+=1
|
||||||
|
|
||||||
|
return found
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def test_find_elem_matrix_bool(module_name='this module'):
|
||||||
|
m1 = [[1,2,8,9], [2,4,9,12], [4,7,10,13], [6,8,11,15]]
|
||||||
|
assert(find_elem_matrix_bool(m1,8) == True)
|
||||||
|
assert(find_elem_matrix_bool(m1,3) == False)
|
||||||
|
m2 = [[0]]
|
||||||
|
assert(find_elem_matrix_bool(m2,0) == True)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
test_find_elem_matrix_bool()
|
||||||
|
|
34
first_edition/ebook_src/builtin_structures/simple_str_comprension.py
Executable file
34
first_edition/ebook_src/builtin_structures/simple_str_comprension.py
Executable file
@ -0,0 +1,34 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
from collections import Counter
|
||||||
|
|
||||||
|
def str_comp(s):
|
||||||
|
'''
|
||||||
|
>>> s1 = 'aabcccccaaa'
|
||||||
|
>>> str_comp(s1)
|
||||||
|
'a2b1c5a3'
|
||||||
|
>>> str_comp('')
|
||||||
|
''
|
||||||
|
'''
|
||||||
|
|
||||||
|
count, last = 1, ''
|
||||||
|
list_aux = []
|
||||||
|
for i, c in enumerate(s):
|
||||||
|
if last == c:
|
||||||
|
count += 1
|
||||||
|
else:
|
||||||
|
if i != 0:
|
||||||
|
list_aux.append(str(count))
|
||||||
|
list_aux.append(c)
|
||||||
|
count = 1
|
||||||
|
last = c
|
||||||
|
list_aux.append(str(count))
|
||||||
|
return ''.join(list_aux)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
41
first_edition/ebook_src/builtin_structures/sum_two_numbers_as_strings.py
Executable file
41
first_edition/ebook_src/builtin_structures/sum_two_numbers_as_strings.py
Executable file
@ -0,0 +1,41 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
'''
|
||||||
|
find the sum of two integers represented as strings,
|
||||||
|
return the sum as string, i.e "123" and "456" would return "579"
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
def get_number(s):
|
||||||
|
count = 1
|
||||||
|
num = 0
|
||||||
|
p = len(s) -1
|
||||||
|
while p >= 0:
|
||||||
|
num = num + int(s[p])*count
|
||||||
|
count *= 10
|
||||||
|
p -= 1
|
||||||
|
return num
|
||||||
|
|
||||||
|
|
||||||
|
def sum_string(s1, s2):
|
||||||
|
'''
|
||||||
|
>>> sum_string('10', '5')
|
||||||
|
'15'
|
||||||
|
>>> sum_string('0', '1')
|
||||||
|
'1'
|
||||||
|
>>> sum_string('123', '456')
|
||||||
|
'579'
|
||||||
|
'''
|
||||||
|
|
||||||
|
n1 = get_number(s1)
|
||||||
|
n2 = get_number(s2)
|
||||||
|
return str(n2 + n1)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|
42
first_edition/ebook_src/dynamic_programming/memo.py
Normal file
42
first_edition/ebook_src/dynamic_programming/memo.py
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
from functools import wraps
|
||||||
|
from do_benchmark import benchmark
|
||||||
|
|
||||||
|
def memo(func):
|
||||||
|
''' an example of dynamic programming using a memoizing decorator '''
|
||||||
|
cache = {}
|
||||||
|
@wraps(func)
|
||||||
|
def wrap(*args):
|
||||||
|
if args not in cache:
|
||||||
|
cache[args] = func(*args)
|
||||||
|
return cache[args]
|
||||||
|
return wrap
|
||||||
|
|
||||||
|
@memo
|
||||||
|
def find_fibonacci_seq_rec(n):
|
||||||
|
''' implements the nth fibonacci value in a recursive exponential runtime '''
|
||||||
|
if n < 2: return n
|
||||||
|
return find_fibonacci_seq_rec(n - 1) + find_fibonacci_seq_rec(n - 2)
|
||||||
|
|
||||||
|
def test_memo():
|
||||||
|
n = 50
|
||||||
|
# find_fibonacci_seq_rec = memo(find_fibonacci_seq_rec)
|
||||||
|
# @benchmark
|
||||||
|
print(find_fibonacci_seq_rec(n))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
test_memo()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,85 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
from itertools import combinations
|
||||||
|
from bisect import bisect
|
||||||
|
from memo import memo
|
||||||
|
from do_benchmark import benchmark
|
||||||
|
|
||||||
|
def naive_longest_inc_subseq(seq):
|
||||||
|
''' naive (exponential) solution to the longest increasing subsequence problem '''
|
||||||
|
for length in range(len(seq), 0, -1):
|
||||||
|
for sub in combinations(seq, length):
|
||||||
|
if list(sub) == sorted(sub):
|
||||||
|
return len(sub)
|
||||||
|
|
||||||
|
|
||||||
|
def longest_inc_subseq1(seq):
|
||||||
|
''' an iterative algorithm for the longest increasing subsequence problem '''
|
||||||
|
end = []
|
||||||
|
for val in seq:
|
||||||
|
idx = bisect(end, val)
|
||||||
|
if idx == len(end): end.append(val)
|
||||||
|
else: end[idx] = val
|
||||||
|
return len(end)
|
||||||
|
|
||||||
|
|
||||||
|
def longest_inc_subseq2(seq):
|
||||||
|
''' another iterative algorithm for the longest increasing subsequence problem '''
|
||||||
|
L = [1] * len(seq)
|
||||||
|
for cur, val in enumerate(seq):
|
||||||
|
for pre in range(cur):
|
||||||
|
if seq[pre] <= val:
|
||||||
|
L[cur] = max(L[cur], 1 + L[pre])
|
||||||
|
return max(L)
|
||||||
|
|
||||||
|
|
||||||
|
def memoized_longest_inc_subseq(seq):
|
||||||
|
''' a memoized recursive solution to find the longest increasing subsequence problem '''
|
||||||
|
@memo
|
||||||
|
def L(cur):
|
||||||
|
res = 1
|
||||||
|
for pre in range(cur):
|
||||||
|
if seq[pre] <= seq[cur]:
|
||||||
|
res = max(res, 1 + L(pre))
|
||||||
|
return res
|
||||||
|
return max(L(i) for i in range(len(seq)))
|
||||||
|
|
||||||
|
|
||||||
|
@benchmark
|
||||||
|
def test_naive_longest_inc_subseq():
|
||||||
|
print(naive_longest_inc_subseq(s1))
|
||||||
|
|
||||||
|
benchmark
|
||||||
|
def test_longest_inc_subseq1():
|
||||||
|
print(longest_inc_subseq1(s1))
|
||||||
|
|
||||||
|
@benchmark
|
||||||
|
def test_longest_inc_subseq2():
|
||||||
|
print(longest_inc_subseq2(s1))
|
||||||
|
|
||||||
|
@benchmark
|
||||||
|
def test_memoized_longest_inc_subseq():
|
||||||
|
print(memoized_longest_inc_subseq(s1))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
from random import randrange
|
||||||
|
s1 = [randrange(100) for i in range(25)]
|
||||||
|
print(s1)
|
||||||
|
test_naive_longest_inc_subseq()
|
||||||
|
test_longest_inc_subseq1()
|
||||||
|
test_longest_inc_subseq2()
|
||||||
|
test_memoized_longest_inc_subseq()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,30 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import shutil
|
||||||
|
|
||||||
|
def change_file_ext():
|
||||||
|
""" read a file and an extension from the command line and produces a copy with its extension changed"""
|
||||||
|
if len(sys.argv) < 2:
|
||||||
|
print("Usage: change_ext.py filename.old_ext 'new_ext'")
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
name = os.path.splitext(sys.argv[1])[0] + "." + sys.argv[2]
|
||||||
|
print (name)
|
||||||
|
|
||||||
|
try:
|
||||||
|
shutil.copyfile(sys.argv[1], name)
|
||||||
|
except OSError as err:
|
||||||
|
print (err)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
change_file_ext()
|
||||||
|
|
||||||
|
|
28
first_edition/ebook_src/manipulating_files/count_unique_words_files.py
Executable file
28
first_edition/ebook_src/manipulating_files/count_unique_words_files.py
Executable file
@ -0,0 +1,28 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
import collections
|
||||||
|
import string
|
||||||
|
import sys
|
||||||
|
|
||||||
|
def count_unique_word_file():
|
||||||
|
if len(sys.argv) < 2:
|
||||||
|
print "Usage: python count_unique_word.py NAMEFILE"
|
||||||
|
|
||||||
|
words = collections.defaultdict(int)
|
||||||
|
strip = string.whitespace + string.punctuation + string.digits + "\"'"
|
||||||
|
for filename in sys.argv[1:]:
|
||||||
|
with open(filename) as file:
|
||||||
|
for line in file:
|
||||||
|
for word in line.lower().split():
|
||||||
|
word = word.strip(strip)
|
||||||
|
if len(word) > 2:
|
||||||
|
words[word] = +1
|
||||||
|
for word in sorted(words):
|
||||||
|
print("'{0}' occurs {1} times.".format(word, words[word]))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
count_unique_word_file()
|
15
first_edition/ebook_src/manipulating_files/count_unique_words_frequency.py
Executable file
15
first_edition/ebook_src/manipulating_files/count_unique_words_frequency.py
Executable file
@ -0,0 +1,15 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
import collections
|
||||||
|
import sys
|
||||||
|
|
||||||
|
def count_unique_word_freq():
|
||||||
|
return collections.Counter(\
|
||||||
|
sys.stdin.read().lower().split()).most_common(n)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
count_unique_word_freq()
|
@ -0,0 +1,27 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
import sys
|
||||||
|
|
||||||
|
def grep_word_from_files():
|
||||||
|
''' using iterator enumerate to create a grep command '''
|
||||||
|
word = sys.argv[1]
|
||||||
|
for filename in sys.argv[2:]:
|
||||||
|
with open(filename) as file:
|
||||||
|
for lino, line in enumerate(file, start=1):
|
||||||
|
if word in line:
|
||||||
|
print("{0}:{1}:{2:.40}".format(filename, lino, line.rstrip()))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
if len(sys.argv) < 2:
|
||||||
|
print("Usage: grep_word_from_files.py word infile1 [infile2...]")
|
||||||
|
sys.exit()
|
||||||
|
else:
|
||||||
|
grep_word_from_files()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,54 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
|
||||||
|
__author__ = "bt3"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
def read_data(filename):
|
||||||
|
lines = []
|
||||||
|
fh = None
|
||||||
|
try:
|
||||||
|
fh = open(filename)
|
||||||
|
for line in fh:
|
||||||
|
if line.strip():
|
||||||
|
lines.append(line)
|
||||||
|
except (IOError, OSError) as err:
|
||||||
|
print(err)
|
||||||
|
finally:
|
||||||
|
if fh is not None:
|
||||||
|
fh.close()
|
||||||
|
return lines
|
||||||
|
|
||||||
|
|
||||||
|
def write_data(lines, filename):
|
||||||
|
fh = None
|
||||||
|
try:
|
||||||
|
fh = open(filename, "w")
|
||||||
|
for line in lines:
|
||||||
|
fh.write(line)
|
||||||
|
except (EnvironmentError) as err:
|
||||||
|
print(err)
|
||||||
|
finally:
|
||||||
|
if fh is not None:
|
||||||
|
fh.close()
|
||||||
|
|
||||||
|
|
||||||
|
def remove_blank_lines():
|
||||||
|
""" read a list of filenames on the command line and for each one produces another file with the same content but with no blank lines """
|
||||||
|
|
||||||
|
if len(sys.argv) < 2:
|
||||||
|
print ("Usage: noblank.py infile1 [infile2...]")
|
||||||
|
|
||||||
|
for filename in sys.argv[1:]:
|
||||||
|
lines = read_data(filename)
|
||||||
|
if lines:
|
||||||
|
write_data(lines, filename)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
remove_blank_lines()
|
||||||
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user