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''' | |
class Node: | |
def __init__(self, key, next=None): | |
self.key = key | |
self.next = next | |
class SinglyLinkedList: |
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 heapq | |
'''course: https://www.coursera.org/learn/algorithms-on-graphs#about''' | |
class PriorityQueue(): | |
'''credits: Eugene Yarmash | |
https://stackoverflow.com/questions/46636656/python-heapq-replace-priority''' |
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 heapq | |
'''course: https://www.coursera.org/learn/algorithms-on-graphs#about''' | |
class DisjointSets: | |
'''Finds quickly if two points belong to same set''' | |
def __init__(self, totalNumOfSets): | |
self.parent = [None] * totalNumOfSets | |
self.rank = [None] * totalNumOfSets |
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
'''https://www.coursera.org/learn/algorithms-on-strings''' | |
def computePrefixFunction(pattern): | |
'''returns a list containing longest border for each char from the first character in the pattern. | |
for example, for string abcab, it would return [0, 0, 0, 1, 2] | |
Border of a string: prefix of the string == suffix of the string | |
Example: ab is the border of string abcbab since ab is the prefix as well as suffix of that string''' |
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
from ortools.sat.python import cp_model | |
''' course: https://www.coursera.org/learn/basic-modeling#about ''' | |
def maximizePowerOfArmyWithinBudget(costList, strengthList, lbUbTupleList, nameList, budget): | |
model = cp_model.CpModel() | |
numOfGroupsOfArmy = len(costList) |
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
from ortools.sat.python import cp_model | |
''' course: https://www.coursera.org/learn/basic-modeling#about ''' | |
def yellowTurbanModel(movesList, timeBound, powerList, durationList): | |
model = cp_model.CpModel() |
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
from ortools.sat.python import cp_model | |
''' course: https://www.coursera.org/learn/basic-modeling#about ''' | |
def luBuAssignmentProblem(heroList, spotList, damageList): | |
model = cp_model.CpModel() | |
numOfSpots = len(spotList) | |
numOfRows = len(damageList) | |
numOfCols = len(damageList[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
from ortools.sat.python import cp_model | |
''' course: https://www.coursera.org/learn/basic-modeling#about ''' | |
def cookingWineProblem(): | |
foodList = ["CHILIFISHHEAD", "MAPOTOFU", "SNAKESOUP", "GONGBAOFROG"] | |
foodDict = {"CHILIFISHHEAD": 0, "MAPOTOFU": 1, "SNAKESOUP": 2, "GONGBAOFROG": 3} | |
tasteList = [5, 2, 4, 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
from ortools.sat.python import cp_model | |
''' course: https://www.coursera.org/learn/advanced-modeling#about ''' | |
def theCavalryWedgeProblem(): | |
model = cp_model.CpModel() | |
horseList = ["H1", "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "H10"] | |
riderList = ["R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11"] |
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
from ortools.sat.python import cp_model | |
''' course: https://www.coursera.org/learn/advanced-modeling#about ''' | |
def formTeam(model, booleanVarList, archerList, cavalryList, infantryList): | |
varList = [] | |
for archer in archerList: | |
varList.append(booleanVarList[archer]) |