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 / correlation.py
Created November 1, 2012 16:12
Implementation of pearson correlation
#!/usr/bin/env python
from math import sqrt
def pearson(pairs):
# Takes in a list of pairwise ratings and produces a pearson similarity
series_1 = [float(pair[0]) for pair in pairs]
series_2 = [float(pair[1]) for pair in pairs]
sum1 = sum(series_1)
sum2 = sum(series_2)
@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":
Meringue:shenanigans chriszf$ python test.py
fish
this is a fish
s/"fish"/"squid"/g
squid
this is a squid
@chriszf
chriszf / gist:7435390
Created November 12, 2013 17:48
"Python needs to be more like vim" -- nobody anywhere, ever
Meringue:shenanigans chriszf$ python
Python 2.7.1 (r271:86832, Jun 25 2011, 05:09:01)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import lib.vim
>>> some_str = "DevBeat is pretty alright"
>>> s/"alright"/"awesome"/some_str
'DevBeat is pretty awesome'
>>>
@chriszf
chriszf / gist:7427457
Created November 12, 2013 08:25
I thought python could use some new syntax...
mac3:shenanigans czf$ cat 031_switchcase2.py
import lib.switchcase
def one():
print "One branch executed"
def two():
print "Two branch executed"
def three():
import lib.switchcase
def one():
print "One branch executed"
def two():
print "Two branch executed"
def three():
print "Three branch executed"
def cross(A, B):
"Cross product of elements in A and elements in B."
return [a+b for a in A for b in B]
digits = '123456789'
rows = 'ABCDEFGHI'
cols = digits
squares = cross(rows, cols)
unitlist = ([cross(rows, c) for c in cols] +
[cross(r, cols) for r in rows] +
class case(object):
EMPTY_CASE = (None for x in [1])
def __init__(self, *args):
self.cases = args
self.used = False
def __iter__(self):
return self
def next(self):
@chriszf
chriszf / ex4
Created October 1, 2013 21:52
Exercise solutions
def custom_len(input_list):
"""custom_len(input_list) imitates len(input_list)"""
count = 0
for item in input_list:
count += 1
return count
def custom_remove(input_list, value):
"""custom_remove(input_list, value) imitates input_list.remove(value)"""
for i in range(custom_len(input_list)):