This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Course: https://www.coursera.org/learn/algorithmic-toolbox#about''' | |
def getLargestNumber(numberList): | |
'''given a sequence of digits, | |
this function prints out the largest number that consists of those digits''' | |
print("\nsequence of digits given: ", numberList) | |
largestNumList = [] | |
while numberList: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
'''Course: https://www.coursera.org/learn/algorithmic-toolbox#about''' | |
def linearSearchRecursive(searchList, minIndex, maxIndex, key): | |
'''not the fastest approach to return index of element in list''' | |
if maxIndex < minIndex: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Course: https://www.coursera.org/learn/algorithmic-toolbox#about''' | |
def changeMoneyGreedy(moneyValue, demoninationsList): | |
'''fast approach but doesnt give optimal result in all cases''' | |
minNumOfDenominations = 0 | |
while moneyValue > 0: | |
largestDenomination = 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Course: https://www.coursera.org/learn/algorithmic-toolbox#about''' | |
def knapsackWithRepetitions(maxCapacity, weightsList, valuesList): | |
'''returns optimal solution. Maximize total value of items that can fit. | |
Knapsack can contain more than 1 quantity of each item''' | |
knapsackValList = [None] * (maxCapacity+1) | |
knapsackValList[0] = 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Course: https://www.coursera.org/learn/data-structures#about''' | |
class Node: | |
def __init__(self, key, next=None): | |
self.key = key | |
self.next = next | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Course: https://www.coursera.org/learn/data-structures#about''' | |
class DynamicArray: | |
'''How python list works. No restriction on size''' | |
def __init__(self): | |
self.array = [None] * 2 | |
self.size = 0 | |
self.capacity = 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Course: https://www.coursera.org/learn/data-structures#about''' | |
class BinaryHeap: | |
'''Priority Queue. Insert values to this data structure and get max value stored quickly''' | |
defaultArraySize = 10 | |
def __init__(self, size=None): | |
self.maxSize = size if size != None else BinaryHeap.defaultArraySize |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
'''Course: https://www.coursera.org/learn/data-structures#about''' | |
def isPrime(num): | |
'''src: https://codereview.stackexchange.com/questions/71212/find-smallest-prime-number-greater-than-given-n''' | |
if num%2 == 0 or num%3 == 0: | |
return False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Course: https://www.coursera.org/learn/data-structures#about''' | |
class Node: | |
def __init__(self, key, parent=None, leftChild=None, rightChild=None): | |
self.key = key | |
self.parent = parent | |
self.leftChild = leftChild | |
self.rightChild = rightChild |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''course: https://www.coursera.org/learn/algorithms-on-graphs#about''' | |
def adjacencyMatrix(): | |
'''Edges: (A,B), (A,C), (A,D), (C,D), (E,F) | |
check if edge exists: O(1) | |
list all edges: O(numOfNodes^2) | |
list all neighbors: O(numOfNodes)''' | |
adjMatrix = {'A': {'A': 0, 'B': 1, 'C': 1, 'D': 1, 'E': 0, 'F': 0}, | |
'B': {'A': 1, 'B': 0, 'C': 0, 'D': 0, 'E': 0, 'F': 0}, |