Skip to content

Instantly share code, notes, and snippets.

View thomasballinger's full-sized avatar

Tom Ballinger thomasballinger

View GitHub Profile
@thomasballinger
thomasballinger / ttt.py
Created January 23, 2014 00:17
Examples of Python syntax
"""An immutable Tic Tac Toe board and minimax ai"""
import itertools
class Board(object):
"""Immutable Tic Tac Toe board
>>> Board().rows
((' ', ' ', ' '), (' ', ' ', ' '), (' ', ' ', ' '))
>>> print Board()
| |
@thomasballinger
thomasballinger / opt.py
Created December 23, 2013 23:27
bpython pastebin script: uploads to Online Python Tutor
#!/usr/bin/env python
# Add the line
# pastebin_helper = opt.py
# to .bpython/config
# under a section called
# [general]
# to enable
import sys
import urllib
@thomasballinger
thomasballinger / created_at.rb
Created December 19, 2013 17:04
Add "created_at" to all ruby objects
class Class
alias :create :new
@@objects = []
def new(*a, &b)
obj = allocate
if obj.class == String
nil
elsif obj.class == Time
@thomasballinger
thomasballinger / subprocess.py
Created December 15, 2013 23:26
Using a pseudo-terminal to interact with interactive Python in a subprocess
from subprocess import Popen, PIPE
import pty
import os
from select import select
import sys
import tty
master, slave = pty.openpty()
p = Popen(['python'], stdin=slave, stdout=PIPE, stderr=PIPE)
pin = os.fdopen(master, 'w')
def combs(array, n)
if array.length < n
[]
elsif n == 0
[[]]
else
(combs(array[1..-1], n-1).map{|comb| [array[0]] + comb} +
combs(array[1..-1], n))
end
end

Practice for Job Fair / Thanksgiving

This is the last job prep day before two important interrogative events: a job fair December 5th, and American Thanksgiving, November 28. We're going to practice responding to questions in pairs. Explicitly state who you are before starting to ask questions - you'd say different things to your uncle over mashed potatoes than you would to an engineer at the job fair from Etsy.

We'll also share solutions to interview questions / programming problems we're finding tough - I want to check in with some people on recursion in particular.

Here's a script to choose questions from:

  • What's Hacker School? (Try telling this to a skeptical interviewer - it's not your job to defend Hacker School! Just to tell your story)
  • What language did they teach you there?
import poplib
USERNAME = '[email protected]'
PASSWORD = 'nevergraduate'
M = poplib.POP3_SSL('pop.gmail.com')
M.user(USERNAME)
M.pass_(PASSWORD)
numMessages = len(M.list()[1])
for i in range(numMessages):
google_homepage = lambda: [
globals().__setitem__('s', __import__('socket').socket()),
globals()['s'].connect(('www.google.com', 80)),
globals()['s'].send('GET /\r\n\r\n'),
''.join(globals()['s'].recv(1000000) for _ in range(100))
][-1]
print google_homepage()
#!/usr/bin/env python
import sys
import urllib2
import json
def do_gist_json(s):
""" Use json to post to github. """
gist_public = False
gist_url = 'https://api.github.com/gists'
# This is a standard python config file
# Valid values can be True, False, integer numbers, strings
# By default bpython will look for ~/.config/bpython/config or you
# can specify a file with the --config option on the command line
# General section tag
[general]
# Display the autocomplete list as you type (default: True).
# When this is off, you can hit tab to see the suggestions.