—the rationale being that the text under the sigma _isn't_ asserting that _j_ _equals_ zero, but rather that _j_ is _assigned_ zero as the initial index value of what is, in fact, a for loop:
-```
+```c
sum = 0;
for (int j=0; j<=n; j++)
{
Although first, we will need a function to find the prime factorization of a natural number. You can write your own, copy-paste [someone else's](http://glowingpython.blogspot.com/2011/07/prime-factor-decomposition-of-number.html), or (my personal favorite) use the subprocess module to call the system's _/usr/bin/factor_:
-```
+```python
from subprocess import check_output
def factorize(n):
But then coding the definition of the arithmetic derivative itself is easy:
-```
+```python
def D(n):
result = 0
factorization = factorize(n)
Yes, turtle graphics! In case you had a deprived childhood, I should explain: the idea is that you have a cursor on the screen (the turtle), and you can type, say,
-```
+```text
FORWARD 10
```
and then the turtle will move forward ten units, drawing a line as it goes. And then you can say, like,
-```
+```text
RIGHT 60
```
Let's start writing instructions for our turtle. First, we'll summon the turtle. Also, let's tell her to look up instructions on how to compute the argument of a complex number—don't ask me how, but I have a feeling we're going to need that later:
-```
+```python
import turtle
from cmath import phase
```
Now, it turns out (claims _Wikipedia_, and I believe it), that if the absolute value of one of our _z__n_s ever exceeds two, then that sequence will diverge. This seems like a useful fact, so let's tell our turtle how to calculate how many iterations it will take for the sequence associated with a particular _c_ to exceed two, and if it doesn't do so within some given number of iterations, then to tell us that:
-```
+```python
def z_n_escape_time(c, n):
z = 0
for i in range(n):
To approximate the boundary of the Mandelbrot set, we'll tell the turtle to consider a grid of reasonably-finely-spaced points, and to consider a point to be on the boundary if the number of iterations it takes for the sequence for that point to exceed two is in some given range. We'll also tell her to sort those points by their argument, because that seems like a somewhat-reasonable order to visit them in:
-```
+```python
def mandelbrot_edge(resolution, iterations, edge):
points = []
x_coordinates = [-2+i*(3/resolution) for i in range(int(resolution*1.5))]
Then (choosing some reasonable-looking numbers as specific parameters for our earlier instructions) we tell our turtle to visit all those points, drawing along the way:
-```
+```python
def draw_boundary(protagonist, boundary_points, speed):
protagonist.speed(speed)
protagonist.penup()
So in Clojure we might say
-```
+```clojure
(require 'clojure.set)
(defn include-element [collection element]
Or _maybe_ ... _not_-so-scandalous. For two or three _characters_ need not imply two or three _keystrokes_; one need only configure one's editor with convenient bindings for the insertion of `do` and `end`. For example, pasting the following code into one's Emacs init file assigns `M-[` (respectively `M-]`) to insert the text `do` (respectively `end`), much as one would type `Shift-[` (respectively `Shift-]`) for an open- (respectively close-) brace, except with Alt ("Meta" in Emacs parlance) instead of Shift—
-```
+```elisp
(fset 'block-do
"do")
(global-set-key (kbd "M-[") 'block-do)
It turns out that there's a conceptually straightforward algorithm for solving the maximum flow problem, known as the Ford-Fulkerson method, which we'll implement in Ruby. But first, we'll want to pin down exactly how the flow network will be represented. Let's define an `Edge` class, each instance of which will be initialized with the names of the nodes at its head and tail, and the edge's maximum capacity:
-```
+```ruby
class Edge
attr_accessor :tail
attr_accessor :head
And let's also make a `FlowNetwork` class, defined by the names of its source and sink nodes, and a hash which maps `Edge`s to the amount of flow currently assigned to that `Edge`:
-```
+```ruby
class FlowNetwork
attr_accessor :source
attr_accessor :sink
So how _do_ we solve the maximum-flow problem? It's pretty simple! We start with the "zero flow." (In our code above, this is already done when we initialize an instance of `FlowNetwork`.) We then find an _augmenting path_ from the source to the sink (possibly including edges traversed backwards) such that all the forward-traversed edges along the path have not been assigned their maximum capacity, and all the backward-traversed edges have a nonzero amount of flow. This is path along which we can _push_ more flow from the source to the sink, by increasing the amount of flow along the forward-traversed edges, and decreasing the amount of flow along the backward-traversed edges. (Convince yourself that pushing flow through an edge _backwards_ amounts to decreasing the amount of flow along that edge.) Once we've found such an augmenting path, we push as much flow as we can along it. Then we look for another augmenting path and do the same, until no more augmenting paths can be found. It turns out that the resulting flow assignment solves our problem. So that's the Ford-Fulkerson method:
-```
+```ruby
def ford_fulkerson
path = augmenting_path
while path
Of course, we need to specify exactly how to _find_ these augmenting paths. This is a little bit more involved. It amounts to doing walking through the graph starting at the source, "labeling" nodes that could be part of an augmenting path, and scanning neighbors of labeled nodes looking for more labelable nodes, until we reach the sink (in which case we have found a path) or until we run out of nodes to scan (in which case there are no augmenting paths left). Sort of like this:
-```
+```ruby
def augmenting_path
labeled = {@source=>nil} # keys are labeled nodes; values, parents thereof
scanned = {}
And of course, we also need code to actually augment the flow along the path found—
-```
+```ruby
def flow_augmentation(path)
flow = +1.0/0 # positive infinity
edges = []
Then throw in a reporting method so we have some way of actually inspecting our network and its completed flow assignment, close the `FlowNetwork` class definition (at long last) ...
-```
+```ruby
def report
print "Source: ", @source, "\n"
print "Sink: ", @sink, "\n"
We can then (admittedly with some typing) call for a solution like so:
-```
+```ruby
my_edges = [Edge.new('A', 'B', 12), Edge.new('A', 'E', 15),
Edge.new('A', 'G', 13), Edge.new('B', 'C', 9), Edge.new('E', 'C', 11),
Edge.new('G', 'E', 7), Edge.new('C', 'D', 18), Edge.new('C', 'F', 10),
And receive it like so:
-```
+```text
Source: A
Sink: I
A -> B; capacity: 12, flow: 9
First, we'll need a data structure to represent the nodes of the tree. Actually, we'll use a structure that represents a node _and_ all of its children, which we'll call `Subtree`. Each instance of `Subtree` will have fields for a _character_ and _frequency_ associated with a node, and the node's left and right children if any (which are also instances of `Subtree`).
-```
+```python
class Subtree:
def __init__(self, char, freq, left, right):
self.char = char
We'll also want nodes to be comparable by their frequencies:
-```
+```python
def __gt__(self, othernode):
return self.freq > othernode.freq
```
When all is said and done, Huffman's algorithm will give us an instance of `Subtree` representing our tree, but it would really be more convenient to have the decoding information embodied by the tree in the form of Python dictionary mapping bitstrings to the characters they encode. We can get this by doing a standard recursive tree walk:
-```
+```python
def codebook(self):
codes = {}
def traversal(item, code):
We're also going to need a _min-priority queue_, a dynamic set from which we can insert items and retrieve the "smallest":
-```
+```python
class MinPriorityQueue:
def __init__(self):
self.queue = []
Now that we have the appropriate data structures, we're ready for Huffman's algorithm. Suppose our character/frequency data are given to us in a Python dictionary `C`. For each character, we create an instance of `Subtree` to be the corresponding leaf node in our tree, and put them all in a min-priority queue. Then we build our tree from the bottom up by selecting the two lowest-frequency nodes in the queue, making them the children of a new node whose frequency is the sum of their frequencies, putting the new node back in the priority queue, and again until we've built the entire tree:
-```
+```python
def Huffman(C):
Q = MinPriorityQueue()
leaves = {Subtree(k, C[k], None, None) for k in C}
And that's Huffman's algorithm. Of course, we'll also want functions for encoding and decoding a message:
-```
+```python
def code(plaintext, codebook):
return ''.join(codebook[c] for c in plaintext)
So, suppose you want to send your friends in a nearby alternate universe the eighty-two character message, "I USED TO WONDER WHAT FRIENDSHIP COULD BE, UNTIL YOU ALL SHARED ITS MAGIC WITH ME." If you used seven-bit ASCII, you'd have to pay the cost of transmitting 82\*7 = 574 bits. But if you use a Huffman code informed by knowledge of [English letter frequencies](http://en.wikipedia.org/wiki/Letter_frequency#Relative_frequencies_of_letters_in_the_English_language) ...
-```
+```python
eng_freqs = {'A' : 8167, 'B' : 1492, 'C' : 2782, 'D' : 4253, 'E' : 12702,
'F' : 2228, 'G' : 2015, 'H' : 6094, 'I' : 6966, 'J' : 153, 'K' : 772,
'L' : 4025, 'M' : 2406, 'N' : 6749, 'O' : 7507, 'P' : 1929, 'Q' : 95,
You can send _this_ instead:
-```
+```text
0111010111100000100111001010110110010101110101001011011001001111110101110100
0001010110101011100111111011100101101100100010000011110000101011110110011111
0010110110010101000000011100001011110001101101011110110010100010100111110001
So, this gift having been provided to us courtesy of [Karol S.](https://github.com/KarolS) and the brony community, let's _do_ something with it! More specifically, how about we implement quicksort?—that is a _classic_. What's quicksort? Well, we want to sort a list, right? So—bear with me—we define this partitioning procedure that, given indices into an array, partitions the subarray between those indices into a subsubarray of elements less-than-or-equal-to a given element dubbed the _pivot_, then the pivot itself, then a subsubarray of elements greater than the pivot. How do we do that? Well, let's designate the last element in our subarray as the pivot. Then we're going to scan through all the other elements, and if any of them are less-than-or-equal-to the pivot, we swap it into our first subsubarray and increment a variable keeping track of where the first subsubarray ends. Then, we swap the pivot into place and return its index. In Ruby—
-```
+```ruby
def partition(array, p, r)
i = p-1
for j in p..(r-1) do
Then we can sort an entire array with a bunch of recursive calls to our partitioning procedure:
-```
+```ruby
def quicksort(array, p, r)
if p < r
q = partition(array, p, r)
So that's quicksort. With a little more effort, we can do the same thing in FIM++:
-```
+```text
Dear Princess Celestia: Letter about Quicksort:
I learned about exchange with Applejack, Rainbow Dash, and Rarity.
Tags: Clojure
Slug: a-line-of-code-i-havent-found-an-excuse-to-use-yet
-```
+```clojure
(defn intentional-mispelling? [sic]
```
Tags: Python
Slug: clarity-of-intent
-```
+```irc
<bob> and here on line 79---
<bob> else:
<bob> return None
Anyway, consistent hashing. The essential nature of your problem is that you need a way to split the space of possible filenames into _n_ [partitions](http://en.wikipedia.org/wiki/Partition_of_a_set) in a way that minimizes the number of files that have to change partitions when _n_ changes. So imagine mapping the 128-bit output space of MD5 around a ring. Now pseudorandomly designate _m_ spots on the ring (_m_ being some prudently-chosen natural number) for each of your _n_ storage devices: say, by hashing the device name suffixed with the number _j_ for each _j_ between 0 and _m_–1 inclusive. Internally, you might represent the ring as just an array of hash/device pairs. Like this (in Clojure; here the array and the pairs it contains are actually "vectors")—
-```
+```clojure
(import 'java.security.MessageDigest)
(defn md5 [string]
Then when your patrons ask you to store a file, you're still going to hash its name, but instead of taking the residue mod _n_ of the hash to decide where the file should live, you're going to locate the hash in your ring, and find the next designated spot on the ring associated with one of your storage devices, and the file will live on that device. In terms of your internal representation of the ring as an array of hash/device pairs, you'll find the first pair whose first item (the hash of the suffixed device name) is greater than the hash of the name of the file to be stored, and the second item of that pair will tell you which storage device to use. (And if the filename hash is greater than all those stored in the (first items of the pairs stored in the) array, then you just use the first pair in the array. It is supposed to represent a _ring_, after all.) Like this—
-```
+```clojure
(defn ring-search [ring key]
(or (some (fn [pair]
(if (> (compare (first pair) key) 0)
And now—why, _now_ when you gain (respectively lose) a storage device, you can decide the allocation question simply by adding (respectively removing) the appropriate designated spots to (respectively from) your ring—
-```
+```clojure
(defn ring-add [ring device spots]
(sort (vec (concat ring (ring-kv-pairs device spots)))))
You decide to run a quick simulation to see if you've gotten this right before saving the edits to your soul. Suppose you had storage devices `device0` through `device5` and your patrons wanted you to store `file0` through `file5999`; how would the files end up distributed across your devices? You choose a prudent-seeming integer for the number of designated spots per device—say, 450?—and write the simulation like this—
-```
+```clojure
(defn filemap [ring files]
(into {}
(for [file files
And the results seem reasonable enough:
-```
+```text
$ clojure consistent_hashing.clj
[device0 1020]
[device1 993]
Tags: Clojure
Slug: convention
-```
+```console
$ lein new 3lg2048
Project names must be valid Clojure symbols.
$ lein new Thirty-Three
Okay, so there actually is a [pandoc-mode](http://joostkremers.github.io/pandoc-mode/), but I couldn't figure out how to configure and use it, so it was easier to just write the one command that I wanted—
-```
+```elisp
(defun markdown-to-html ()
(interactive)
(let* ((basename (file-name-sans-extension (buffer-file-name)))
Tags: Python
Slug: debugging-techniques-i
-```
+```python
#def my_problematic_function(x):
def my_problematic_function(x, call_count=[]):
call_count.append(1)
Well, I already made it for you (unless your Linux distribution doesn't use `notify-send` or you're on a Mac or something).
-```
+```python
#!/usr/bin/env python3
import argparse
\* \* \*
-```
+```console
$ heroku create
Creating howling-nightmare-4505... done, stack is cedar
http://howling-nightmare-4505.herokuapp.com/ | git@heroku.com:howling-nightmare-4505.git
\* \* \*
-```
+```text
-----> Python app detected
-----> Installing runtime (python-2.7.8)
```
\* \* \*
-```
+```console
$ echo "python-3.4.1" > runtime.txt
$ g a .
$ gco -m "the month of July 2010 called and wants their programming language back"
Tags: Git, Python
Slug: my-favorite-error-message-this-year
-```
+```text
zmd@SuddenHeap:~/Code/Finetooth$ git commit --amend
Traceback (most recent call last):
File "./manage.py", line 8, in <module>
Tags: akrasia, Python
Slug: attentional-shunt
-```
+```python
#!/usr/bin/env python3
# Copyright © 2015 Zack M. Davis
Tags: akrasia
Slug: back-from-running
-```
+```irc
[16:03:37] <alice> I'm back from literally running, metaphorically from
figurative demons
[16:03:50] <alice> including the celebrity demon-prince Rateirs-Blak
Note! The end of the string, _or just before the newline at the end of the string_.
-```
+```text
In [2]: my_regex = re.compile("foo$")
In [3]: my_regex.match("foo")
> [b]y default, the regular expressions `^` and `$` ignore line terminators and only match at the beginning and the end, respectively, of the entire input sequence.
-```
+```text
user=> (re-matches #"foo$" "foo")
"foo"
user=> (re-matches #"foo$" "foo\n")
[(Previously.)](http://zackmdavis.net/blog/2014/05/a-short-story/)
-```
+```irc
[19:26:50] <bob> alice: you still around?
[19:27:08] <alice> bob, sort of
[19:27:20] <bob> alice: ok. never mind.
__Favorite line of code:__ a tie, between
-```
+```rust
let mut time_radios: Vec<(Commit, mpsc::Receiver<(Option<Commit>, f32)>)> =
Vec::new();
```
and
-```
+```python
for (previous, new), expected in zip(
itertools.product(('foo', None), ('bar', None)),
("from foo to bar", "from foo", "to bar", "")):
So suppose we want to save some textual data; say, the string, "THE FUNDAMENTAL PROBLEM OF COMMUNICATION IS THAT OF REPRODUCING AT ONE POINT EITHER EXACTLY OR APPROXIMATELY A MESSAGE SELECTED AT ANOTHER POINT". Now, this data is made out of letters and spaces, not polynomials, but we can process it into a form that will make it easier to make-believe that it is. Say, we split the text into chunks of a fixed size (padding the end with extra spaces if necessary so that our chunk size evenly divides it), and convert the characters into integers from 0 to 26 (space is 0, _A_ is 1, _B_ is 2, _&c._). Here are some functions to help with that—
-```
+```python
from string import ascii_uppercase
ALPHABET = " "+ascii_uppercase
(It's actually better if you use polynomials over the _finite field_ 𝔽_q_ of the integers modulo _q_ for some _q_ which is a prime raised to the power of something, but let's not worry about that.)
-```
+```python
def evaluate_polynomial(coefficients, x):
return sum(c * x**i for i, c in enumerate(coefficients))
Then, with a choice for the original chunk size (which you'll recall will also be the number of terms each in the polynomials used to encode each chunk) and the size of the resulting encoded chunk (that is, the number of points we'll sample from the polynomials), we can encode our text.
-```
+```text
$ python3
>>> from reed_solomon import *
>>> text = "THE FUNDAMENTAL PROBLEM OF COMMUNICATION IS THAT OF
Let's simulate distributing that encoded information across several storage nodes by writing points with different _x_-values to different files. We'll make-believe that each file is a different storage node. We'll write another function for that.
-```
+```python
import json
def disperse(encoded_chunks):
And try it out—
-```
+```text
>>> disperse(encoded)
>>>
$ ls
What's that you say, dear reader? Demonstrating how to encode something is useless unless you also demonstrate how to decode it? Well, I suppose you may have a point. Never fear—we can do that, too! But first, we'll need some functions for manipulating polynomials (in the "list of coefficients in order of ascending power" form that we've been using).
-```
+```python
def get_coefficient(P, i):
if 0 <= i < len(P):
return P[i]
So by design, our interpolated polynomial takes value _y_1 at _x_1, _y_2 at _x_2, and so forth. In Python, the whole process looks like this—
-```
+```python
def lagrange_basis_denominator(xs, i):
denominator = 1
for j, x in enumerate(xs):
With this technique, we now have all the tools we need to recover our text from a subset of the data we wrote to our various "nodes" earlier. What we need to do is this: for each chunk, arbitrarily select a number of stored points equal to our chunk size, _interpolate_ the polynomial from them, _deconvert_ the numbers which are the coefficients of that polynomial back into their character equivalents, _unchunkify_ the chunks into a unified whole, and _unpad_ any whitespace we added to the end when we began.
-```
+```python
def deconvert(sequence):
return ''.join(INT_TO_CHAR[i] for i in sequence)
But _about_ that data that we wrote out earlier.
-```
+```console
$ ls
node0 node1 node2 node3 node4 node5 node6 node7
```
It would hardly be a compelling test of our erasure-coding skills if there were any suspicion that we actually needed _all_ of those files—we really only need as many as our chunk size. So let's suppose that three of our nodes die in a fire—
-```
+```console
$ rm node1 node3 node6
$ ls
node0 node2 node4 node5 node7
Could this the end of our data? With a full three-eighths of our encoding having been utterly destroyed, is it delusional to hold out hope that our text might yet be faithfully recovered? No! No, it is not! We only need one more function to retrieve the encoded chunks—
-```
+```python
def retrieve(*nodes):
responses = []
for node in nodes:
—and then—
-```
+```text
$ python3
>>> from reed_solomon import *
>>> node_data = retrieve("node0", "node2", "node4", "node5", "node7")
—successfully decode them!
-```
+```pycon
>>> erasure_decode(node_data, 5, 8)
'THE FUNDAMENTAL PROBLEM OF COMMUNICATION IS THAT OF REPRODUCING AT
ONE POINT EITHER EXACTLY OR APPROXIMATELY A MESSAGE SELECTED AT AN
Tags: Clojure
Slug: xxx-i
-```
+```clojure
;; XXX: adorable
(defmacro λ [& code]
`(fn ~@code))
Tags: Rust
Slug: xxx-ii
-```
+```rust
// XXX: old_io is probably facing deprecation if names mean anything
#![feature(old_io)]
use std::old_io;
Tags: Rust
Slug: xxx-iii
-```
+```rust
const PSEUDO_DIGITS: [char; 7] = ['M', 'D', 'C', 'L', 'X', 'V', 'I'];
const PSEUDO_PLACE_VALUES: [usize; 7] = [1000, 500, 100, 50, 10, 5, 1];
Tags: Git, Unicode
Slug: 0x1f431-cat-face
-```
+```diff
diff --git a/.bash_aliases b/.bash_aliases
index 648287f..e00dbc9 100644
--- a/.bash_aliases
Real chess aficionados (chessters? chessies?) will laugh at me, but it actually took me a while to understand why Ng3 was in that principal variation (I might even have invoked the engine again to help). The position after Ng3 looks like
-```
+```text
a b c d e f g h
8 ♜ ♜ ♚
7 ♟ ♟ ♟ ♟ ♟ ♟
Python has this elegant destructuring-assignment iterable-unpacking syntax that every serious Pythonista and her dog tends to use whereëver possible. So where a novice might write
-```
+```python
split_address = address.split(':')
host = split_address[0]
port = split_address[1]
a serious Pythonista (_and_ her dog) would instead say
-```
+```python
host, port = address.split(':')
```
Consider, however, the somewhat-uncommon case where we have an iterable that, for whatever reason, we happen to _know_ contains only one element, and we want to assign that one element to a variable. Here, I've seen people who ought to know better fall back to indexing:
-```
+```python
if len(jobs) == 1:
job = jobs[0]
```
But there's _no reason_ to violate the æsthetic principle of "use a length-_n_ ([or smaller](https://www.python.org/dev/peps/pep-3132/)) tuple of identifiers on the left side of a destructuring assignment in order to name the elements of a length-_n_ iterable" just because _n_ happens to be one:
-```
+```python
if len(jobs) == 1:
job, = jobs
```
Tags: Rust
Slug: todo-i
-```
+```rust
let path = Path::new("/proc/meminfo");
let proc_meminfo = match File::open(path) {
Ok(f) => f,
Tags: akrasia
Slug: binge-purge
-```
+```console
$ history | grep freeciv
605 freeciv
606 sudo apt-get install freeciv
# Markdown extensions
MARKDOWN = {
'extension_configs': {
- 'markdown.extensions.codehilite': {'css_class': 'highlight'},
+ 'markdown.extensions.codehilite': {'css_class': 'highlight', 'guess_lang': False},
'markdown.extensions.extra': {},
'markdown.extensions.meta': {},
'markdown.extensions.toc': {