Skip to content

Instantly share code, notes, and snippets.

View AdityaSoni19031997's full-sized avatar
🎯
Focusing

Aditya Soni AdityaSoni19031997

🎯
Focusing
View GitHub Profile
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import random
from itertools import chain, cycle, islice
import torch.utils.data as data
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import time
import torch
import numpy as np
@AdityaSoni19031997
AdityaSoni19031997 / redis.py
Created April 18, 2021 09:39 — forked from hanneshapke/redis.py
Load word vectors into a redis db
import bz2
import pickle
from django.conf import settings
from djang_redis import get_redis_connection
from tqdm import tqdm
from .constants import GOOGLE_WORD2VEC_MODEL_NAME
@AdityaSoni19031997
AdityaSoni19031997 / NOTES.md
Created July 14, 2021 07:03 — forked from wvengen/NOTES.md
Semantic web of food notes
@AdityaSoni19031997
AdityaSoni19031997 / gist:0916ddf3d0918d4980eac13d3411fbfa
Created July 23, 2021 18:03 — forked from Nicksil/gist:5646003
Python - Knuth-Morris-Pratt (KMP) string-matching algorithm
# File: KnuthMorrisPratt.py
# Author: Keith Schwarz (htiek@cs.stanford.edu)
#
# An implementation of the Knuth-Morris-Pratt (KMP) string-matching algorithm.
# This algorithm takes as input a pattern string P and target string T, then
# finds the first occurrence of the string T in the pattern P, doing so in time
# O(|P| + |T|). The logic behind the algorithm is not particularly complex,
# though getting it to run in linear time requires a few non-obvious tricks.
#
# To motivate KMP, consider the naive algorithm for trying to match a pattern

Scaling your API with rate limiters

The following are examples of the four types rate limiters discussed in the accompanying blog post. In the examples below I've used pseudocode-like Ruby, so if you're unfamiliar with Ruby you should be able to easily translate this approach to other languages. Complete examples in Ruby are also provided later in this gist.

In most cases you'll want all these examples to be classes, but I've used simple functions here to keep the code samples brief.

Request rate limiter

This uses a basic token bucket algorithm and relies on the fact that Redis scripts execute atomically. No other operations can run between fetching the count and writing the new count.

@AdityaSoni19031997
AdityaSoni19031997 / grokking_to_leetcode.md
Created August 2, 2022 02:54 — forked from tykurtz/grokking_to_leetcode.md
Grokking the coding interview equivalent leetcode problems

GROKKING NOTES

I liked the way Grokking the coding interview organized problems into learnable patterns. However, the course is expensive and the majority of the time the problems are copy-pasted from leetcode. As the explanations on leetcode are usually just as good, the course really boils down to being a glorified curated list of leetcode problems.

So below I made a list of leetcode problems that are as close to grokking problems as possible.

Pattern: Sliding Window

@AdityaSoni19031997
AdityaSoni19031997 / best_path.py
Created August 14, 2022 15:06 — forked from nlehrer/best_path.py
Best path through a grid of point values
# Nathan Lehrer
def get_best_path(grid):
# Finds the best path through an M x N grid of point values, and that path's score
# Input: grid = grid of point values = M x N list of lists
# Returns: best_score = best possible score = int, path = best possible path = string
M,N = len(grid),len(grid[0])
scores = {(0,0):grid[0][0]} # best score for a path to each cell; score of (0,0) is grid value
trace = {} # whether we optimally come from up ('U') or left ('L') into each cell
gunicorn run:app --workers=9
gunicorn run:app --workers=9 --worker-class=meinheld.gmeinheld.MeinheldWorker

Macbook Pro 2015 Python 3.7

Framework Server Req/s Max latency +/- Stdev