Skip to content

Instantly share code, notes, and snippets.

@cosinekitty
cosinekitty / run.bash
Created February 25, 2020 01:42
Bash script for creating the Mandelbrot Set zoom movie.
#!/bin/bash
Fail()
{
echo "FATAL($0): $1"
exit 1
}
# Compile the source code for mandelzoom. Optimize for speed.
echo "Building C++ code..."
g++ -Wall -Werror -Ofast -o mandelzoom mandelzoom.cpp lodepng.cpp || Fail "Error building C++ code."
@cosinekitty
cosinekitty / covidcurve.py
Last active March 26, 2020 00:51
Create best fit model of COVID-19 cases in the USA.
#!/usr/bin/env python3
import sys
import os
import re
import requests
import math
from scipy import stats
import datetime
DataUrl = 'https://raw.githubusercontent.com/datasets/covid-19/master/data/key-countries-pivoted.csv'
@cosinekitty
cosinekitty / tally1.py
Created April 24, 2020 19:58
Simple Tally function
def Tally(text):
count = {}
for c in text:
count[c] = 1 + count.get(c, 0)
return count
print(Tally('aaabbaccaaa')) # prints {'b': 2, 'c': 2, 'a': 7}
@cosinekitty
cosinekitty / tally2.py
Last active April 25, 2020 00:15
Tally function with count dictionary argument
def Tally(text, count):
for c in text:
count[c] = 1 + count.get(c, 0)
return count
c = {}
Tally('aaabbaccaaa', c)
Tally('xyzaa', c)
Tally('qrs', c)
print(c)
@cosinekitty
cosinekitty / tally3.py
Created April 24, 2020 20:19
The more flexible Tally function can still process strings independently.
print(Tally('abc', {})) # {'a': 1, 'c': 1, 'b': 1}
print(Tally('def', {})) # {'e': 1, 'd': 1, 'f': 1}
print(Tally('xyz', {})) # {'x': 1, 'y': 1, 'z': 1}
@cosinekitty
cosinekitty / tally4.py
Last active April 25, 2020 01:05
Tally function with naive default dictionary argument.
# WARNING: This code contains a subtle bug!
def Tally(text, count = {}):
for c in text:
count[c] = 1 + count.get(c, 0)
return count
@cosinekitty
cosinekitty / tally5.py
Created April 24, 2020 20:45
Illustration of the problem with Python default arguments.
print(Tally('abc'))
# {'c': 1, 'b': 1, 'a': 1}
print(Tally('def'))
# {'c': 1, 'f': 1, 'b': 1, 'd': 1, 'a': 1, 'e': 1}
print(Tally('xyz'))
# {'c': 1, 'f': 1, 'x': 1, 'b': 1, 'd': 1, 'z': 1, 'a': 1, 'e': 1, 'y': 1}
@cosinekitty
cosinekitty / tally6.py
Created April 24, 2020 21:05
Illustration of why the Tally function's use of a default argument is broken.
GlobalCount = {}
def Tally(text, count = GlobalCount):
for c in text:
count[c] = 1 + count.get(c, 0)
return count
@cosinekitty
cosinekitty / tally7.py
Created April 24, 2020 21:38
The right way to create a mutable default function argument.
def Tally(text, count = None):
if count is None:
# Create a brand new, empty dictionary!
count = {}
for c in text:
count[c] = 1 + count.get(c, 0)
return count
print(Tally('abc'))
# {'a': 1, 'c': 1, 'b': 1}
@cosinekitty
cosinekitty / squash_loop.py
Created April 26, 2020 22:05
Main loop of Compression Game test harness
bestSize = None
bestSourceCode = None
for algorithm in AlgorithmList:
compressedDataCode = algorithm.Compress(words)
stubFileName = os.path.join(StubDirName, algorithm.Name() + '.py')
with open(stubFileName, 'rt') as infile:
stubCode = infile.read()
targetFileName = os.path.join(OutputDirName, algorithm.Name() + '.py')
code = CommonHeadCode + compressedDataCode + stubCode + CommonTailCode
with open(targetFileName, 'wt') as outfile: