Skip to content

Instantly share code, notes, and snippets.

@chriszf
chriszf / Exercise05
Created March 9, 2013 20:13
Weekend Info
Exercise 05: Files, Creative uses of lists
==========================================
Introduction
------------
Files can be opened using the .open method, and then processed one character at a time as a bytestream using the .read method with an argument of 1:
f = open("myfile.txt")
firstletter = f.read(1)
f.close()
@chriszf
chriszf / gist:5121546
Created March 9, 2013 00:07
letter count
def count(filename):
f = open(filename)
text = f.read()
text.lower()
letters = [0] * 26
for letter in text:
index = ord(letter) - ord("a")
if index >= 0 and index < 26:
@chriszf
chriszf / Exercise 2
Created March 6, 2013 04:33
Tomorrow's Exercises
Exercise 02: Math and functions
=======
Introduction
--------
We're taught mathematical notation using 'infix' notation, that is, the operator goes between the numbers:
eg: In '3 + 2', the operator, '+', goes between the operands, 3 and 2.
An alternate notation for math is called 'prefix'. As you might have guessed, in prefix notation, the operator goes in front of the operands:
@chriszf
chriszf / gist:5057478
Created February 28, 2013 15:19
Arduino pin reading nonsense
#define XPIN 2
#define YPIN 1
#define ZPIN 0
#define MAX_POINTS 48
#define ACTUAL_POINTS 16
unsigned long readTimer = 0;
unsigned long reportTimer = 0;
unsigned long lastTimer = 0;
@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 / gist:3860594
Created October 9, 2012 18:37
Exercise 09 Solution
def main():
f = open("twain.txt")
text = f.read()
f.close()
text = text.lower()
num_letters = [0] * 26
@chriszf
chriszf / gist:3860588
Created October 9, 2012 18:35
Challenge 1 Solution
import os
import shutil
def make_dir(name):
raise Exception()
if not os.path.exists("target /" + name):
os.mkdir("target /" + name)
def main():
file_list = os.listdir("./files")
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)
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 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)