Skip to content

Instantly share code, notes, and snippets.

View textbook's full-sized avatar

Jonathan Sharpe textbook

View GitHub Profile
@textbook
textbook / ProblemSet4.py
Created October 23, 2012 22:08
MIT 6.00x - PSet 4 answers by jonrsharpe
# 1. Word Scores
def getWordScore(word, n):
return (len(word) * sum(SCRABBLE_LETTER_VALUES[x] for x in word)) + (50 if len(word) == n else 0)
# Test implementation
def getFrequencyDict(aStr):
return dict((letter, aStr.count(letter)) for letter in aStr)
# 2. Dealing with hands
def updateHand(hand, word):
@textbook
textbook / ProblemSet2.py
Created November 1, 2012 13:14
MIT 6.00x - PSet 2 answers by jonrsharpe
# 1. Paying the minimum
totalPaid = 0
for month in range(12):
print("Month: {0:d}".format(month + 1))
minPayment = balance * monthlyPaymentRate
totalPaid += minPayment
print("Minimum monthly payment: {0:.2f}".format(minPayment))
balance -= minPayment
balance *= (1 + (annualInterestRate / 12))
print("Remaining balance: {0:.2f}".format(balance))
@textbook
textbook / ProblemSet3.py
Created November 1, 2012 13:16
MIT 6.00x - PSet 3 answers by jonrsharpe
import string
# 1. Polynomials
def evaluatePoly(poly, x):
return float(sum(b * (x ** a) for a,b in enumerate(poly)))
# 2. Derivatives
def computeDeriv(poly):
return [float (a * b) for a, b in enumerate(poly)][1:] or [0.0]
@textbook
textbook / ProblemSet5.py
Created November 9, 2012 11:42
MIT 6.00x - PSet 5 answers by jonrsharpe
from string import ascii_lowercase as lower, ascii_uppercase as upper, ascii_letters as both
# 1. Build a coder
def buildCoder(shift):
return dict(zip(both, (lower[shift:] + lower[:shift] + upper[shift:] + upper[:shift])))
# 2. Apply the coder
def applyCoder(text, coder):
return "".join([coder.get(letter, letter) for letter in text])
@textbook
textbook / MidTerm1.py
Created November 9, 2012 11:50
MIT 6.00x - MidTerm1 answers by jonrsharpe
# 3. Iterative/recursive log function
def myLog(x, b):
a = 0
while b ** a <= x:
a += 1
return a - 1
# 4. Interlace two strings iteratively
def laceStrings(s1, s2):
shorter, longer = sorted([s1, s2], key=len)
@textbook
textbook / ProblemSet6.py
Created November 15, 2012 11:02
MIT 6.00x - PSet 6 answers by jonrsharpe
# 1. NewsStory
class NewsStory(object):
def __init__(self, guid, title, subject, summary, link):
self.guid = guid
self.title = title
self.subject = subject
self.summary = summary
self.link = link
@textbook
textbook / ProblemSet7.py
Created November 27, 2012 11:08
MIT 6.00x - PSet 7 answers by jonrsharpe
# 1. RectangularRoom
class RectangularRoom(object):
def __init__(self, width, height):
self.width, self.height = width, height
self.tiles = dict(((x, y), 0) for x in range(width) for y in range(height))
def cleanTileAtPosition(self, pos):
self.tiles[(int(pos.x), int(pos.y))] += 1
def isTileCleaned(self, m, n):
@textbook
textbook / ProblemSet8.py
Created December 13, 2012 09:49
MIT 6.00x - PSet 8 answers by jonrsharpe
# Problem 2: SimpleVirus and Patient
class SimpleVirus(object):
def __init__(self, maxBirthProb, clearProb):
self.maxBirthProb = maxBirthProb
self.clearProb = clearProb
def getMaxBirthProb(self):
return self.maxBirthProb
@textbook
textbook / MidTerm2.py
Created December 13, 2012 09:58
MIT 6.00x - MidTerm2 answers by jonrsharpe
# Problem 4-2
class edXPerson(Person):
nextIdNum = 0
def __init__(self, name):
Person.__init__(self, name)
self.idNum = edXPerson.nextIdNum
edXPerson.nextIdNum += 1
def getIdNum(self):
return self.idNum
def __lt__(self, other):
@textbook
textbook / testing.py
Last active December 27, 2015 01:59
Collection of useful bits and pieces of Python code, plus a few simple test routines - now pylint compliant
"""Simple unit testing functionality."""
from __future__ import print_function
def _run_function_test(function, expected, args=None, kwargs=None):
"""Check whether function returns/raises expected with supplied args."""
if args is None:
args = tuple()
if kwargs is None:
kwargs = dict()