Skip to content

Instantly share code, notes, and snippets.

View abecus's full-sized avatar
🕹️
Curious

Abdul Salam abecus

🕹️
Curious
View GitHub Profile
@abecus
abecus / TOH.py
Last active August 9, 2019 17:49
simple recurrsion for tower of Hanoi
# 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
@abecus
abecus / sortestPath.py
Last active August 9, 2019 17:50
simple bellman-ford algorithm sustituted with bfs having same runtime complexity O(|V|*|E|)
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},
@abecus
abecus / heap.py
Created August 6, 2019 05:33
Simple Heap Implementation in python using list programming
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
@abecus
abecus / generateNumbers.py
Created August 9, 2019 17:41
Generates every possible "n" digit number in "base"<=10
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)
@abecus
abecus / LIS.py
Created August 9, 2019 17:55
longest Increasing Subsequence in python
def lisArray(arr, n):
"""
returns a array of LIS
"""
dp = [[arr[0]]]
for i in range(1, n):
temp = 0
tempSubArray = []
@abecus
abecus / allValidPrenthsis.py
Created August 11, 2019 08:11
Prints all valid Prenthsis of given number of pairs
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
@abecus
abecus / permutations.py
Created August 11, 2019 08:14
prints all possible unique permutations of a list
def allPermutations(s):
"""
itype: string
rtype: None
"""
d = {}
def helper(s, p=[]):
if s == []:
@abecus
abecus / SinglyLinkedList.py
Created August 14, 2019 19:11
simple Implementation of singly Linked List
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
@abecus
abecus / mergeSort.py
Created August 16, 2019 19:12
A simple Merge Sort Implementation in python
import random # just for checking result
def mergeSort(arr):
"""
itype: list
rtype: list
"""
def merge(l1, l2):
"""
@abecus
abecus / convexHull.py
Created August 16, 2019 19:15
a simple Gift Rapping ( convex hull Garaham's Algorithm ) algorithm in python. It returns image of points with bound or a patch of convex hull on them.
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)