Skip to content

Instantly share code, notes, and snippets.

@charlietuna
charlietuna / inv_sqrt_rrrlog.c
Created April 9, 2014 05:52
Improved (over John Carmack's) inverse square root hack from Řrřola - http://rrrola.wz.cz/inv_sqrt.html
float inv_sqrt(float x)
{
union { float f; uint32 u; } y = {x};
y.u = 0x5F1FFFF9ul - (y.u >> 1);
return 0.703952253f * y.f * (2.38924456f - x * y.f * y.f);
}
@charlietuna
charlietuna / homestyle-edits.css
Created May 13, 2014 05:39
Refactor of The Incomparable HTML and CSS
body
{
font-family: HelveticaNeue-Medium, "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: large;
background-color: #252b6f;
color: white;
text-align: center;
}
a, a:visited, a:hover { color: white; }
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
pip install networkx distance pattern
In Flipboard's article[1], they kindly divulge their interpretation
of the summarization technique called LexRank[2].
@charlietuna
charlietuna / google.logo
Created September 2, 2015 20:49
Google Logo
@charlietuna
charlietuna / lldb-pwin.txt
Created December 11, 2012 22:07
LLDB .lldbinit tip for making a command alias for showing iOS UIKit's current view hierarchy.
command alias pwin expression -o -- (NSString *)[[UIWindow keyWindow] recursiveDescription]
@charlietuna
charlietuna / permute_list
Created April 23, 2013 04:02
Takes a list and returns an array of permutations of that list.
permute_list = (list) ->
if list.length <= 1
return [ list ]
permutations = []
for first, i in list
restOfList = list.slice()
restOfList.splice(i, 1)
for subperm in permute_list(restOfList)
permutations.push([ first ].concat(subperm))
return permutations
@charlietuna
charlietuna / gist:6336352
Created August 25, 2013 21:14
For people who are interested in learning about compiler optimizations
Expression related optimizations: Constant Folding, Constant Propagation, Global Propagation, Strength Reduction, Common Subexpression Elimination, Partial Redundancy Elimination, Induction Variable Elimination, Reassociation
Loop related optimizations: Loop Invariant Code Motion, Loop Peeling, Loop Unrolling, Loop Distribution, Loop Autoparallelization, Loop Fusion, Loop Fission, Loop Interchange, Loop Tiling/Stripmining, Vectorization, Scalarization
Memory/cache related optimizations: Cache blocking, False Sharing Elimination, Structure Peeling, Structure Splitting, Array Contraction, Multi-dimensional Array Dimension Reordering
Control flow related optimizations: Code block re-ordering (by frequency), Branch prediction (by static analysis/feedback guided), Code hoisting/sinking (to optimize CPU pipeline), Automatic Inlining, Tail Call Optimization
Code generation related optimizations: Register allocation (np complete), Peephole optimization, Superoptimization (no one really does this yet, but cool nonethe
@charlietuna
charlietuna / rsa.py
Last active March 17, 2016 21:48
RSA in 4 lines of Python.
#!/usr/bin/python # Usage: rsa.py exponent modulus < plaintext > ciphertext
from sys import*;from string import*;a=argv;[s,p,q]=filter(lambda x:x[:1]!=
'-',a);d='-d'in a;e,n=atol(p,16),atol(q,16);l=(len(q)+1)/2;o,inb=l-d,l-1+d
while s:s=stdin.read(inb);s and map(stdout.write,map(lambda i,b=pow(reduce(
lambda x,y:(x<<8L)+y,map(ord,s)),e,n):chr(b>>8*i&255),range(o-1,-1,-1)))
@charlietuna
charlietuna / rc4.swift
Last active July 29, 2016 06:43
RC4 in Swift
func RC4(🔏:[UInt8], 🔑:[Int]) -> [UInt8]
{
var i = 0, j = 0, S = Array(0...255)
for i in 0...255 {
j = (j + S[i] + 🔑[i % 🔑.count]) % 256
(S[j], S[i]) = (S[i], S[j])
}
i = 0; j = 0
return 🔏.map {
i = (i + 1) % 256; j = (j + S[i]) % 256
Rob Pike's 5 Rules of Programming
Rule 1. You can't tell where a program is going to spend its time. Bottlenecks occur in surprising places, so don't try to second guess and put in a speed hack until you've proven that's where the bottleneck is.
Rule 2. Measure. Don't tune for speed until you've measured, and even then don't unless one part of the code overwhelms the rest.
Rule 3. Fancy algorithms are slow when n is small, and n is usually small. Fancy algorithms have big constants. Until you know that n is frequently going to be big, don't get fancy. (Even if n does get big, use Rule 2 first.)
Rule 4. Fancy algorithms are buggier than simple ones, and they're much harder to implement. Use simple algorithms as well as simple data structures.
Rule 5. Data dominates. If you've chosen the right data structures and organized things well, the algorithms will almost always be self-evident. Data structures, not algorithms, are central to programming.
Pike's rules 1 and 2 restate Tony Hoare's famous maxim "Prematur