About Me
No description provided.
Classes
|
Class status: Established
Role:
Student
|
.
23% complete
|
Submitted Assignments
 |
MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 5, HW 1
# File ps3a.py
#===============================================================
def countSubStringMatch(target,key):
""" Count the number of substring returns int """
index = count = 0
while index != -1:
index = target.find(key)
if index >= 0 :
count += 1
target = target[index+len(key):]
return count
def countSubStringMatchRecursive(target,key):
""" Count the number of substring returns int """
index = target.find(key)
if index == -1:
return 0;
else:
return 1 + countSubStringMatchRecursive(target[index+len(key):],key)
#================================================================
# File ps3b.py
#================================================================
def subStringMatchExact(target,key):
""" return the tuple of indices of the exact matches of key in the target"""
index = 0
indexes = ()
if len(key) == 0:
return tuple(range(len(target)))
while index != -1:
index = target.find(key,index)
if index >= 0 :
indexes = indexes + (index,)
index = index + len(key)
return tuple(indexes)
#================================================================
# File ps3c.py
#================================================================
def findSubStringsKey(key):
""" Finds combination of keys """
strings = []
for i in range(len(key)):
t = key[:i],key[i+1:]
strings.append(t)
return strings
def constrainedMatchPair(firstMatch,secondMatch,length):
return tuple(fM for fM in firstMatch for sM in secondMatch if fM + length + 1 == sM)
### the following procedure you will use in Problem 3
def subStringMatchOneSub(key,target):
"""search for all locations of key in target, with one substitution"""
allAnswers = ()
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))
if filtered:
allAnswers = allAnswers + filtered
#print 'match1',match1
#print 'match2',match2
#print 'possible matches for',key1,key2,'start at',filtered
return allAnswers
#================================================================
#File ps3d.py
#================================================================
def subStringMatchExactlyOneSub(target,key):
return set(subStringMatchOneSub(key,target)) - set(subStringMatchExact(target,key))
#================================================================
electracool
1 year ago
|
 |
MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 3, HW 1
import sys
"""
Return the combinations possbile as a list
"""
def getNuggets(param,val):
if not len(param) or val <= 0:
return []
# list
return [(a1,a2,a3) for a1 in range(val/param[0]+1) for a2 in range(val/param[1]+1) for a3 in range(val/param[2]+1) if (a1*param[0] + a2*param[1] + a3*param[2] == val)]
def getMaxNotPossible(param):
i = count = 0
while i < sys.maxint:
if len(getNuggets(param,i)) != 0:
count += 1
else:
count = 0
if count == min(param):
break;
i += 1
if count == min(param):
return i-min(param)
else:
return -1
if __name__ == "__main__":
print "Largest number of McNuggets that cannot be bought in exact quantity: " , getMaxNotPossible((6,9,20))
print max([i for i in range(201) if len(getNuggets( (6,9,20), i )) == 0])
print max([i for i in range(201) if len(getNuggets( (4,5,6), i )) == 0])
electracool
1 year ago
|
 |
MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 2, HW 1
import math
def getPrime(num):
i = 1
j = 3;
primes = [1,3]
while i < num:
k = 2;
while k < math.sqrt(j):
if j%k == 0:
break
k = k + 1
if k >= math.sqrt(j):
i = i + 1;
j = j + 2;
primes.append(j-2)
return primes;
p = getPrime(1000)
print p[len(p)-1]
for i in range(2,100):
primes = getPrime(i)
k = 0
for i in primes:
k = k + math.log(i)
print " Number : {0} Sum of logs : {1} ratio : {2}".format(i,k,i/k)
electracool
1 year ago
|
 |
|
|
|