Skip to content

Instantly share code, notes, and snippets.

@piyush01123
piyush01123 / quickSelect.cpp
Last active February 17, 2023 19:02
QuickSelect implementation
#include <bits/stdc++.h>
using namespace std;
int randGen (int i) {srand(time(0)); return rand()%i;}
int partition(int A[], int lo, int hi)
{
int pivot = A[hi];
int i = lo-1;
for (int j=lo; j<=hi; j++)
@piyush01123
piyush01123 / quickSort.cpp
Last active February 17, 2023 19:01
QuickSort implementation
#include <bits/stdc++.h>
using namespace std;
int randGen (int i) {srand(time(0)); return rand()%i;}
int ctr = 0;
int partition(int A[], int lo, int hi)
{
int pivot = A[hi];
int i = lo-1;
@piyush01123
piyush01123 / autoWayBack.js
Created February 15, 2023 18:29
Auto WayBack - Load latest snapshot from Wayback Machine
// ==UserScript==
// @name Auto Wayback
// @namespace http://tampermonkey.net/
// @version 3.1.1
// @description Load latest snapshot from Wayback Machine
// @author Piyush Singh
// @match *://*/*
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant unsafeWindow
// ==/UserScript==
@piyush01123
piyush01123 / generatePassword.js
Last active March 21, 2023 23:14
Generate and copy to clipboard strong password - Tampermonkey script
// ==UserScript==
// @name Generate Strong Password
// @namespace http://tampermonkey.net/
// @version 3.1.1
// @description Strong password generator of size 12
// @author Piyush Singh
// @match *://*/*
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant unsafeWindow
// @grant GM_setClipboard
@piyush01123
piyush01123 / cli_productivity.md
Last active December 2, 2022 11:15
Command Line Productivity Tools

Command Line Productivity Tools

In course of my everyday work, sometimes I need a tool which does not exist. So I build them myself and later release them. Here are 3 command line tools I have built that each serve some specific purpose:

Tool 1: Flask Image Gallery

Generates image gallery from your server. Very useful during ML experimentation. Link to repository.

@piyush01123
piyush01123 / track_visitors.py
Last active November 18, 2022 10:26
Simple flask app to track visitors. Can also be used to test if your proxy server or vpn is running properly.
from flask import Flask, request
from datetime import datetime
import json,os
app = Flask(__name__)
@app.route('/', methods=["GET","POST"])
def index():
if request.method=="POST":
ip_data = request.get_json()
print(ip_data)
@piyush01123
piyush01123 / fisr.cpp
Last active March 16, 2023 01:36
Fast Inverse Square Root - Quake III
float fisr(float n){
long x = 0x5f3759df - (*(long *)&n >> 1); //magic number = 1.5*2^23*(127-mu); mu represents log(1+x)=x+mu; mu=0.0450465
float y = *(float *)&x; // Solution from bit manipulation
y = 1.5F*y - 0.5F*n*y*y*y; // Newton improvement 1
y = 1.5F*y - 0.5F*n*y*y*y; // Newton improvement 2
return y;
}
@piyush01123
piyush01123 / viterbi.py
Last active March 16, 2023 01:36
Hidden Markov Models - Viterbi Algorithm
import numpy as np
def viterbi(mood_sequence, priors, transmission_probs, emission_probs):
n = len(mood_sequence)
weather_matrix = np.zeros((n, 2))
history = [(None,None)]
for i, mood in enumerate(mood_sequence):
if i==0:
weather_matrix[i] = priors['s']*emission_probs['s'+mood], priors['r']*emission_probs['r'+mood]
else:
@piyush01123
piyush01123 / tfidf.py
Last active March 31, 2021 03:32
TF-IDF implementation
from sklearn.feature_extraction.text import TfidfTransformer
import numpy as np
counts = [[3, 0, 1],
[2, 0, 0],
[3, 0, 0],
[4, 0, 0],
[3, 2, 0],
[3, 0, 2]]
@piyush01123
piyush01123 / kmeans.py
Last active November 24, 2020 22:02
K Means clustering
import numpy as np, matplotlib.pyplot as plt
def KMeans(X, K, num_steps=100):
L = len(X)
cluster_assignments=np.concatenate([np.full((L//K,), k) for k in range(K-1)]+[np.full((L-(K-1)*(L//K),), K-1)])
centroids = np.empty((K,*X.shape[1:]))
for step in range(num_steps):
# E step: centroid calculation
for k in range(K):
centroids[k] = X[cluster_assignments==k].mean(axis=0)