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
| # for printing the output | |
| def printSteps(frm, to): | |
| print(f"move from {frm} --> to {to}") | |
| def towerOfHanoi(n, frm, to, spare): | |
| ''' | |
| itype: | |
| n: integer for number of blocks | |
| frm: pile{0,1,2} for from which pile the blocks are |
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 collections import deque | |
| from collections import defaultdict | |
| negativeWeightDirectedGraph = { | |
| "A":{"B":1, "E":2}, | |
| "B":{"C":3}, | |
| "C":{"D":2}, | |
| "D":{"F":2}, | |
| "E":{"F":3, "B":-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
| class Heap(): | |
| sortedHeap = [] | |
| def __init__(self, heap, heapType='max'): | |
| """ | |
| itype: heap: a list for complete binary tree | |
| itype: heapType: max or min for heap type |
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
| def generateNumber(n, base=2): | |
| """ | |
| itype: int : number of digits in a number | |
| rtype: none | |
| It generates every possible n digit number of a base <= 10 | |
| """ | |
| def helper(n, base, prefix=""): | |
| # print(n) |
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
| def lisArray(arr, n): | |
| """ | |
| returns a array of LIS | |
| """ | |
| dp = [[arr[0]]] | |
| for i in range(1, n): | |
| temp = 0 | |
| tempSubArray = [] | |
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
| def pren(l): | |
| # keeping track of duplicates | |
| d = {} | |
| def helper(l, p): | |
| if l==0: | |
| # base case | |
| p = ''.join(p) | |
| if p not in d: | |
| d[p]=1 |
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
| def allPermutations(s): | |
| """ | |
| itype: string | |
| rtype: None | |
| """ | |
| d = {} | |
| def helper(s, p=[]): | |
| if s == []: |
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
| class Node: | |
| def __init__(self, val, *args, **kwargs): | |
| # for creating Node Instances | |
| self.val = val | |
| self.next = None | |
| class LinkedList: | |
| def __init__(self, *args, **kwargs): | |
| self.head = None | |
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 # just for checking result | |
| def mergeSort(arr): | |
| """ | |
| itype: list | |
| rtype: list | |
| """ | |
| def merge(l1, l2): | |
| """ |
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 functools import cmp_to_key | |
| import matplotlib.pyplot as plt | |
| from matplotlib.patches import Polygon | |
| import random | |
| #________ Graham algorithm for convex hull runtime O(n*ln(n)) space O(|H|) ________________ | |
| def convexHull(arr): | |
| """ | |
| itype: list of arr in tuples (x1, x2) |
OlderNewer