Skip to content

Commit

Permalink
update more algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
keon committed Nov 27, 2016
1 parent 6f33534 commit 9980417
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 12 deletions.
23 changes: 23 additions & 0 deletions array/longest_increasing_subsequence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@


def longest_increasing_subsequence(sequence):
"""
Dynamic Programming Algorithm for
counting the length of longest increasing subsequence
type sequence: List[int]
"""
length = len(sequence)
counts = [1 for _ in range(length)]
for i in range(1, length):
for j in range(0, i):
if sequence[i] > sequence[j]:
counts[i] = max(counts[i], counts[j] + 1)
print(counts)
return max(counts)


sequence = [1, 101, 10, 2, 3, 100, 4, 6, 2]
print("sequence: ", sequence)
print("output: ", longest_increasing_subsequence(sequence))
print("answer: ", 5)

File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 4 additions & 2 deletions sorting/merge_sort.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


def merge_sort(arr):
""" Merge Sort
Complexity: O(n log(n))
Expand Down Expand Up @@ -36,3 +34,7 @@ def merge(left, right):
arr.extend(right[right_cursor:])
return arr

array = [1,5, 7,4,3,2,1,9,0,10,43,64]
print(array)
print(merge_sort(array, 0, len(array)-1))
print(array)
18 changes: 8 additions & 10 deletions sorting/quick_sort.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
array = [1,5, 7,4,3,2,1,9,0,10,43,64]


def quick_sort(arr, first, last):
""" Quicksort
Complexity: O(n log(n))
Complexity: best O(n) avg O(n log(n)), worst O(N^2)
"""
if first < last:
pos = partition(arr, first, last)
Expand All @@ -12,14 +9,15 @@ def quick_sort(arr, first, last):
quick_sort(arr, pos+1, last)

def partition(arr, first, last):
pivot = first
wall = first
for pos in xrange(first, last):
if arr[pos] < arr[last]:
arr[pos], arr[pivot] = arr[pivot], arr[pos]
pivot += 1
arr[pivot], arr[last] = arr[last], arr[pivot]
return pivot
if arr[pos] < arr[last]: # last is the pivot
arr[pos], arr[wall] = arr[wall], arr[pos]
wall += 1
arr[wall], arr[last] = arr[last], arr[wall]
return wall

array = [1,5, 7,4,3,2,1,9,0,10,43,64]
print(array)
print(quick_sort(array, 0, len(array)-1))
print(array)
19 changes: 19 additions & 0 deletions string/license_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

def license_number(key, K):
res, alnum = [], []
for char in key:
if char != "-":
alnum.append(char)
for i, char in enumerate(reversed(alnum)):
res.append(char)
if (i+1) % K == 0 and i != len(alnum)-1:
res.append("-")
return "".join(res[::-1])


print(license_number("a-bc-dfd-df", 1), 1)
print(license_number("a-bc-dfd-df", 2), 2)
print(license_number("a-bc-dfd-df", 3), 3)
print(license_number("a-bc-dfd-df", 4), 4)
print(license_number("a-bc-dfd-df", 5), 5)

File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions string/reverseWords.py → string/reverse_words.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ def reverse(array):
def reverseWords(array):
reverse(array)


49 changes: 49 additions & 0 deletions tree/bintree2list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class Node():
def __init__(self, val = 0):
self.val = val
self.left = None
self.right = None

def bintree2list(root):
"""
type root: root class
"""
if not root:
return root
root = bintree2list_util(root)
while root.left:
root = root.left
return root

def bintree2list_util(root):
if not root:
return root
if root.left:
left = bintree2list_util(root.left)
while left.right:
left = left.right
left.right = root
root.left = left
if root.right:
right = bintree2list_util(root.right)
while right.left:
right = right.left
right.left = root
root.right = right
return root

def print_tree(root):
while root:
print(root.val)
root = root.right

tree = Node(10)
tree.left = Node(12)
tree.right = Node(15)
tree.left.left = Node(25)
tree.left.left.right = Node(100)
tree.left.right = Node(30)
tree.right.left = Node(36)

head = bintree2list(tree)
print_tree(head)
File renamed without changes.

0 comments on commit 9980417

Please sign in to comment.