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
# Problem Set 3, Problem 1
# Name: Ariel Mamani
# Collaborators:
# Time: 10:30 #
from string import *
target1 = 'atgacatgcacaagtatgcat'
target2 = 'atgaatgcatggatgtaaatgcag'
# key strings
key10 = 'a'
key11 = 'atg'
key12 = 'atgc'
key13 = 'atgca'
def countSubStringMatch(target,key):
i=0
c=0
while(i+len(key)<len(target)):
s=target[i:i+len(key)]
if(s==key):
c+=1
i=i+len(key)
else:
i=i+1
return c
def countSubStringMatchRecursive(target,key):
if(len(target)<len(key)):
return 0
if(len(target)==len(key)):
if(target==key):
return 1
else:
return 0
else:
s=target[0:len(key)]
if(s==key):
return countSubStringMatchRecursive(target[len(key):],key)+1
else:
return countSubStringMatchRecursive(target[1:],key)
print countSubStringMatch(target1,key13)
print countSubStringMatch(target1,key12)
print countSubStringMatch(target1,key11)
# Problem Set 3, Problem 2
# Name: Ariel Mamani
# Collaborators:
# Time: 10:30 #
from string import *
target1 = 'atgacatgcacaagtatgcat'
target2 = 'atgaatgcatggatgtaaatgcag'
# key strings
key10 = 'a'
key11 = 'atg'
key12 = 'atgc'
key13 = 'atgca'
def subStringMatchExact(target,key):
t=[]
encontrado=find(target,key)
while encontrado!=-1:
t.append(encontrado)
encontrado=find(target,key,encontrado+1)
return tuple(t)
print subStringMatchExact(target1,key12)
print subStringMatchExact(target1,key13)
# Problem Set 3, Problem 3
# Name: Ariel Mamani
# Collaborators:
# Time: #
from string import *
# this is a code file that you can use as a template for submitting your
# solutions
# these are some example strings for use in testing your code
# target strings
target1 = 'atgacatgcacaagtatgcat'
target2 = 'atgaatgcatggatgtaaatgcag'
# key strings
key10 = 'a'
key11 = 'atg'
key12 = 'atgc'
key13 = 'atgca'
def subStringMatchExact(target,key):
t=[]
encontrado=find(target,key)
while encontrado!=-1:
t.append(encontrado)
encontrado=find(target,key,encontrado+1)
return tuple(t)
def constrainedMatchPair(firstMatch, secondMatch, length):
res = []
for r1 in firstMatch:
for r2 in secondMatch:
if r1 + length + 1 == r2:
res.append(r1)
return res
### 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 + tuple(filtered)
print 'match1',match1
print 'match2',match2
print 'possible matches for',key1,key2,'start at',filtered
return allAnswers
print subStringMatchOneSub(key10,target1)
print subStringMatchOneSub(key11,target1)
print subStringMatchOneSub(key12,target1)
print subStringMatchOneSub(key13,target1)
print subStringMatchOneSub(key10,target2)
print subStringMatchOneSub(key11,target2)
print subStringMatchOneSub(key12,target2)
print subStringMatchOneSub(key13,target2)
# Problem Set 3, Problem 4
# Name: Ariel Mamani
# Collaborators:
# Time: #
from string import *
# target strings
target1 = 'atgacatgcacaagtatgcat'
target2 = 'atgaatgcatggatgtaaatgcag'
# key strings
key10 = 'a'
key11 = 'atg'
key12 = 'atgc'
key13 = 'atgca'
def subStringMatchExact(target,key):
t=[]
encontrado=find(target,key)
while encontrado!=-1:
t.append(encontrado)
encontrado=find(target,key,encontrado+1)
return tuple(t)
def constrainedMatchPair(firstMatch, secondMatch, length):
res = []
for r1 in firstMatch:
for r2 in secondMatch:
if r1 + length + 1 == r2:
res.append(r1)
return res
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 + tuple(filtered)
#print 'match1',match1
#print 'match2',match2
#print 'possible matches for',key1,key2,'start at',filtered
return allAnswers
def subStringMatchExactlyOneSub(target,key):
exacto = set(subStringMatchExact(target,key))
#print exacto
soloUno = set(subStringMatchOneSub(key,target))
#print soloUno
resultado=soloUno-exacto
#print resultado
return tuple(resultado)
print subStringMatchExactlyOneSub(target2,key13)
ariel0
11 months ago
|
 |
MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 3, HW 1
sorry if my English is not very good
# Problem 1
#
# Name: Ariel Mamani (ariel0)
# Collaborators:
# Time: 9:31
#
#funcion que devuelve el valor de la ecuacion
#6a+9b+20c
def ecuacion(a,b,c):
return 6*a+9*b+20*c
#funcion que calcula las combinaciones de McNuggets
#para una cantidad dada
def comprar(cantidad):
#3 bucles que iteran los valores de a,b y c en:
#6a+9b+20c
a=0
while a<=cantidad:
b=0
if(ecuacion(a,b,0)>cantidad): break
while b<=cantidad:
c=0
if(ecuacion(a,b,c)>cantidad): break
while c<=cantidad:
if(ecuacion(a,b,c)==cantidad):
print "(a="+str(a)+",b="+str(b)+",c="+str(c)+") , 6*"+str(a)+"+9*"+str(b)+"+20*"+str(c)+"="+str(cantidad)
elif(ecuacion(a,b,c)>cantidad): break
c=c+1
b=b+1
a=a+1
#probar desde 50 -> 65
for i in range(140,150):
print "Para comprar "+str(i)+" McNuggets:"
comprar(i)
#Problem 2
#
#El teorema es cierto ya que si es posible comprar x, x+1,…, x+5 McNuggets
#y se tiene paquetes de 6, 9 y 20, todos los siguientes valores se pueden
#comprar añadiendo a cada valor en el rango de x, x+1,…, x+5 un multiplo de 6.
#Es decir, si x+y es cualquier valor mayor a x+5:
#x+y=6*i donde i es cualquier valor de x -> x+5.
#The theorem is true because if you can buy x, x +1, ..., x +5 McNuggets
#and we have packages of 6, 9 and 20, all of the following values can be
#buy adding to each value in the range of x, x +1, ..., x +5 a multiple of 6.
#That is, if x + y is any value greater than x + 5:
# x+y=6*i where i is any value of x -> x+5.
# Problem 3
#
# Name: Ariel Mamani (ariel0)
# Collaborators:
# Time: 10:31
#
#funcion que devuelve el valor de la ecuacion
#6a+9b+20c
def ecuacion(a,b,c):
return 6*a+9*b+20*c
#funcion que devuelve True si existe una combinacion para comprar la cantidad McNuggets
#devuelve False si no es posible comprar la cantidad dada
def comprar(cantidad):
#3 bucles que iteran los valores de a,b y c en:
#6a+9b+20c
a=0
while a<=cantidad:
b=0
if(ecuacion(a,b,0)>cantidad): break
while b<=cantidad:
c=0
if(ecuacion(a,b,c)>cantidad): break
while c<=cantidad:
if(ecuacion(a,b,c)==cantidad):
return True
elif(ecuacion(a,b,c)>cantidad): break
c=c+1
b=b+1
a=a+1
return False
#probar desde 49 -> 0
#como con el Teorema anterior se probo que se puede comprar McNuggets desde 50 en
#adelante, no tiene sentido comprobar desde 50 en adelante
#si probamos desde 49 hacia abajo, la primera vez que obtengamos una cantidad que
#no puede ser comprada, esta sera la mayor.
i=49
while(i>=0):
if(comprar(i)==False):
print "Largest number of McNuggets that cannot be bought in exact quantity: "+str(i)
break
i=i-1
# Problem 4
#
# Name: Ariel Mamani (ariel0)
# Collaborators:
# Time: 10:31
#
###
### template of code for Problem 4 of Problem Set 2, Fall 2008
###
bestSoFar = 0 # variable that keeps track of largest number
# of McNuggets that cannot be bought in exact quantity
packages = (6,9,20) # variable that contains package sizes
for n in range(1, 150): # only search for solutions up to size 150
## complete code here to find largest size that cannot be bought
## when done, your answer should be bound to bestSoFar
posible=False
a=0
while a<=n:
b=0
c=0
if(packages[0]*a+packages[1]*b+packages[2]*c>n): break
while b<=n:
c=0
if(packages[0]*a+packages[1]*b+packages[2]*c>n): break
while c<=n:
if(packages[0]*a+packages[1]*b+packages[2]*c==n):
posible=True
elif(packages[0]*a+packages[1]*b+packages[2]*c>n): break
c=c+1
b=b+1
a=a+1
if(posible==False):
if(n>bestSoFar):bestSoFar=n
print "Given package sizes "+str(packages[0])+", "+str(packages[1])+", and "+str(packages[2])+", the largest number of McNuggets that cannot be bought in exact quantity is: "+str(bestSoFar)
ariel0
1 year ago
|
 |
MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 2, HW 1
# Problem Set 1
# Name: ariel0
# Collaborators:
# Time: 10:10
#
n_primo=1
limite=1000
candidato=1
while(n_primo<limite):
#initialize the candidate
candidato=candidato+2
#check the candidate
i=2
es_primo=True
while(i*i<=candidato):
if(candidato%i==0):
es_primo=False
break
i=i+1
if(es_primo==True):
n_primo=n_primo+1
print "The " +str(n_primo)+"th prime number is: "+str(candidato)
# Problem Set 1
# Name: ariel0
# Collaborators:
# Time: 40:20
#
from math import *
n=int(raw_input("Enter a value for n:"))
suma=0
for j in range(2,n):
if(j==2 or j==3):
es_primo=True
else:
if(j%2<>0):
i=2
es_primo=True
while(i*i<=j):
if(j%i==0):
es_primo=False
break
i=i+1
else:
es_primo=False
if(es_primo==True):
suma=suma+log(j)
j=j+1
print "Value of n: "+str(n)
print "Sum of the logarithms: "+str(suma)
print "Ratio: "+str(suma/n)
ariel0
1 year ago
|
 |
|
|
|