Skip to content

Instantly share code, notes, and snippets.

View shivamMg's full-sized avatar

shivam mamgain shivamMg

View GitHub Profile
@shivamMg
shivamMg / map_reduce_word_counts.py
Last active March 1, 2026 15:17
MapReduce Algorithm
import shutil
from collections import Counter
from pathlib import Path
from typing import List
def print_file(file_path):
"""Print specific parts of a file using seek()."""
with open(file_path, "rb") as f: # use rb to use seek()
f.seek(0, 0) # move cursor to beginning

g.co, Google's official URL shortcut (update: or Google Workspace's domain verification, see bottom), is compromised. People are actively having their Google accounts stolen.

Someone just tried the most sophisticated phishing attack I've ever seen. I almost fell for it. My mind is a little blown.

  1. Someone named "Chloe" called me from 650-203-0000 with Caller ID saying "Google". She sounded like a real engineer, the connection was super clear, and she had an American accent. Screenshot.

  2. They said that they were from Google Workspace and someone had recently gained access to my account, which they had blocked. They asked me if I had recently logged in from Frankfurt, Germany and I said no.

  3. I asked if they can confirm this is Google calling by emailing me from a Google email and they said sure and sent me this email and told me to look for a case number in it, which I saw in

@shivamMg
shivamMg / lru_cache_ttl.py
Last active March 1, 2026 15:17
LRU Cache with TTL
import time
from datetime import datetime, timedelta
class Node:
def __init__(self, key, value, expiry):
self.key = key
self.value = value
self.expiry = expiry
self.next = None
@JoaoLages
JoaoLages / RLHF.md
Last active January 6, 2026 11:28
Reinforcement Learning from Human Feedback (RLHF) - a simplified explanation

Maybe you've heard about this technique but you haven't completely understood it, especially the PPO part. This explanation might help.

We will focus on text-to-text language models 📝, such as GPT-3, BLOOM, and T5. Models like BERT, which are encoder-only, are not addressed.

Reinforcement Learning from Human Feedback (RLHF) has been successfully applied in ChatGPT, hence its major increase in popularity. 📈

RLHF is especially useful in two scenarios 🌟:

  • You can’t create a good loss function
    • Example: how do you calculate a metric to measure if the model’s output was funny?
  • You want to train with production data, but you can’t easily label your production data
@jlherren
jlherren / levenshtein.py
Created February 19, 2020 16:10
Find Levenshtein distance between two strings and construct edit instructions to go from one string to the other
import numpy
def wagner_fisher(s: str, t: str):
"""
Computes the Levenshtein distance between the two strings. Returns a tuple containing
the distance itself and also the entire matrix for further processing.
See: https://en.wikipedia.org/wiki/Wagner%E2%80%93Fischer_algorithm
"""
m, n = len(s), len(t)
@coolreader18
coolreader18 / segfault.py
Last active August 12, 2025 17:18
CPython segfault in 5 lines of code
class E(BaseException):
def __new__(cls, *args, **kwargs):
return cls
def a(): yield
a().throw(E)
@shivamMg
shivamMg / binary_exponentiation.py
Last active March 1, 2026 15:13
Popular algorithms
"""
Binary Exponentiation computes x^n in O(logn) time.
"""
def pow(x, n):
result = 1
power = x
while n > 0:
bit = n % 2
if bit == 1:
@shivamMg
shivamMg / bellman-ford.py
Last active March 1, 2026 15:14
Graph Theory
class GraphBellmanFord(Graph):
def bellman_ford(self, src: str):
dist = {}
for _, u in self.V.items():
dist[u] = float('Inf')
src = self.V[src]
dist[src] = 0
edges = []
for e in self.E.values():
@shivamMg
shivamMg / counting-sort.py
Last active March 1, 2026 15:14
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
@shivamMg
shivamMg / min-heap.py
Last active March 1, 2026 15:14
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):