Skip to content

Instantly share code, notes, and snippets.

View siddhantkushwaha's full-sized avatar
🏠
Working from home

Siddhant Kushwaha siddhantkushwaha

🏠
Working from home
  • India
View GitHub Profile
@siddhantkushwaha
siddhantkushwaha / custom_crawl.py
Created June 18, 2019 06:53
This gist contains code snippet for allowing a scrapy spider to be called via a function and return the data.
from scrapy.crawler import CrawlerProcess
from scrapy_tasks.spiders.trial import *
class CustomCrawler:
def __init__(self):
self.output = None
self.process = CrawlerProcess(settings={'LOG_ENABLED': False})
@siddhantkushwaha
siddhantkushwaha / remove_lock.sh
Created July 10, 2019 06:55
Handles the following error : Unable to lock the administration directory (/var/lib/dpkg/) is another process using it?
sudo rm /var/lib/apt/lists/lock
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock
@siddhantkushwaha
siddhantkushwaha / nqueens.py
Created August 4, 2019 05:04
N-Queens: backtrack to print all solutions
def isSafe(board, row, col):
for i in range(col):
if board[row][i] == 'Q':
return False
i = row
j = col
while i >= 0 and j >= 0:
if board[i][j] == 'Q':
@siddhantkushwaha
siddhantkushwaha / parantheses.py
Created August 4, 2019 05:21
Generate valid parantheses strings of length 2*n.
def solve(n, o, c, res):
if len(res) == 2*n:
yield res
if o > 0:
yield from solve(n, o-1, c, res+'(')
# derived from c > 0 and n-o > n-c
if c > o:
yield from solve(n, o, c-1, res+')')
@siddhantkushwaha
siddhantkushwaha / lazy_prop.cpp
Last active October 19, 2019 20:03
Segment Tree - range-query and point-update, range update (lzp)
/*
Bitmasks are cool. A bitmask is a string of binary bits (0s and 1s). For example: "01010" is a bitmask.
Kuldeep is a naughty but brilliant computer scientist. In his free time, he writes some random programs
to play with bitmasks. He has a PhD student under him and to test him (and entertain himself),
he has given him the following task: Given a number N, write a bitmask of length N containing all 0s.
Now, you are given Q operations. Each operation contains two numbers (l, r) as input.
An operation can be one of the following:
Update operation: Take the XOR of all the bits in the bitmask from index l to r (both inclusive) with 1.
@siddhantkushwaha
siddhantkushwaha / partition.cpp
Last active August 14, 2019 16:05
Most partitioning implementations depend on where the pivot is, but not this one.
#include "bits/stdc++.h"
using namespace std;
int partition(int a[], int low, int high) {
/* try with different values of p in range [low, high] */
int p = (low + high) / 2;
int i = low;
int j = high;
while (1) {
@siddhantkushwaha
siddhantkushwaha / subsequence.cpp
Last active September 3, 2019 14:37
Longest palindromic substring. - Dynamic Programming O(n^2) time and O(n) space.
int longestPalindromeSubsequence(string A) {
int n = A.length();
int dp[3][n];
int res = 1;
for (int l = 1; l <= n; l++) {
int idx = (l - 1) % 3;
for (int left = 1; left <= n - l + 1; left++) {
int right = left + l - 1;
@siddhantkushwaha
siddhantkushwaha / check_duplicates.cpp
Last active October 17, 2019 19:11
Some tricky tree implementations.
/*
Hash each subtree recursively and check if that hash already exists.
*/
string check_dup(node *root, set<string> &subtrees, int &duplicate_exists) {
if (root == NULL)
return "#";
string hash_left = check_dup(root->left, subtrees, duplicate_exists);
string hash_right = check_dup(root->right, subtrees, duplicate_exists);
@siddhantkushwaha
siddhantkushwaha / bipartite.cpp
Created October 17, 2019 18:30
Graph implementations
typedef vector<vector<int>> graph;
graph create(int n, vector<vector<int>> &edges) {
graph adj(n, vector<int>(0));
for(auto e : edges) {
adj[e[0]].push_back(e[1]);
adj[e[1]].push_back(e[0]);
}
return adj;
}
@siddhantkushwaha
siddhantkushwaha / DSU.cpp
Last active October 20, 2019 19:43
Disjoint set union with path compression.
class DSU {
private:
vector<int> arr;
vector<int> size;
public:
DSU(int n) {
arr.resize(n);
for (int i = 0; i < n; i++) arr[i] = i;
size.resize(n, 1);