Skip to content

Instantly share code, notes, and snippets.

@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) {
@betaveros
betaveros / obfuscatedprimes.hs
Last active December 17, 2015 17:49
An "efficient" prime generator with the Sieve of Eratosthenes in Haskell, as close to point-free (= unreadable) as I could make it.
import Control.Applicative (liftA2, liftA3)
import qualified Data.Map as M (empty, lookup, insertWith, delete)
import Data.Function (fix)
import Control.Monad (join)
primes = fix (flip (flip liftA2 head) tail . (liftA2 (liftA3 liftA2 (flip flip
. M.lookup)) (liftA2 ($) ((.) . (.) (liftA3 maybe) . (.) . (:)) . ((. flip
(.) . liftA2 (M.insertWith (++)) (join (*)) (replicate 1)) . flip (.)))
(((. flip (.) . liftA2 (.) (foldl . ((. flip flip) . flip flip (replicate
1) . flip liftA2 . (.) (M.insertWith (++)) . (+))) M.delete) . (flip (.))
. (.) (.) ) ))) [2..] M.empty
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CurrencyExchange { // I'm not an actionListener!
String[] differentCurrency = {"US Dollars","Japanese YEN","Chinese YUAN", "Korean WON"};
JFrame frame;
JPanel contentPane;
JLabel label, result;
@betaveros
betaveros / rotated.hs
Created June 1, 2013 03:36
find equations like (a + b + c = d) which are still true after 180-degree rotation
import Control.Applicative
import Data.Maybe
-- Calculator digit rotation
cRotateDigit 0 = Just 0
cRotateDigit 1 = Just 1
cRotateDigit 2 = Just 2
cRotateDigit 5 = Just 5
cRotateDigit 6 = Just 9
cRotateDigit 8 = Just 8
cRotateDigit 9 = Just 6
hello
hello
hello
hello
hello
hello
@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