Median of 5 sorted arrays

(This is a generalization of your idea for two arrays.) If you start by looking at the five medians of the five arrays, obviously the overall median must be between the smallest and the largest of the five medians. Proof goes something like this: If a is the min of the medians, and b is … Read more

Type-juggling and (strict) greater/lesser-than comparisons in PHP

PHP’s comparison operators deviate from the computer-scientific definitions in several ways: In order to constitute an equivalence relation == has to be reflexive, symmetric and transitive: PHP’s == operator is not reflexive, i.e. $a == $a is not always true: var_dump(NAN == NAN); // bool(false) Note: The fact that any comparison involving NAN is always … Read more

Django edit form based on add form?

If you are extending your form from a ModelForm, use the instance keyword argument. Here we pass either an existing instance or a new one, depending on whether we’re editing or adding an existing article. In both cases the author field is set on the instance, so commit=False is not required. Note also that I’m … Read more