Persisting state on a static site, for free. Motivation Most “dynamic” sites repeat the same choreography: pick a datastore, wrap it with an API, wrap that with a framework, then deploy everything across services you’ll eventually forget to clean up. All I wanted was a little interactivity on an otherwise static site, without signing up for another monthly bill. A simple 10×10 grid of checkboxes…
Motivation Let’s explore how we can simulate physical phenomena in Python by building everything from scratch, step by step. When it comes to modeling physical systems, there are three primary approaches: the Newtonian, the Lagrangian, and the Hamiltonian. The Newtonian approach, grounded in \(F = ma\) , focuses on forces and accelerations to describe motion. The Lagrangian approach, based on the…
Background \[(x \lor \neg y) \land (\neg x \lor y) \land (\neg x \lor \neg y) \land (y \lor z)\] While ( SAT ) is generally NP-complete, meaning no efficient algorithm is known to solve it for all cases, 2-SAT , a special case of SAT where each clause contains exactly two literals, can be solved efficiently in linear time! Specifically, 2-SAT can be solved in O(n + m) time, where n is the number…
Summary In Building Ads Optimization we explored the process of building ads optimization and highlighted common pitfalls from an intuitive perspective. In this post, we’ll take a more theoretical approach, starting with the definition of an objective function and constraints, and then delving into solving the constrained optimization problem. Optimization Formulation Let’s work on finding an ads…
Summary I’ve observed many companies struggle to build a successful ads revenue business. They build reasonable solutions for personalized feed, recommendation and search systems, but they fail to build a successful ads optimization engine. I started this series to share my learnings and hopefully help those who are in the early stages of their ads optimization journey. Ads Ecosystem In an…
Motivation Let’s take a look at how we multiply two polynomials of degree \(N\): \[\begin{align*} f(x) &= 4x^{4}-2x^{3}-6x^{2}\ +4x\ +\ 3\\ g(x) &= -x^{4}+11x^{3}-9x^{2}+-1x\ +\ 6\\ \end{align*}\] We can use the distributive property to multiply two polynomials and then sum up coefficients for identical terms. \[\begin{align*} f(x) &\cdot g(x) = \\ (4x^{4}-2x^{3}-6x^{2}\ +4x\ +\ 3) &\cdot…
The Chaos Game The Sierpiński triangle is a famous fractal with many interesting properties. Here we can take a look at a fun way to generate it! Follow these steps: Start from a random point inside the triangle (P) Mark P Pick a random corner of the triangle (C) P = the midpoint of the line segment PC go to step (2) Simulation The simulation below demonstrates what happens if we repeat this…
Puzzle You are a single point in a square room and all the walls are mirrors. Someone in the room is trying to shoot you with a laser from another point in the room. No one is allowed to move, but you can install single-point blockers to completely block the line and not reflect it. What is the minimum number of blockers you need to guarantee you’ll be safe? Would that even be finite? Solution…
Introduction Fibonacci Numbers form a sequence in which each number is the sum of the two preceding ones, defined recursively as: \[\begin{equation*} \begin{aligned} &F(0) = 0\\ &F(1) = 1\\ &F(n) = F(n-1) + F(n-2) \end{aligned} \end{equation*}\] We would like to compute \(F(n)\) in the most efficient way possible! Dynamic Programming You can solve it in O(n) time and O(n) space via dynamic…
Problem Inversions Find the number of permutations of \({1,2,...,N}\) with exactly \(K\) inversions (modulus \(2\)). Summary of my approach Step 1 Figure it is called “mahonian triangle numbers” , either by using OEIS or by reading at the bottom of this wikipedia page inversions . Step 2 Find the relationship with “Truncated Euler Product Triangle” from this paper . You can basically write…