Skip to content

Instantly share code, notes, and snippets.

@chriszf
chriszf / gist:2342247
Created April 9, 2012 08:07
FizzBuzz without conditionals
# SCREW YOU CONDITIONALS
def fizzbuzz(n):
fizzes = [1, 0, 0]
buzzes = [2, 0, 0, 0, 0]
words = [None, "Fizz", "Buzz", "FizzBuzz"]
for i in range(1, n):
words[0] = i
print(words[fizzes[i%3] + buzzes[i%5]])
@chriszf
chriszf / gist:2342280
Created April 9, 2012 08:21
A simple conditional
# Hypothetical win condition for a hangman game called 'words with fiends'
if guess == answer:
print("You guessed the right answer! May all your children be blessed from here until eternity.")
else:
print("Words fail me, as they have clearly failed you. Begone from my sight.")
@chriszf
chriszf / gist:2344561
Created April 9, 2012 16:28
Every academic program ever
analysis_mode = get_mode_from_input()
if analysis_mode == 1:
# Do Pearson correlation
data = load_data()
elif analysis_mode == 2:
# Do Spearman rank
data = load_data()
elif analysis_mode == 3:
# Do a literature correlation lookup
@chriszf
chriszf / twipy.py
Created June 27, 2012 16:52
Sample command line twitter client
import tweepy
import sys
while True:
line = raw_input("> ")
tokens = line.split()
if not tokens:
continue
command = tokens[0]
@chriszf
chriszf / flask_sample.py
Created June 27, 2012 16:54
Sample flask app
from flask import Flask, request, session, g, redirect, url_for, \
abort, render_template, flash
import tweepy
SECRET_KEY = 'development key'
DEBUG = True
app = Flask(__name__)
app.config.from_object(__name__)
@chriszf
chriszf / game.py
Created June 28, 2012 17:38
Game sample
import sys
import random
import puppies
class CentralCorridor(object):
def play(self):
print "Hey guys this is the central corridor. Neat, huh?"
your_input = raw_input("> ")
if your_input == "death":
@chriszf
chriszf / multipart_hack.py
Created July 22, 2012 07:56
Multipart hack to passthrough uploads on GAE to abbyysdk
class MultipartPostHandler(urllib2.BaseHandler):
handler_order = urllib2.HTTPHandler.handler_order - 10 # needs to run first
def http_request(self, request):
data = request.get_data()
if data is not None and type(data) != str:
v_files = []
v_vars = []
try:
def format(filename, class_type):
"""Creates list of names from file ordered by name length"""
f = open(filename)
file_list = f.read().split()
f.close()
person_list = []
for person in file_list:
person_list.append(class_type)
person_list.sort(key = lambda p: p.length)
class Person(object):
@classmethod
def format(cls, filename):
f = open(filename)
file_list = f.read().split()
f.close()
person_list = []
for person in file_list:
person_list.append(cls(person))
person_list.sort(key = lambda p: p.length)
def match(m, p):
"""Matches each player with their optimal mentor pair"""
pairs = []
total_score = 0
while m:
mentor = m.pop()
player = p.pop()
score = mentor.score(player)
total_score += score
pair = Pair(player, mentor, score)