Skip to content

Instantly share code, notes, and snippets.

andyg0808 /
Last active Jun 25, 2020
Git prepare-commit-msg hook


This hook automatically inserts an appropriate filename description in the suggested commit message for each commit you make which touches a single file.


These are inserted as the first line of the message opened in your editor when running git commit. Messages entered with -m will not be affected:

If you've only touched a single static method

andyg0808 /
Created May 31, 2020
Heap's algorithm in Python
from typing import List
def swap(A, i, j):
temp = A[i]
A[i] = A[j]
A[j] = temp
def generate(k: int, A: List):
andyg0808 /
Last active Mar 5, 2020
Strategy Pattern

Note: I'm not 100% certain of my descriptions vs. how others might define things; hopefully this is enough to get an intuative idea of what's going on.

The Strategy pattern lets you change how a function or method does its job. It might be helpful to think of it in terms of refactoring shapes. When you have two functions with similar parts at the top, you break that top part out into a new function. When they have similar parts at the bottom, you break that out into a new function. In both of those cases, you can replace the original function call with a call to the new "top half" function followed by a call to the new "bottom half" function. But if the similar parts are in the middle of the function, you can break them out into a strategy. This lets you replace a call to each of the specialized methods with a call to the new combined function, passing the appropriate strategy to handle the bit in the middle.

Consider the following two functions:

andyg0808 / git-annex-downloader
Created Apr 9, 2013
Two-line script to make Git Annex able to be a download manager for FlashGot (although it doesn't provide user-visible status). To use, install FlashGot ( and add a new download manager. Specify this script as the download manager executable and use something like [FOLDER] [URL] as the template for its command line arguments.
View git-annex-downloader
# $1=folder to cd to (must be a git annex repo)
# $2=URL to download
cd "$1"
git-annex addurl "$2"