Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
keon committed Nov 22, 2016
1 parent eb09d25 commit 6f33534
Show file tree
Hide file tree
Showing 16 changed files with 324 additions and 109 deletions.
88 changes: 0 additions & 88 deletions algorithms/sorting.py

This file was deleted.

8 changes: 8 additions & 0 deletions linkedlist/firstCyclicNode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# find the first node of a cycle in the linked list.

# 1 -> 2 -> 3 -> 4 -> 5 -> 1 => 1
# A -> B -> C -> D -> E -> C => C

def firstCyclicNode():
pass

4 changes: 2 additions & 2 deletions linkedlist/linkedlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
# In contrast, arrays have constant time operations to access
# elements in an array.

class DoublyLinkedList(object):
class DoublyLinkedListNode(object):
def __init__(self, value):
self.value = value
self.next = None
self.prev = None


class SinglyLinkedList(object):
class SinglyLinkedListNode(object):
def __init__(self, value):
self.value = value
self.next = None
44 changes: 44 additions & 0 deletions linkedlist/printKthToLast.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Node():
def __init__(self, val = None):
self.val = val
self.next = None

def printKthToLast(head):
"""
Time Complexity: O()
Space Complexity: O()
"""
pass

def printLinkedList(head):
string = ""
while head.next:
string += head.val + " -> "
head = head.next
string += head.val
print(string)

# A A B C D C F G

a1 = Node("A")
a2 = Node("A")
b = Node("B")
c1 = Node("C")
d = Node("D")
c2 = Node("C")
f = Node("F")
g = Node("G")

a1.next = a2
a2.next = b
b.next = c1
c1.next = d
d.next = c2
c2.next = f
f.next = g

# removeDups(a1)
# printLinkedList(a1)
# removeDupsWithoutSet(a1)
# printLinkedList(a1)

67 changes: 67 additions & 0 deletions linkedlist/removeDups.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
class Node():
def __init__(self, val = None):
self.val = val
self.next = None

def removeDups(head):
"""
Time Complexity: O(N)
Space Complexity: O(N)
"""
hashset = set()
prev = Node()
while head:
if head.val in hashset:
prev.next = head.next
else:
hashset.add(head.val)
prev = head
head = head.next

def removeDupsWithoutSet(head):
"""
Time Complexity: O(N^2)
Space Complexity: O(1)
"""
current = head
while current:
runner = current
while runner.next:
if runner.next.val == current.val:
runner.next = runner.next.next
else:
runner = runner.next
current = current.next

def printLinkedList(head):
string = ""
while head.next:
string += head.val + " -> "
head = head.next
string += head.val
print(string)

# A A B C D C F G

a1 = Node("A")
a2 = Node("A")
b = Node("B")
c1 = Node("C")
d = Node("D")
c2 = Node("C")
f = Node("F")
g = Node("G")

a1.next = a2
a2.next = b
b.next = c1
c1.next = d
d.next = c2
c2.next = f
f.next = g

removeDups(a1)
printLinkedList(a1)
removeDupsWithoutSet(a1)
printLinkedList(a1)

6 changes: 3 additions & 3 deletions queue/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ def expand(self):
expands size of the array.
Time Complexity: O(n)
"""
newArray = [None] * len(self.array) * 2 # double the size of the array
new_array = [None] * len(self.array) * 2 # double the size of the array
for i, element in enumerate(self.array):
newArray[i] = element
self.array = newArray
new_array[i] = element
self.array = new_array

def __iter__(self):
probe = self.top - 1
Expand Down
10 changes: 5 additions & 5 deletions search/binarySearch.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def binarySearch(array, query):
def binary_search(array, query):
lo, hi = 0, len(array) - 1
while lo <= hi:
print("--------")
Expand All @@ -18,11 +18,11 @@ def binarySearch(array, query):
array = [1,2,3,3,3,3,4,4,4,4,5,6]
print(array)
print("-----SEARCH-----")
print("found: ", 5, " in index:" , binarySearch(array, 5))
print("found: ", 5, " in index:" , binary_search(array, 5))
print("-----SEARCH-----")
print("found: ", 6, " in index:" , binarySearch(array, 6))
print("found: ", 6, " in index:" , binary_search(array, 6))
print("-----SEARCH-----")
print("found: ", 7, " in index:" , binarySearch(array, 7))
print("found: ", 7, " in index:" , binary_search(array, 7))
print("-----SEARCH-----")
print("found: ", -1, " in index:" , binarySearch(array, -1))
print("found: ", -1, " in index:" , binary_search(array, -1))
print("-----SEARCH-----")
22 changes: 11 additions & 11 deletions search/countElem.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
def countElem(array, query):
def firstOccurance(array, query):
def count_elem(array, query):
def first_occurance(array, query):
lo, hi = 0, len(array) -1
while lo <= hi:
mid = lo + (hi - lo) // 2
Expand All @@ -10,7 +10,7 @@ def firstOccurance(array, query):
lo = mid + 1
else:
hi = mid - 1
def lastOccurance(array, query):
def last_occurance(array, query):
lo, hi = 0, len(array) -1
while lo <= hi:
mid = lo + (hi - lo) // 2
Expand All @@ -22,8 +22,8 @@ def lastOccurance(array, query):
else:
hi = mid - 1

first = firstOccurance(array, query)
last = lastOccurance(array, query)
first = first_occurance(array, query)
last = last_occurance(array, query)
if first is None or last is None:
return None
return last - first + 1
Expand All @@ -34,19 +34,19 @@ def lastOccurance(array, query):
print(array)
print("-----COUNT-----")
query = 3
print("count: ", query, " :" , countElem(array, query))
print("count: ", query, " :" , count_elem(array, query))
print("-----COUNT-----")
query = 5
print("count: ", query, " :" , countElem(array, query))
print("count: ", query, " :" , count_elem(array, query))
print("-----COUNT-----")
query = 7
print("count: ", query, " :" , countElem(array, query))
print("count: ", query, " :" , count_elem(array, query))
print("-----COUNT-----")
query = 1
print("count: ", query, " :" , countElem(array, query))
print("count: ", query, " :" , count_elem(array, query))
print("-----COUNT-----")
query = -1
print("count: ", query, " :" , countElem(array, query))
print("count: ", query, " :" , count_elem(array, query))
print("-----COUNT-----")
query = 9
print("count: ", query, " :" , countElem(array, query))
print("count: ", query, " :" , count_elem(array, query))
14 changes: 14 additions & 0 deletions sorting/insertion_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def insertion_sort(arr):
""" Insertion Sort
Complexity: O(n^2)
"""
for i in xrange(len(arr)):
cursor = arr[i]
pos = i
while pos > 0 and arr[pos-1] > cursor:
# Swap the number down the list
arr[pos] = arr[pos-1]
pos = pos-1
# Break and do the final swap
arr[pos] = cursor
return arr
38 changes: 38 additions & 0 deletions sorting/merge_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@


def merge_sort(arr):
""" Merge Sort
Complexity: O(n log(n))
"""
size = len(arr)
half = size/2
# Our recursive base case
if size <= 1:
return arr
# Perform merge_sort recursively on both halves
left, right = merge_sort(arr[half:]), merge_sort(arr[:half])

# Merge each side together
return merge(left, right)

def merge(left, right):
""" Merge helper
Complexity: O(n)
"""
arr = []
left_cursor, right_cursor = 0,0
while left_cursor < len(left) and right_cursor < len(right):
# Sort each one and place into the result
if left[left_cursor] <= right[right_cursor]:
arr.append(left[left_cursor])
left_cursor+=1
else:
arr.append(right[right_cursor])
right_cursor+=1
# Add the left overs if there's any left to the result
if left:
arr.extend(left[left_cursor:])
if right:
arr.extend(right[right_cursor:])
return arr

25 changes: 25 additions & 0 deletions sorting/quick_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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))
"""
if first < last:
pos = partition(arr, first, last)
# Start our two recursive calls
quick_sort(arr, first, pos-1)
quick_sort(arr, pos+1, last)

def partition(arr, first, last):
pivot = 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

print(array)
print(quick_sort(array, 0, len(array)-1))
print(array)
Loading

0 comments on commit 6f33534

Please sign in to comment.