Skip to content

Instantly share code, notes, and snippets.

@andrewgho
andrewgho / fizzbuzz.c
Created August 4, 2015 06:22
Branchless FizzBuzz
#include <stdio.h>
int main() {
int i, idx, mask[] = {3, 0, 0, 0, 0};
char buf[4], *out[] = {buf, "Fizz", "Buzz", "FizzBuzz"};
for(i = 1; i <= 100; i++) {
sprintf(buf, "%d", i);
idx = (mask[i % 3] & 1) + (mask[i % 5] & 2);
printf("%s\n", out[idx]);
#!/bin/sh
# bee - return Spelling Bee words from maximal set
# Andrew Ho <andrew@zeuscat.com>
ME=`basename "$0"`
USAGE="usage: $ME letters middle-letter"
die_usage() { (echo "$ME: $@"; echo "$USAGE") 1>&2; exit 1; }
letters="$1"
[[ -z "$letters" ]] && die_usage 'missing required letters'
@andrewgho
andrewgho / bash_sub.sh
Last active October 27, 2022 17:31
bash in-process string substitution with one capturing group
# Bash parameter expansion syntax for ${parameter/match/replacement}:
# https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
#
# Bash pattern matching syntax:
# https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html
#
# Bash conditional [[ string =~ pattern ]] and $BASH_REMATCH syntax:
# https://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html
#
# Recipe to do a string replace with capturing:
#!/usr/bin/env ruby
require 'uri'
require 'net/http'
require 'json'
require 'http-cookie'
module Uck
class Client
attr_reader :baseurl
@andrewgho
andrewgho / firstboot.sh
Last active June 21, 2020 23:42
firstboot.sh - Raspberry Pi OS (buster) first boot initialization script
#!/bin/sh
# firstboot.sh - Raspberry Pi OS (buster) first boot initialization script
# Andrew Ho (andrew@zeuscat.com)
# Set pi user password
passwd
# Add en_US.UTF-8 locale and set as default
cp /etc/locale.gen locale.gen.orig
sudo sed -i 's/^# \(en_US.UTF-8.*\)$/\1/' /etc/locale.gen
@andrewgho
andrewgho / unifi_clients.sh
Last active June 21, 2020 05:01
unifi_clients.sh - dump list of clients from UniFi Cloud Key
#!/bin/sh
# unifi_clients.sh - dump list of clients from UniFi Cloud Key
# Andrew Ho (andrew@zeuscat.com)
#
# https://ubntwiki.com/products/software/unifi-controller/api
USERNAME=redacted
PASSWORD=redacted
BASEURL=https://unifi-cloudkey-gen2:8443
@andrewgho
andrewgho / signal_standalone.md
Last active October 4, 2017 04:27
Build Standalone Signal Desktop App for OS X

Build Standalone Signal Desktop App for OS X

This document describes how to build a standalone (not Chrome app) Signal desktop application for OS X, including packaging up the appropriate icons for the application.

Procedure

Find latest version of the NW.js SDK from http://dl.nwjs.io, then download and unpack it:

@andrewgho
andrewgho / keybase.md
Created February 10, 2017 06:39
Keybase proof

Keybase proof

I hereby claim:

  • I am andrewgho on github.
  • I am andrewgho (https://keybase.io/andrewgho) on keybase.
  • I have a public key whose fingerprint is 9BBA F92A 9768 B022 29E0 AFB0 9765 DF56 02C1 B75F

To claim this, I am signing this object:

@andrewgho
andrewgho / git-total-reset
Last active December 20, 2015 18:59
git-total-reset - wipe out any changes, make repo match origin/master
#!/bin/sh
# git-total-reset - wipe out any changes, make repo match origin/master
# Andrew Ho (andrew@zeuscat.com)
git checkout -f master
git stash clear
git fetch
git reset --hard origin/master
git clean -xfd
@andrewgho
andrewgho / wordchomp
Created July 31, 2013 21:50
Given set of letters, generate sets of words that use them all
#!/usr/bin/env ruby
# wordchomp - given set of letters, generate sets of words that use them all
# Andrew Ho (andrew@zeuscat.com)
require 'multimap'
ME = File.basename(__FILE__)
USAGE = "usage: #{ME} letters"
# Given set of letters, generate set partitions, look for matches in dictionary