Skip to content

Instantly share code, notes, and snippets.

@270ajay
270ajay / distancesBetweenNodes.py
Created April 2, 2020 09:53
breadth first search of nodes
'''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:
@270ajay
270ajay / fastestRoute.py
Created April 2, 2020 09:54
finding fastest route (Dijkstra algorithm, etc.)
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'''
@270ajay
270ajay / spanningTree.py
Created April 2, 2020 09:55
finding minimum spanning tree (kruskal and prim algorithms)
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
@270ajay
270ajay / stringPatternMatching.py
Created April 14, 2020 09:02
finding where a string is present in text
'''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'''
@270ajay
270ajay / basicModelingWeek1.py
Last active November 12, 2020 12:12
basic constraint programming modeling
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)
@270ajay
270ajay / basicModelingWeek2.py
Last active November 11, 2020 08:35
Basic constraint programming modeling
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()
@270ajay
270ajay / basicModelingWeek3.py
Last active November 19, 2020 05:25
Basic constraint programming modeling
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])
@270ajay
270ajay / basicModelingWeek4.py
Created November 12, 2020 12:15
Basic constraint programming modeling
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]
@270ajay
270ajay / advancedModelingWeek1.py
Created December 21, 2020 07:23
Advanced constraint programming modeling
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"]
@270ajay
270ajay / advancedModelingWeek2.py
Created December 21, 2020 07:24
Advanced constraint programming modeling
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])