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
from string import *
#=============== Probelm 1 =====================
def countSubStringMatch(target,key):
count = 0
matchFound = find(target,key)
while matchFound != -1:
matchFound = find(target,key,matchFound+1)
if matchFound != -1:
count = count + 1
return count
def testCountSubStringMatch():
target= "atgacatgcacaagtatgcat"
key = "atg"
countSubStringMatch(target,key)
def countSubStringMatchRecursive(target, key):
global counter
matchFound = find(target,key)
if matchFound != -1:
counter +=1
return countSubStringMatchRecursive(target[matchFound + 1 :], key)
else:
return counter
counter = 0
def testCountSubSringMatchRecursive():
target= "atgacatgcacaagtatgcatcatcatjgjgcatjjgvjcatjjbjcat"
key = "cat"
countSubStringMatchRecursive(target,key)
#============= Problem2 ================== =======
def subStringMatchExact(target,key):
matchFound =[]
match = find(target,key)
while match != -1:
matchFound.append(match)
match = find(target,key, match + 1)
return tuple(matchFound)
def testSubStringMatchExact():
# target strings
target1 = 'atgacatgcacaagtatgcat'
target2 = 'atgaatgcatggatgtaaatgcag'
# key strings
key10 = 'a'
key11 = 'atg'
key12 = 'atgc'
key13 = 'atgca'
# test
print "test1"
print subStringMatchExact('atgacatgcacaagtatgcat','atg')
print "test2"
print subStringMatchExact(target1,key11)
print "test3"
print subStringMatchExact(target1,key12)
print "test4"
print subStringMatchExact(target1,key13)
print "test5"
print subStringMatchExact(target2,key10)
print "test6"
print subStringMatchExact(target2,key11)
print "test6"
print subStringMatchExact(target2,key12)
print "test8"
print subStringMatchExact(target2,key13)
#================= Problem 3 =====================
target1 = 'atgacatgcacaagtatgcat'
target2 = 'atgaatgcatggatgtaaatgcag'
# key strings
key10 = 'a'
key11 = 'atg'
key12 = 'atgc'
key13 = 'atgca'
def constrainedMatchPair(firstMatch, secondMatch, length):
results = []
for ans in firstMatch:
for ans1 in secondMatch:
if ans + length + 1 == ans1:
results.append(ans)
return tuple(results)
### 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))
allAnswers = allAnswers + filtered
#print 'match1',match1
#print 'match2',match2
#print 'possible matches for',key1,key2,'start at',filtered
return allAnswers
#================== Problem 4 ================
def subStringMatchExactlyOneSub(target,key):
result = []
# One way of fiding the answer is with Sets
#exactMatches = set(subStringMatchExact(target,key))
#upToOneMatches = set(subStringMatchOneSub(key,target))
#return tuple(upToOneMatches - exactMatches )
# Second way
exactMatches = subStringMatchExact(target,key)
upToOneMatches = subStringMatchOneSub(key,target)
for ans in upToOneMatches:
if ans not in exactMatches:
result.append(ans)
print tuple(result)
apoo
1 year ago
|
 |
MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 3, HW 1
#===================== Problem 1 ==========================
def nuggetsFound(n):
"""
Returns a dictionary contaning Key: number of Nuggets Value: the
packages
"""
dic = {}
for i in range(0,n+1):
for j in range(0,n+1):
for k in range(0,n+1):
if (6*i) + (9*j)+ (20*k) == n:
if n in dic:
dic[n].append((i,j,k))
else:
dic[n] = [(i,j,k)]
return dic
def displayNuggestsFound(n):
"""
Gets a input (number of Nuggets) and pass it as an argument to
nuggetsFound(). Finally prints the output from nuggetsFound
"""
nMcNuggets = n # n = 50,51,52,53,54,54
dicNuggets = nuggetsFound(nMcNuggets)
for nuggets,numbers in dicNuggets.iteritems():
if type(numbers) == type([]):
for number in numbers:
print 'Number of McNuggets:',nuggets,'The set:', number
else:
print 'Number of McNuggets:',nuggets,'The set:', number
#===================== Problem 2 ===================
##### The theorem is true becasue any set after x, x+5 is a multiplay of 6.
##### Because 50 + 6 = 56 (a new set with only 6 added), 51 + 6 = 57 (new set with adding 6) and so on so on.
##### Therefore we can create a new set with the combination of 6, 9 and 12!
#==================== Problem 3 ====================
def numNuggetsNotExact(n):
"""
"""
Found = False
notExact = 0
for i in range(0,n):
for j in range(0,n):
for k in range(0,n):
if (6*i) + (9*j)+ (20*k) == n:
Found = True
return Found
def displaynumNuggetsNotExact():
nuggetsNotPurchased = 1
n = 2
counter = 1
while n < 50:
n = n +1
if not numNuggetsNotExact(n):
nuggetsNotPurchased = n
counter = 0
else:
counter = counter + 1
if counter == 6:
return "Largest number of McNuggets that cannot be bought in exact quantity: %d" % nuggetsNotPurchased
# ================== Problem 4 =========================
def nNumberNuggetsNotExact(x,y,z):
bestSoFar = 0
packages = (x,y,z)
counter = 0
for n in range(1,200):
found = False
for i in range(0 ,200):
for j in range(0,200):
for k in range(0,200):
if (i * packages[0]) + (j * packages[1])+ (k * packages[2]) == n:
found = True
if found:
counter = counter + 1
if not found:
bestSoFar = n
counter = 0
if counter == packages[0]:
print "Given package sizes %d, %d and %d, the largest number of McNuggets that cannot be bought in exact quantity is %d" % (packages[0],packages[1],packages[2],bestSoFar)
apoo
1 year ago
|
 |
MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 2, HW 1
# problem set 1
from time import time
from math import *
import sys
########## Problem 1 ##########
def PrimeNth(n):
"prints out the nth prime number"
primesFound = 0
number = 1
while primesFound < n:
number = number + 1
divisor = 2
isPrime = True
while divisor * divisor <= number:
if number % divisor == 0:
isPrime = False
break
divisor = divisor + 1
if isPrime:
primesFound = primesFound + 1
return number
######## Problem2 ##########
def Prime(n):
"Returns a list containing prime numbers"
primes = []
primesFound = 0
number = 1
while primesFound < n:
number = number + 1
divisor = 2
isPrime = True
while divisor * divisor <= number:
if number % divisor == 0:
isPrime = False
break
divisor = divisor + 1
if isPrime:
primesFound = primesFound + 1
primes.append(number)
return primes
def Log(lst):
sum = 0
for prime in lst:
sum = sum + log(prime)
return sum
def main():
start = time()
print "Problem 1:"
print "The",str(sys.argv[1])+"nth prime number is :", PrimeNth(int(sys.argv[1]))
#print Prime(int(sys.argv[1]))
print "The sum of log of",str(sys.argv[1]),"is: ", Log(Prime(int(sys.argv[1])))
print 'Time in seconds:' + str(time() - start)
if __name__ == '__main__':
main()
apoo
1 year ago
|
 |
|
|
|