Skip to content

Instantly share code, notes, and snippets.

@betaveros
betaveros / gist:11311438
Last active August 29, 2015 14:00
Google Code Jam Round 1A 2014 problem C: Proper Shuffle. Testing on my machine correctly identified 88.6% of 1000 good shuffles and 98.1% of 1000 bad shuffles. I don't know why...
-- @betaveros
-- Google Code Jam Round 1A 2014: Proper Shuffle (polished version)
import Control.Applicative
import Control.Monad
import Text.Printf
inputInt :: IO Int
inputInt = read <$> getLine
inputInts :: IO [Int]
inputInts = map read . words <$> getLine
@betaveros
betaveros / code-tags
Created October 28, 2014 11:55
Sample BBCode syntax-highlighted code snippets
CSS Tag
[css]body #container .hello:before {
content: "Hello, world!";
border: 10px solid black;
}[/css]
C Tag
[c]#include <stdio.h>
int main(void) {
if (2 + 2 == 4) {
import java.awt.image.BufferedImage
import java.io.File
import java.awt.Color
import javax.imageio.ImageIO
object ImageTabler {
var classMap: Map[Int, Int] = Map()
var nextClass: Int = 1
def addClassFor(c: Int): Unit = if (!(classMap contains c)) {
var cls = nextClass
set ruler
set showcmd
set hlsearch
set incsearch
set ignorecase
set smartcase
set autoindent
filetype plugin indent on
let mapleader = ","
@betaveros
betaveros / zipmagic.py
Created May 17, 2015 10:19
magically fix (most of) this zip with corrupted filenames I got
import codecs
import os
import shutil
import zipfile
def magic(fname):
if type(fname) == unicode: return fname
assert type(fname) == str
return codecs.decode(fname, 'shift-jis', 'replace')
@betaveros
betaveros / underblank.sty
Created July 22, 2015 05:04
I wrote this in... December 2009? What the hell?
% underblank.sty: Blanks for tests and study guides and all that multiverse cr&p
% to get the answers, declare a command \underblankanswers to anything.
\ProvidesPackage{underblank}
\usepackage{ulem}
\usepackage{ifthen} % need 2 versions: w/, w/o answers
\normalem
\newlength{\answerwidth}
\newcommand{\emptyline}{\vspace{\baselineskip}}
\newcommand{\underblank}[2]{\settowidth{\answerwidth}{#1}\uline{\makebox[\answerwidth][c]{#2}}}
@betaveros
betaveros / digraph.py
Created December 4, 2015 00:49
sketchy sketchy digraphs
#!/usr/bin/env python
# nontrivial parts come from
# http://paste.pound-python.org/raw/6aCbTG3lKgCWdrRygkBG/
# tmux magic incantation:
# bind-key v command-prompt -p "Enter digraph:" "set-buffer \"%%\"; save-buffer /tmp/tmux-digraph-input; delete-buffer; if-shell 'digraph.py < /tmp/tmux-digraph-input > /tmp/tmux-digraph-output' 'load-buffer /tmp/tmux-digraph-output; paste-buffer -d; run-shell \"rm /tmp/tmux-digraph-output /tmp/tmux-digraph-input\"' 'display-message \"Error in digraph lookup\"'"
import logging
import os.path
import sys
import unicodedata
@betaveros
betaveros / atomicweights.py
Last active December 16, 2015 10:59
Atomic weights in Python variables and a nearly-trivial REPL, for weird people like me who decide to do high-school chemistry homework using Python as a calculator.
from __future__ import division, print_function
H=1.008; He=4.002602;
Li=6.94; Be=9.012182; B=10.81; C=12.011; N=14.007; O=15.999; F=18.9984032;Ne=20.1797;
Na=22.98976928;Mg=24.3050; Al=26.9815386;Si=28.085; P=30.973762; S=32.06; Cl=35.45; Ar=39.948;
K=39.0983; Ca=40.078; Sc=44.955912;Ti=47.867;V=50.9415; Cr=51.9961;Mn=54.938045;Fe=55.845; Co=58.933195;Ni=58.6934;Cu=63.546; Zn=65.38; Ga=69.723; Ge=72.63; As=74.92160; Se=78.96; Br=79.904; Kr=83.798;
Rb=85.4678; Sr=87.62; Y=88.90585; Zr=91.224;Nb=92.90638; Mo=95.96; Tc=98.0; Ru=101.07; Rh=1
@betaveros
betaveros / troll.java
Created May 7, 2013 12:08
An experiment in breaking certain "easy" CS questions about whether an expression can evaluate to "true". (Note that you can get "a != a" to be true without messing with threads (Java Puzzlers, Puzzle 29; left as an exercise for the reader), but this method is far more general; you can get things like "a < 0 && a > 0" to be true, but only someti…
public class Troll {
static volatile int a;
public static void main(String[] args) {
new Thread(new Runnable(){
public void run(){
while (true){ a = -42; a = 1337; }
}
}).start();
for (int i = 0; i < 1000000; i++){
if (a != a) {