About Me
No description provided.
Classes
|
Class status: Established
Role:
Student
|
.
29% complete
|
Submitted Assignments
 |
MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 7, HW 1
# Problem Set 4
# Name: Matt Coley
# Collaborators:
# Time:
#
# Problem 1
#
def nestEggFixed(salary, save, growthRate, years):
"""
- salary: the amount of money you make each year.
- save: the percent of your salary to save in the investment account each
year (an integer between 0 and 100).
- growthRate: the annual percent increase in your investment account (an
integer between 0 and 100).
- years: the number of years to work.
- return: a list whose values are the size of your retirement account at
the end of each year.
"""
fundGrowth = []
for currYear in range(years):
if currYear == 0:
fundGrowth.append(salary * save * 0.01)
else:
fundGrowth.append(fundGrowth[currYear-1] * (1 + 0.01 * growthRate) + salary * save * 0.01)
return fundGrowth
#
# Problem 2
#
def nestEggVariable(salary, save, growthRates):
"""
- salary: the amount of money you make each year.
- save: the percent of your salary to save in the investment account each
year (an integer between 0 and 100).
- growthRate: a list of the annual percent increases in your investment
account (integers between 0 and 100).
- return: a list of your retirement account value at the end of each year.
"""
fundGrowth = []
for currYear in range(len(growthRates)):
if currYear == 0:
fundGrowth.append(salary * save * 0.01)
else:
fundGrowth.append(fundGrowth[currYear-1] * (1 + 0.01 * growthRates[currYear]) + salary * save * 0.01)
return fundGrowth
#
# Problem 3
#
def postRetirement(savings, growthRates, expenses):
"""
- savings: the initial amount of money in your savings account.
- growthRate: a list of the annual percent increases in your investment
account (an integer between 0 and 100).
- expenses: the amount of money you plan to spend each year during
retirement.
- return: a list of your retirement account value at the end of each year.
"""
retireFund = []
for currYear in range(len(growthRates)):
if currYear == 0:
retireFund.append(savings * (1 + 0.01 * growthRates[currYear]) - expenses)
else:
retireFund.append(retireFund[currYear-1] * (1 + 0.01 * growthRates[currYear]) - expenses)
return retireFund
#
# Problem 4
#
def findMaxExpenses(salary, save, preRetireGrowthRates, postRetireGrowthRates,
epsilon):
"""
- salary: the amount of money you make each year.
- save: the percent of your salary to save in the investment account each
year (an integer between 0 and 100).
- preRetireGrowthRates: a list of annual growth percentages on investments
while you are still working.
- postRetireGrowthRates: a list of annual growth percentages on investments
while you are retired.
- epsilon: an upper bound on the absolute value of the amount remaining in
the investment fund at the end of retirement.
"""
savings = nestEggVariable(salary, save, preRetireGrowthRates)[len(preRetireGrowthRates)-1]
low = 0
high = savings
guess = (low + high)/2.0
ctr = 1
finalSavings = postRetirement(savings, postRetireGrowthRates, guess)[len(postRetireGrowthRates)-1]
while abs(finalSavings) > epsilon and ctr <= 100:
#print 'finalSavings:', finalSavings, ' guess:', guess, ' high:', high, ' low:', low
if finalSavings < 0:
high = guess
else:
low = guess
guess = (low + high)/2
finalSavings = postRetirement(savings, postRetireGrowthRates, guess)[len(postRetireGrowthRates)-1]
ctr += 1
return guess
dimebucker
1 year ago
|
 |
MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 5, HW 1
# Problem Set 3a
# Name: Matt Coley
#
from string import *
def countSubStringMatch(target, key):
"""search for the number of matches of key in target"""
matchCnt = 0
lastMatchLoc = 0
matchChk = 1
while matchChk:
currMatchLoc = find(target, key, lastMatchLoc)
if currMatchLoc == -1:
matchChk = 0
else:
matchCnt += 1
lastMatchLoc = currMatchLoc + 1
return matchCnt
def countSubStringMatchRecursive(target, key):
"""search for the number of matches of key in target"""
currMatchLoc = find(target, key)
if currMatchLoc == -1:
return 0
else:
return 1 + countSubStringMatchRecursive(target[currMatchLoc+1:], key)
# Problem Set 3b
# Name: Matt Coley
#
from string import *
def subStringMatchExact(target, key):
"""search for all locations of key in target"""
matchLst = []
lastMatchLoc = 0
matchChk = 1
while matchChk:
currMatchLoc = find(target, key, lastMatchLoc)
if currMatchLoc == -1:
matchChk = 0
else:
matchLst.append(currMatchLoc)
lastMatchLoc = currMatchLoc + 1
return tuple(matchLst)
# Problem Set 3c
# Name: Matt Coley
#
from string import *
def subStringMatchExact(target, key):
"""search for all locations of key in target"""
matchLst = []
lastMatchLoc = 0
matchChk = 1
while matchChk:
currMatchLoc = find(target, key, lastMatchLoc)
if currMatchLoc == -1:
matchChk = 0
else:
matchLst.append(currMatchLoc)
lastMatchLoc = currMatchLoc + 1
return tuple(matchLst)
def constrainedMatchPair(firstMatch, secondMatch, length):
"""search for all locations of firstMatch, where secondMatch follows with 1 gap in between"""
matchLst = []
for n in firstMatch:
for k in secondMatch:
if n+length+1 == k:
matchLst.append(n)
return tuple(matchLst)
# Problem Set 3d
# Name: Matt Coley
#
from string import *
def subStringMatchExact(target, key):
"""search for all locations of key in target"""
matchLst = []
lastMatchLoc = 0
matchChk = 1
while matchChk:
currMatchLoc = find(target, key, lastMatchLoc)
if currMatchLoc == -1:
matchChk = 0
else:
matchLst.append(currMatchLoc)
lastMatchLoc = currMatchLoc + 1
return tuple(matchLst)
def constrainedMatchPair(firstMatch, secondMatch, length):
"""search for all locations of firstMatch, where secondMatch follows with 1 gap in between"""
matchLst = []
for n in firstMatch:
for k in secondMatch:
if n+length+1 == k:
matchLst.append(n)
return tuple(matchLst)
def subStringMatchExactlyOneSub(target, key):
"""search for all locations of key in target, only containing one substitution"""
allAnswers = ()
onlySubAns = []
for miss in range(0,len(key)):
# miss picks location for missing element
# key1 and key2 are substrings to match
key1 = key[:miss]
key2 = key[miss+1:]
## print 'breaking key',key,'into',key1,key2
# match1 and match2 are tuples of locations of start of matches
# for each substring in target
match1 = subStringMatchExact(target,key1)
match2 = subStringMatchExact(target,key2)
# when we get here, we have two tuples of start points
# need to filter pairs to decide which are correct
filtered = constrainedMatchPair(match1,match2,len(key1))
allAnswers = allAnswers + filtered
## print 'match1',match1
## print 'match2',match2
## print 'possible matches for',key1,key2,'start at',filtered
onlySubAns = list(allAnswers)
numRem = 0
for x in allAnswers:
for y in subStringMatchExact(target, key):
if x == y:
onlySubAns.remove(x)
numRem += 1
return tuple(onlySubAns)
dimebucker
1 year ago
|
 |
MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 3, HW 1
Used a program to find the answers to problem 1 with problem 2 answered in comments at the bottom.
used the same program for problems 3 and 4.
# Problem Set 2
# Name: Matt Coley
# Time:
#
nugPk1 = 6
nugPk2 = 9
nugPk3 = 20
nugTotal = 0
nugCombo = 51
nugPk1Cnt = 0
nugPk2Cnt = 0
nugPk3Cnt = 0
# loop through each type of nugget limiting the test to the desired amount / each package size
# also including 0 as a size
while nugPk1Cnt < (nugCombo/nugPk1+1):
nugPk2Cnt = 0
nugTotal = nugPk1Cnt*nugPk1
while nugPk2Cnt < (nugCombo/nugPk2+1):
nugPk3Cnt = 0
nugTotal = (nugPk1Cnt*nugPk1) + (nugPk2Cnt*nugPk2) + (nugPk3Cnt*nugPk3)
while nugPk3Cnt < (nugCombo/nugPk3+1):
if nugTotal == nugCombo:
print nugTotal, "nuggets can be exactly obtained buying", nugPk1Cnt, nugPk1, \
"packs,", nugPk2Cnt, nugPk2, "packs, and", nugPk3Cnt, nugPk3, "packs."
nugTotal += nugPk3
nugPk3Cnt += 1
nugTotal += nugPk2
nugPk2Cnt += 1
nugTotal += nugPk1
nugPk1Cnt += 1
# Any number above 55 can now be obtained by multiples of 6. For example, 56 can be obtained by 50 + 6.
# 65 can be obtained by 53 + 6 = 59 + 6 = 65. Since the next available number past 50-55 is at least 6 numbers
# away from the start and one of the nugget sizes is 6, you can continue to generate groupings of 6 off into
# infinity. 50-55, 56-61, 62-67, etc.
# Problem Set 2a & 2b
# Name: Matt Coley
# Time:
#
nugPk1 = 6
nugPk2 = 9
nugPk3 = 20
nugTotal = 0
nugCombo = 200
nugPk1Cnt = 0
nugPk2Cnt = 0
nugPk3Cnt = 0
testNum = 0
lastConseq = 0
lastUACnt = 0
conseqChk = 0
conseqCnt = 0
while testNum < nugCombo:
conseqChk = 0
testNum += 1
nugPk1Cnt = 0
while nugPk1Cnt < (nugCombo/nugPk1+1):
nugPk2Cnt = 0
nugTotal = nugPk1Cnt*nugPk1
while nugPk2Cnt < (nugCombo/nugPk2+1):
nugPk3Cnt = 0
nugTotal = (nugPk1Cnt*nugPk1) + (nugPk2Cnt*nugPk2) + (nugPk3Cnt*nugPk3)
while nugPk3Cnt < (nugCombo/nugPk3+1):
if testNum == nugTotal:
conseqChk = 1
else:
lastUACnt = testNum
nugTotal += nugPk3
nugPk3Cnt += 1
nugTotal += nugPk2
nugPk2Cnt += 1
nugTotal += nugPk1
nugPk1Cnt += 1
if conseqChk == 1:
conseqCnt += 1
else:
conseqCnt = 0
if conseqCnt == nugPk1:
print lastUACnt, "is the largest exact unpurchasable amount."
if conseqCnt < nugPk1:
print lastUACnt, "is the largest exact unpurchasable amount."
dimebucker
1 year ago
|
 |
MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 2, HW 1
# Problem Set 1a
# Name: Matthew Coley
# Collaborators:
# Time: 1:00
#
testNumber = 1
primeCounter = 1
testCounter = 2
primeTester = 0
while primeCounter < 1000:
testNumber = testNumber + 2
while testCounter < (testNumber/2):
if testNumber % testCounter == 0:
print "divisible by", testCounter
primeTester = primeTester + 1
testCounter = testCounter + 1
if primeTester == 0:
primeCounter = primeCounter + 1
print testNumber, "is a prime number"
primeTester = 0
testCounter = 2
#testNumber = testNumber + 2
print testNumber, "is the 1000th prime"
# Problem Set 1b
# Name: Matthew Coley
# Collaborators:
# Time: :30
#
testNumber = 1
primeCounter = 1
testCounter = 2
primeTester = 0
sumLogPrime = 0
# Loop until 100 primes are found
while primeCounter < 100:
# Skip even numbers
testNumber = testNumber + 2
# Test all odds divisibility up to half the number
while testCounter < (testNumber/2):
if testNumber % testCounter == 0:
# Incriment primeTester each time a number is divisible
primeTester = primeTester + 1
testCounter = testCounter + 1
# If no numbers were divisible, testNumber is prime
if primeTester == 0:
primeCounter = primeCounter + 1
sumLogPrime = sumLogPrime + log(testNumber)
# Reset counter values
primeTester = 0
testCounter = 2
# Add log of 2, since it was skipped
sumLogPrime = sumLogPrime + log(2)
print sumLogPrime, testNumber, (sumLogPrime / testNumber)
dimebucker
1 year ago
|
 |
|
|
|