Skip to content

Instantly share code, notes, and snippets.

@aglee
aglee / gist:1267594
Created October 6, 2011 14:51
UNTESTED but I already know this is still not quite right. See http://www.mikeash.com/pyblog/key-value-observing-done-right.html for what could go wrong. Should use MAKVONotificationCenter to do the KVO. In any case, this code is just to give a general id
// MyViewTickler.h
#import <Cocoa/Cocoa.h>
@interface MyViewTickler : NSObject
{
}
+ (id)sharedInstance;
@aglee
aglee / gap-stores-autism-discount.htm
Created September 28, 2012 15:16
What you get when you ask Excel to save as a .htm file
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=macintosh">
<meta name=ProgId content=Excel.Sheet>
<meta name=Generator content="Microsoft Excel 14">
<link rel=File-List href="gap-stores-autism-discount_files/filelist.xml">
<style>
<!--table
{mso-displayed-decimal-separator:"\.";
@aglee
aglee / NSApplication+MarkdownCredits.h
Created December 22, 2013 18:53
Category on NSApplication that makes it easy to use a Markdown-formatted Credits file, or to use your README as the Credits file.
//
// NSApplication+MarkdownCredits.h
// ALUtilities
//
// Copyright (c) 2013 Andy Lee
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
// and associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute,
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
//
// RVNReceiptValidation.m
//
// Created by Satoshi Numata on 12/06/30.
// Copyright (c) 2012 Sazameki and Satoshi Numata, Ph.D. All rights reserved.
//
// This sample shows how to write the Mac App Store receipt validation code.
// Replace kRVNBundleID and kRVNBundleVersion with your own ones.
//
// This sample is provided because the coding sample found in "Validating Mac App Store Receipts"
@aglee
aglee / TreeSearch.swift
Last active June 22, 2016 16:52
A Swift exercise
func ==(lhs: Node, rhs: Node) -> Bool {
return lhs === rhs
}
class Node: Equatable {
var value: String
var children: [Node] = []
init(_ value: String) {
self.value = value
#!/usr/bin/python
"""
https://www.hackerrank.com/challenges/cut-the-tree
"""
class Node(object):
def __init__(self, weight):
self.weight = weight
self.adjacent_nodes = []
@aglee
aglee / ij.swift
Last active October 15, 2016 11:24
import Foundation
// Expects i and j to be 1-based row and column indexes, respectively.
func f(_ i: Int, _ j: Int) -> Int {
return ((i+j-1) * (i+j))/2 - (((i+j-1) & 1 == 0) ? i : j) + 1
}
// Test: use f() to generate matrix and visually inspect it
// to see that the numbers are correct.
for i in 1...10 {
@aglee
aglee / ij.py
Last active October 15, 2016 11:23
# Expects i and j to be 1-based row and column indexes, respectively.
def f(i, j):
return (((i+j-1) * (i+j))/2) - (i if ((i+j-1) & 1 == 0) else j) + 1
# Test: use f() to generate matrix and visually inspect it
# to see that the numbers are correct.
for i in range(10):
line = ""
for j in range(10):
line += "%5d" % (f(i+1, j+1))
#import <Foundation/Foundation.h>
// Two things to know about NSSet:
//
// - NSSet assumes its elements will never mutate. It breaks if they do.
// - Unlike NSDictionary, NSSet does not copy its unique objects. It stores
// the original pointers.
//
// Bottom line: DO NOT MUTATE OBJECTS THAT ARE ELEMENTS OF AN NSSET. The
// code below demonstrates the kind of bugs you could run into if you
@aglee
aglee / main.swift
Last active December 6, 2018 16:36
Advent of Code 2018 day 06
import Foundation
let commandURL = URL(fileURLWithPath: CommandLine.arguments[0])
let inputFileURL = commandURL.deletingLastPathComponent().appendingPathComponent("input.txt")
let inputText = try! String(contentsOf: inputFileURL, encoding: .utf8).trimmingCharacters(in: .whitespacesAndNewlines)
let inputLines = inputText.split(separator: "\n")
// I will call the input coordinates "pins", as in pins on a map (is clearer to my mind than "coordinates").
// I will call the index of a pin in allPins its "pinID".