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
# 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): |
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
# 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)) |
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 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] |
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
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]) |
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
# 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) |
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
# 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 | |
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
# 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): |
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
# 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 |
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
# 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): |
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
"""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() |
OlderNewer