Skip to content

Instantly share code, notes, and snippets.

View dimo414's full-sized avatar
🦀
Playing with Rust

Michael Diamond dimo414

🦀
Playing with Rust
View GitHub Profile
@dimo414
dimo414 / bprefix.py
Created June 24, 2012 02:09
Testing Prefix Lookup Data Structures
import bisect
import error;
class Prefix:
'''
A prefix data structure built on a sorted list, which uses binary search.
Note that for more reasonable lookup, it only searches in lower
case. This means there can be colliding strings such as 'prefix' vs 'Prefix'.
In this case, more recent inserts override older ones.
@dimo414
dimo414 / cat.py
Created June 26, 2012 04:54
Fast File Concatenation in Python
'''
Tests different methods of concatenating files in Python.
'''
from __future__ import print_function
import json,os,shutil,subprocess
import util
def verify(file,expected):
count = 0
@dimo414
dimo414 / includes-common.inc.php
Last active November 11, 2016 19:28
Sample PHP website structure, demonstrating environment abstraction
<?php
/*
This file handles all necessary setup that has to happen on each page load.
It should have no need to be aware of where the server is running from.
*/
session_start();
// This file isn't tracked by version control, and might not exist
import java.io.*;
import java.util.*;
import com.google.common.collect.*;
public class WordFreq {
public static void main(String[] args) throws FileNotFoundException {
final HashMultiset<String> counts = HashMultiset.create(
ImmutableList.copyOf(new Scanner(new File(args[0]))));
@dimo414
dimo414 / GCChurn.java
Created March 26, 2013 04:34
GC Churn Java application, intended to cause high amounts of garbage collection for benchmarking
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Runnable which causes a heavy yet stable amount of GC; useful for
* benchmarking applications under load.
*
@dimo414
dimo414 / index.php
Created April 29, 2013 21:19
How To Identify The Requested Page In PHP
<code><pre>
<?php
echo "__FILE__: ".__FILE__."\n";
echo "PHP_SELF: ".$_SERVER['PHP_SELF']."\n";
echo "SCRIPT_NAME: ".$_SERVER['SCRIPT_NAME']."\n";
echo "REQUEST_URI: ".$_SERVER['REQUEST_URI']."\n";
echo "parse_url(REQUEST_URI): ".parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH)."\n";
?>
@dimo414
dimo414 / classes-template.class.php
Created November 16, 2014 20:42
Example code for creating a flexible PHP template with shared header and footer behavior.
<?php
/*
Enables a PHP page to abstract out the site design (the template) from
the page design (the content).
Generally, a page will have a common header and footer (sidebars and the
like would live in one or the other). Following this pattern, a new page
can be created (after including the necessary files of course)) like so:
@dimo414
dimo414 / description.md
Created March 4, 2020 06:06
St. Petersburg Paradox Simulation

The St. Petersburg paradox, in a nutshell, is: why would you refuse to play a game with an unlimited expected payout? Shouldn't you be willing to pay any finite price to play, since your expected return is infinite?

This simulation explores what the Stanford Encyclopedia refers to as the "Average Win" explanation, that:

Expected value is in effect average payback in the long run.... [I]f you're going to play St. Petersburg with a substantial fee per game, you're likely to have to play a very very long time before you come out with a positive net payoff. Ordinary practical considerations thus apply... You may have to play a very long time before winning once. But worse: in a series of losses, the amount needed for the next bet rises. How much bankroll would you need to guarantee success...? Whatever bankroll you take into the casino, there's a chance that this will not be enough.

The

@dimo414
dimo414 / MapSpeedTest.java
Created March 4, 2020 06:22
Benchmark of map implementation access speeds
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
@dimo414
dimo414 / default_detect.py
Created March 5, 2020 05:00
Detecting whether an argparse argument was set explicitly or not
class DefaultCapturingNamespace(argparse.Namespace):
"""An argparse Namespace that tracks the number of times the given argument
has been set. In theory this can allow you to distinguish between values
set by argparse (e.g. default values) and those specified by the user.
"""
def __init__(self, **kwargs):
type_name = type(self).__name__
argparse.Namespace.__setattr__(self, '_%s__original_values' % type_name, {})
argparse.Namespace.__setattr__(self, '_%s__set_counts' % type_name, {})