Skip to content

Instantly share code, notes, and snippets.

@maxrothman
maxrothman / .bash_colors
Last active August 23, 2017 11:46
bash tips & one-liners
# Reset
NoColor='\[\e[m\]' # Text Reset
# Regular Colors
Black='\[\e[0;30m\]' # Black
Red='\[\e[0;31m\]' # Red
Green='\[\e[0;32m\]' # Green
Yellow='\[\e[0;33m\]' # Yellow
Blue='\[\e[0;34m\]' # Blue
Purple='\[\e[0;35m\]' # Purple
@maxrothman
maxrothman / convert.py
Created October 14, 2014 17:03
Parsing the Arisia HTML schedule for conversion into your favorite calendar format
#!/usr/bin/python
#Converts arisia HTML schedule to the following format:
#{name:NAME, start:YYYY-MM-DDTHH:MM:SS.000-05:00, end:(same as start), location:LOCATION, body:TEXT}
#name includes type
import re
from datetime import datetime, timedelta
import dateutil.parser
from sample import main
@maxrothman
maxrothman / ita_word_numbers.py
Created October 14, 2014 17:10
ITA word numbers puzzle solution
'''My solution to the ITA puzzle "Word Numbers"
<http://www.itasoftware.com/careers/puzzle_archive.html>
"If the integers from 1 to 999,999,999 are written as words,
sorted alphabetically, and concatenated, what is the 51 billionth letter?"
To be precise: if the integers from 1 to 999,999,999 are expressed in words
(omitting spaces, 'and', and punctuation[1]), and sorted alphabetically
so that the first six integers are
@maxrothman
maxrothman / formula.jison
Created October 31, 2014 13:56
spreadsheet-like formula jison language spec
/** Parses end executes mathematical expressions with functions mixed in.
* TODO: add referencing symbols elsewhere on the page (via some selector).
**/
%{
var funcs = {
pow: function(a,b) { return Math.pow(a,b); },
test: function(a) { return a*2; }
}
%}
@maxrothman
maxrothman / gist:fbc17ca2c99822af7546
Created January 7, 2015 15:38
Handy Python datatypes
class Board(list):
'''Interprets board[1,2] as board[1][2]'''
def __getitem__(self, key):
try:
cur = self
key = list(key)
while len(key) > 0:
cur = list.__getitem__(cur, key.pop(0))
return cur
@maxrothman
maxrothman / gist:af71ff2cef2a04f69384
Created January 8, 2015 17:41
iTerm2 nightly build integration with Alfred
-- The nightly builds of iTerm have changed its AppleScript API, which breaks Alfred's default iTerm integration.
-- The new API examples are here: https://iterm2.com/applescript.html
-- Discussion about it and support can be found here: https://groups.google.com/forum/#!msg/iterm2-discuss/PcMGixwuceY/81JMZheXb_UJ
--Simply paste this into the box under Alfred Preferences > Terminal/Shell > Application: Custom
on alfred_script(q)
tell application "iTerm"
create window with default profile
tell current session of last terminal window
write text q & "; clear"
@maxrothman
maxrothman / gist:98ba2af4b2f1dd7df93f
Created January 8, 2015 19:09
Open iTerm to current Finder directory automator application
-- Add a button to Finder that opens iTerm to the current directory
-- Inspired by http://peterdowns.com/posts/open-iterm-finder-service.html
-- but updated to use the new iTerm nightly build AppleScript API and OSX 10.9.5 AppleScript
-- Setup (same as above link):
-- - Create a new Automator Application
-- - Add a "Run AppleScript" action
-- - Paste this in and save
-- - with Command-drag the application to the top bar in Finder
on run {input, parameters}
@maxrothman
maxrothman / gist:ddaf202a4604533bf717
Last active August 29, 2015 14:15
Sierpinski triangles
"""
Usage: test.py [l|c|r] <n>
Print a Sierpinski's Gasket of iteration n, left, center, or right-aligned (accordingly)
"""
import string, sys
def sierpinski(n, c):
gasket = [c]
for i in range(1, n+1):
gasket += ['{0}{0:>{1}}'.format(j, 2**i) for j in gasket]
@maxrothman
maxrothman / chopsticks.py
Last active August 29, 2015 14:19
chopsticks solution
"""
Solution for the game chopsticks (http://en.wikipedia.org/wiki/Chopsticks_(hand_game)) with even splitting only
Requrements: collections-extended
Future work:
- find subset of tree where the game halts
- remove "dumb moves", i.e. moves where a player COULD win but doesn't
- investigate deterministic lines, e.g. when the "only intelligent" move leads on a deterministic path
- investigate avoiding "dumb moves" with lookahead
"""
@maxrothman
maxrothman / rename-replset.yml
Created October 28, 2015 20:02
An Ansible playbook that changes the name of a mongo replication set
# Renames a mongo replication set
# This necessarily resyncs the secondaries, taking them offline, but total downtime on the primary should be short.
#
# Usage:
# ansible-playbook rename_replset.yml -e 'password=SuperSecretAdminPassword name=new_replset_name' -i host1,host2,host3
#
# Note: the first host in the specified inventory will become the new primary
- hosts: all
vars: