Skip to content

Instantly share code, notes, and snippets.

View shivamMg's full-sized avatar

shivam mamgain shivamMg

View GitHub Profile
@shivamMg
shivamMg / min.bashrc
Last active August 2, 2017 12:12
Minimalistic Bashrc
GREEN="\[$(tput setaf 2)\]"
RESET="\[$(tput sgr0)\]"
export PS1="${GREEN}$PS1${RESET}"
alias v="vim"
alias ls="ls --color=auto"
alias ll="ls -lah"
alias grep="grep --color=auto"
ainit() { source "$1"venv/bin/activate; }
@shivamMg
shivamMg / git-cheat-list.md
Created February 17, 2017 18:26
Git cheat list

Git cheat list

  • name of the current banch

    git rev-parse --abbrev-ref HEAD
    
  • all commits that your branch have that are not yet in master

    git log master..<HERE_COMES_YOUR_BRANCH_NAME>
    
@shivamMg
shivamMg / git.sh
Created March 25, 2017 06:20
Understanding git merge --no-ff
git init
echo a > a.txt
git add a.txt && git commit -m "a"
git checkout -b feature-b
echo b > b.txt
git add b.txt && git commit -m "b"
git checkout master
# ($)
# Python
def counter(x):
def f():
nonlocal x
x += 1
return x
return f
@shivamMg
shivamMg / s3_copy.py
Last active August 20, 2017 08:18
Copy objects from Source S3 Bucket to Destination S3 Bucket (Prefixes are used to filter which objects must be copied where)
import boto3
MAX_KEYS = 10
PAGE_SIZE_COUNT = 10
def s3_copy(src_bucket, src_prefix, dest_bucket, dest_prefix):
"""
Copies objects from Source Bucket Prefix to Destination Bucket Prefix.
s3://srcbucket/srcprefix -> s3://destbucket/destprefix
@shivamMg
shivamMg / attach_branch.py
Created August 31, 2017 21:53
Given a list of prefixes create a tree structure. (sort of how objects are stored in AWS S3. Another example could be Consul)
import json
def attach_branch(tree, branch):
l = len(branch)
node = branch[0]
# If node doesn't exist get an empty subtree
subtree = tree.get(node, {})
if l == 1:
@shivamMg
shivamMg / factory.py
Last active March 6, 2022 18:43
Design patterns
from abc import ABC, abstractmethod
RED = 'RED'
YELLOW = 'YELLOW'
GREEN = 'GREEN'
class Light(ABC):
@property
@abstractmethod
@shivamMg
shivamMg / arithmetic-progression.py
Last active April 20, 2018 11:49
Arithmetic Progression with a Closure, and a Generator
def arith(a: 'initial term', d: 'common difference'):
"""Returns initial and subsequent arithmetic progression terms"""
current = a - d
def _():
nonlocal current
current += d
return current
return _
@shivamMg
shivamMg / min-heap.py
Last active April 1, 2022 04:43
Data Structures
class MinHeap:
"""A min-heap using an array"""
# storing tree in level-order
arr = []
@staticmethod
def parent(i): return (i - 1) // 2
@staticmethod
def children(i): return (2*i + 1, 2*i + 2)
def insert(self, a):
@shivamMg
shivamMg / counting-sort.py
Last active April 20, 2018 13:40
Sorting Algorithms
# Total possible values in array
# Array can only be sorted if it's in range [0, K)
K = 10
def counting_sort(arr):
counter = [0 for _ in range(K)]
for a in arr:
counter[a] += 1