Skip to content

Instantly share code, notes, and snippets.

@sjswitzer
sjswitzer / hh_vs_ht.py
Last active March 21, 2024 02:05
Computes odds of Bob winning in Daniel Litt's diabolical puzzle
# Flip a fair coin 100 times--it gives a sequence of heads (H) and tails (T).
# For each HH in the sequence of flips, Alice gets a point; for each HT,
# Bob does, so e.g. for the sequence THHHT Alice gets 2 points and Bob
# gets 1 point. Who is most likely to win?
# -- https://twitter.com/littmath/status/1769044719034647001
# Based on https://gist.github.com/llllvvuu/02e08ee87b2191c0f3f504072be73a0f#file-hh_vs_ht-c,
# a _very_ clever algorithm by L (llllvvuu). See https://twitter.com/llllvvuu/status/1770557954372382765
# I implemented this to try to understand llllvvuu's algorithm and as a bonus to benefit from
# Python's bignum support.
@sjswitzer
sjswitzer / gist:5e54f4d00dc2ab40468a7fca09e7d675
Created July 18, 2021 05:27
An HTML definition list that works the way you want it to
<html>
<style>
dl.definitions, dl.definitions dt, dl.definitions dd {
display: block;
}
dl.definitions dt {
float: left;
clear: right;
margin-inline-end: 2ch;
}
@sjswitzer
sjswitzer / tuple-utils.cpp
Last active December 30, 2017 23:19
C++ tuple utilities. Shows very simple way to recurse over tuple elements.
#include <iostream>
#include <tuple>
namespace detail {
template <size_t... N> struct Indices { };
template <typename T, size_t I>
inline void print_tuple(std::ostream &out, const T &tup, Indices<0, I>) { }
template <typename T, size_t N, size_t I>
//
// main.cpp
// TreeShared
//
// Created by stan on 12/8/17.
// Copyright © 2017 stan. All rights reserved.
//
#include <iostream>
@sjswitzer
sjswitzer / LinkedList.h
Created December 8, 2015 01:34
Intrusive list templates
/*
* LinkedList.h
*/
#include <assert.h>
template <class C, int N=0>
class Linked
{
Linked* m_next;
@sjswitzer
sjswitzer / llsort.h
Last active August 13, 2021 21:04
Bottom-up natural mergesort in C++
//
// Copyright (c) 2015 Stan Switzer.
//
#ifndef llsort_llsort_h
#define llsort_llsort_h
#include <vector>
#include <functional>
@sjswitzer
sjswitzer / msort.hs
Last active June 12, 2016 22:41
Bottom-up natural mergesort in Haskell
module Msort (msortBy, msort) where
msortBy :: (a -> a -> Ordering) -> [a] -> [a]
msortBy orderOp =
foldr merge [] . foldr mergeStack [] . runs
where
-- mergeStack :: [a] -> [[a]] -> [[a]]
-- mergeStack "k" [ "" "ij" "" "abcdefgh" ] = [ "k" "ij" "" "abcdefgh" ]
-- mergeStack "l" [ "k" "ij" "" "abcdefgh" ] = [ "" "" "ijkl" "abcdefgh" ]
mergeStack x ([]:s) = x:s
@sjswitzer
sjswitzer / quine.js
Created October 31, 2013 00:11
Javascript / Node.js Quine
a="console.log(\"a=\"+JSON.stringify(a)+\";\"+a)";console.log("a="+JSON.stringify(a)+";"+a)
@sjswitzer
sjswitzer / hanoi.py
Created October 30, 2013 21:42
Python generators can't recurse in the conventional sense, but they can do something almost as good: recursively iterate other generators. Here's a classic recursion problem implemented as a generator.
def hanoiGenerator(n, a="left", b="center", c="right"):
if n > 0:
for i in hanoiGenerator(n-1, a, c, b):
yield i
yield (a, c)
for i in hanoiGenerator(n-1, b, a, c):
yield i
def test_hanoiGenerator():
for (fm, to) in hanoiGenerator(3):