This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def main(): | |
f = open("twain.txt") | |
text = f.read() | |
f.close() | |
text = text.lower() | |
num_letters = [0] * 26 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |