Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env python3
""" Binary search tree with method to invert in-place. """
class Node(object):
def __init__(self, x):
self.value = x
self.left = None
self.right = None
@dslaw
dslaw / btree.cpp
Created May 29, 2016 21:17
Invert binary tree
#include "btree.h"
template <typename T>
void Btree<T>::insert(const T &x) {
if (root == nullptr) {
node_ptr node = std::make_unique<Node>(x);
root = std::move(node);
return;
}
def chunk(xs, n):
""" Split an iterable into chunks.
Parameters
----------
xs : iterable
Iterable to be split.
n : int
Chunk size.
@dslaw
dslaw / lru_cache.py
Created January 31, 2018 05:44
Example of LRU Cache in Python3 using OrderedDict.
from collections import OrderedDict
class LRUCache(object):
def __init__(self, max_size=4):
if max_size <= 0:
raise ValueError
self.max_size = max_size
self._items = OrderedDict()
@dslaw
dslaw / plot_state_seq.py
Last active February 26, 2018 00:29
Plot state sequence with generative distributions
from scipy.stats import norm
import numpy as np
import matplotlib.pyplot as plt
plt.style.use("ggplot")
def save_and_close(fig, filename):
@dslaw
dslaw / plot_mixture.py
Last active February 26, 2018 00:26
Plot mixture of univariate distributions
from scipy.stats import norm
import matplotlib.pyplot as plt
import numpy as np
plt.style.use("ggplot")
# Simulate from a Univariate Gaussian Mixture Model.
@dslaw
dslaw / .skhdrc
Last active October 22, 2021 14:47
Setup for macOS
# open terminal
cmd - return : osascript -e 'tell application "iTerm" to create window with default profile'
# enter fullscreen mode for the focused container
# cmd + f clobbers text search in firefox.
shift + cmd - f : yabai -m window --toggle zoom-fullscreen
# kill focused window
shift + cmd - q : yabai -m window --close
@dslaw
dslaw / data_element_numbers.py
Last active October 23, 2018 22:22
Extract data element numbers for Univ of Oregon solar data
"""Parse data element numbers as trees."""
# XXX: Second spectral digit is broken - both values are 0!
from dataclasses import dataclass
from lxml import html
from typing import Any, Dict, List
DELIMITER = "\x97" # &#151;
MIN_COLSPAN = 1
@dslaw
dslaw / example.py
Last active April 16, 2019 20:23
Example demonstrating how to inspect canonical HTTP requests to S3
import boto3
s3_client = boto3.client("s3")
# List available operations.
s3_client._service_model.operation_names
operation_name = "ListObjectsV2"
params = {
@dslaw
dslaw / spark-misc.md
Last active June 24, 2019 16:45
Miscellaneous tidbits on working with (Py)Spark

Add packages to the Spark session from within python. They'll be automatically downloaded if they're not already available.

# Using `JsonSerDe` as an example.
os.environ["PYSPARK_SUBMIT_ARGS"] = (
   '--packages "org.apache.hive.hcatalog.data.JsonSerDe" pyspark-shell'
)
spark = SparkSession.builder.config().getOrCreate()