This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Print out a simple table with right-justified columns of strings. | |
// This can handle some columns having fewer entries than others. | |
// (For mid-table missing entries, provide empty strings.) | |
// The `cols` value is an array of arrays of strings, the columns of the table. | |
// The optional `topSep` is a string the indicates the separator to use in the | |
// top row only. If you don't provide topSep, a single space is used. | |
function showTableWithColumns(cols, topSep) { | |
if (topSep === undefined) topSep = ' '; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This will sort the values in `inputArr` according to the comparison data | |
// provided by calls to `inputCmp()`, which is expected to return the values '=' | |
// (a string), '<', '>', or null; where a null indicates that the comparison | |
// value is undetermined, and that those two elements may go in any order. | |
// This function attempts to reduce the number of calls to inputCmp() in several | |
// ways: | |
// * It memorizes given return values. | |
// * It assumes that if a < b then also b > a (otherwise what is happening?). | |
// * It builds a tree to infer transitive comparisons, and tries to maximize | |
// the use of that tree. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The inputArr is an array of any kind of values. | |
// The inputCmp() function accepts two values, x and y; with the return | |
// value indicating the relationship as such: | |
// | |
// If: then cmp(x, y) returns: | |
// x < y '<' | |
// x > y '>' | |
// x = y '=' | |
// x ? y null | |
// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Stops at any null characters. | |
int decode_code_point(char **s) { | |
int k = **s ? __builtin_clz(~(**s << 24)) : 0; // Count # of leading 1 bits. | |
int mask = (1 << (8 - k)) - 1; // All 1's with k leading 0's. | |
int value = **s & mask; | |
for (++(*s), --k; k > 0 && **s; --k, ++(*s)) { // Note that k = #total bytes, or 0. | |
value <<= 6; | |
value += (**s & 0x3F); | |
} | |
return value; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local t_sum = 0 | |
local t_num = 0 | |
local t1, t2, t3 | |
local do_time = true | |
local function mark1() | |
if not do_time then return end | |
t1 = os.clock() | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def call_on_leaves(json_obj, fn, path=None): | |
path = path or [] | |
if type(json_obj) is dict: | |
for k, v in json_obj.items(): | |
if not call_on_leaves(v, fn, path + [k]): | |
return False | |
elif type(json_obj) is list: | |
for i, v in enumerate(json_obj): | |
if not call_on_leaves(v, fn, path + [i]): | |
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" toggle | |
Usage: | |
toggle start | |
toggle getip | |
toggle stop | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ -z "$1" ]; then | |
echo Usage: ./fix_aspect.sh '<img_file>' | |
echo | |
echo This will create a new image file with the word 'fixed' appended before | |
echo the filename extension. Eg, myimg.jpg will become myimg.fixed.jpg. | |
exit | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML> | |
<html> | |
<body> | |
<svg width="600" height="600" vertion="1.1" | |
id="svg" xmlns="http://www.w3.org/2000/svg"> | |
<defs> | |
<clipPath id="angleClip"> | |
<polygon id="atriangle" /> | |
</clipPath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" reservoir.py | |
This is an example implementation of an efficient | |
reservoir sampling algorithm -- this algorithm is useful | |
when you have a data stream, possibly of unknown length, | |
and you'd like to maintain an incrementally updated | |
random subset of fixed size k. This algorithm works by | |
occasionally adding a new data point from the stream | |
into the 'reservoir,' which is simply a length-k list | |
of data points. |