Stop learning alone!

Learn faster and stay on-track by joining this free class with other self-learners.

Register for MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming now.

MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming

Class length: 24 weeks. Start anytime.

Creator: duallain

Status: Established

Join this class!

Lesson 5: Assignment 1

Assignment 3: Matching strings: a biological perspective

Homework Submissions

33 total

wolfei (Self-grade: Pretty good)
Submitted 1 month ago | Permalink | Time spent: 20 days

it took me a while to finish this assignment, i had trouble understanding what they wanted me to do in the last two problems.....gladly i think i have done it properly :)

from string import *

#  target strings

target1 = 'atgacatgcacaagtatgcat'
target2 = 'atgaatgcatggatgtaaatgcag'

# key strings

key10 = 'a'
key11 = 'atg'
key12 = 'atgc'
key13 = 'atgca'

#####################################
##### Problem Set 1             #####  
##### Name: Wolfei              #####  
##### Collaborators: Internet   ##### 
##### Time: pending             #####

def countSubStringMatch(target, key):
    instance = 0
    w = 0 
    while find(target, key, w) != -1:
        instance += 1
        w = find(target, key, w) + 1
    return instance
     
def countSubStringMatchRecursive(target, key):
    instance = 0 
    for lmao in range(0,len(target)):
        if target[lmao:len(key)+lmao] == key:
            instance += 1
    return instance


#####################################
##### Problem Set 2             #####  
##### Name: Wolfei              #####  
##### Collaborators: Internet   ##### 
##### Time: pending             #####
            

def subStringMatchExact(target, key):
    instance = 0
    w = 0
    group = ()
    while find(target, key, w) != -1:
        group = group + (find(target,key,w),)
        instance += 1
        w = find(target, key, w) + 1
    return group
    
    
def subStringMatchExact2(target, key):
    group = ()
    instance = 0 
    for lmao in range(0,len(target)):
        if target[lmao:len(key)+lmao] == key:
            group = group + (lmao,)
            instance += 1
    return group




#####################################
##### Problem Set 3             #####  
##### Name: Wolfei              #####  
##### Collaborators: Internet   ##### 
##### Time: pending             #####


### Function given to help complete problem 3


def subStringMatchOneSub(target,key):
    """search for all locations of key in target, with one substitution"""
    allAnswers = ()
    if len(key) != 1:
        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)
    ##        print match1, match2
            # 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
    else: return " your key has only 1 character, therefore it could substitude all characters in the string"

def constrainedMatchPair(firstMatch,secondMatch,length):
    group = ()
    for lmao in secondMatch:
        for lol in firstMatch:
            if lol+length+1 == lmao:
                group = group + (lol,)
    return group 
    


#####################################
##### Problem Set 4             #####  
##### Name: Wolfei              #####  
##### Collaborators: Internet   ##### 
##### Time: pending             #####


def subStringMatchExactlyOneSub(target,key):
    x = subStringMatchOneSub(target,key) #long one
    y = subStringMatchExact(target,key)  #short one
    if len(key) != 1:
##        print x,y
##        print subStringMatchExactlyOneSub(x,y)
        x, y = givenumbers(x,y)
        if len(x) == 0:
            print "there isn't an incorrect character in the string "
        else:
            print " This is your number"
            return x
    else:return " your key needs at least two characters in it"
        
    
def givenumbers(x,y):
##    tuple(x)
##    tuple(y)
    
    for n in range(len(y)):
        dummy03(x,y)
        x, y = dummy03(x,y)
##        print dummy03(x,y)
##        print x, y
    return x , y


    
def dummy03(x,y):
##    a = subStringMatchOneSub(key,target) long one
##    b = subStringMatchExact(target,key)  short one
##    group = ()
##    x = 5,15,5,15,0,15,5
##    y = 5,15
    group = ()

##    while len(y) != 0:
    for XXX in x:
        if y[0] != XXX:
            group += (XXX,)
##    print group, x
    x = group
    y = y[1:]
    return x, y
sebrenner (Self-grade: Pretty good)
Submitted 3 months ago | Permalink | Time spent: 8 hours

This assignment called for four different code files:

ps3a.py ps3b.py ps3c.py ps3d.py

I have included all four in the code section.

# Problem Set 3 (Problem 1)  ps3a.py
# Name: Scott Brenner
# Collaborators: None
# Date: 2011-01-29
# Time: 2:45 PM
#
 
#!/usr/local/bin/python
 
from string import *        #    used for find()
 
# Problem 1.
# Write two functions, called countSubStringMatch and countSubStringMatchRecursive that take two arguments, a key string and a target string. These functions iteratively and recursively count the number of instances of the key in the target string. You should complete definitions for
#    Targets and keys
target = "atgacatgcacaagtatgcat"
key = "atgc"
 
 
def countSubStringMatch(target,key):
    """This function returns the number of times a key string appears in target string.  This function iterates through the taret string."""
    count = 0
    start = 0
    while find(target, key, start) != -1:
        count += 1
        start = find(target, key, start) + 1
    return count
 
 
def countSubStringMatchRecursive (target, key):
    """This function returns the number of times a key string appears in target string.  This function uses recursion."""
    answer = 0
    next_match = find(target,key)
    if next_match == -1:
        return answer
    else:
        answer = 1 + countSubStringMatchRecursive(target[next_match+1:], key)
        return answer
 
print countSubStringMatch(target,key)
print countSubStringMatchRecursive(target,key)

#*****************************************************

# Problem Set 3 (Problem 2) ps3b.py
# Name: Scott Brenner
# Collaborators: None
# Date: 2011-02-01
# Start Time: 11:29 AM
# End Time: 12:04 PM
# I reused code I had drafted earlier.

#!/usr/local/bin/python

from string import *		#	used for find()


# Problem 2.
# Write the function subStringMatchExact. This function takes two arguments: a target string, and a key string. It should return a tuple of the starting points of matches of the key string in the target string, when indexing starts at 0. Complete the definition for

#	def subStringMatchExact(target,key):

#	For example, would return the tuple (5, 15). The file ps3_template.py includes some test strings that you can use to test your function. In particular, we provide two target strings and four key strings.  Test your function on each combination of key and target string, as well as other examples that you create. Place your answer in a file named ps3b.py

#	Targets and keys
target1 = 'atgacatgcacaagtatgcat'
target2 = 'atgaatgcatggatgtaaatgcag'

key10 = 'a'
key11 = 'atg'
key12 = 'atgc'
key13 = 'atgca'


def subStringMatchExact (target, key):
	"""This function returns a tuple of the starting position(s) of a key string within a target string"""
	answer_tuple = () # initialize the tuple we will return
	start = 0 # use this initial the starting point for find()
	while find(target, key, start) >=0:
		start = find(target, key, start)
		answer_tuple += (start,)
		start+=1
	return answer_tuple
	
####	Run Program with a variety of strings and keys	####
print "atgacatgcacaagtatgcat", "atgc"
print subStringMatchExact("atgacatgcacaagtatgcat", "atgc")
print target1, key10
print subStringMatchExact(target1, key10)
print target1, key11
print subStringMatchExact(target1, key11)
print target1, key12
print subStringMatchExact(target1, key12)
print target1, key13
print subStringMatchExact(target1, key13)
print target2, key10
print subStringMatchExact(target2, key10)
print target2, key11
print subStringMatchExact(target2, key11)
print target2, key12
print subStringMatchExact(target2, key12)
print target2, key13
print subStringMatchExact(target2,key13)



#*****************************************************
# Problem Set 3 (Problem 3)   ps3c.py
# Name: Scott Brenner
# Collaborators: None
# Date: 2011-02-01
# Time: 11:29 AM
#

#!/usr/local/bin/python

from string import *		#	used for find()


# Problem 3.
# Write a function, called constrainedMatchPair which takes three arguments: a tuple representing starting points for the first substring, a tuple representing starting points for the second substring, and the length of the first substring. The function should return a tuple of all members (call it n) of the first tuple for which there is an element in the second tuple (call it k) such that n+m+1 = k, where m is the length of the first substring. Complete the definition

# To test this function, we have provided a function called subStringMatchOneSub, which takes two arguments: a target string and a key string. This function will return a tuple of all starting points of matches of the key to the target, such that at most one element of the key is incorrectly matched to the target. This function is provided for you in the file ps3_template.py and invokes the function you are to write. Save your answers in a file named ps3c.py.

# these are some example strings for use in testing your code

#  target strings

target1 = 'atgacatgcacaagtatgcat'
#		   01234567890123456789
target2 = 'atgaatgcatggatgtaaatgcag'

# key strings

key10 = 'a'
key11 = 'atg' 	# matches in target1 == (0, 5, 15)	length == 3
key12 = 'atgc'	# matches in target1 == (5, 15)
key13 = 'atgca'


#### This is the method I defined.  ####
def constrainedMatchPair(firstMatch,secondMatch,length):
	"""This function takes three arguments: a tuple representing starting points for the first substring, a tuple representing starting points for the second substring, and the length of the first substring. The function should return a tuple of all members (call it n) of the first tuple for which there is an element in the second tuple (call it k) such that n+m+1 = k, where m is the length of the first substring."""
	answer = ()
	for i in firstMatch:
		for j in secondMatch:
			if i + length + 1 == j:
				answer += (i,)
	return answer

#### This is the method I defined in the earlier problem.  ####
def subStringMatchExact (target, key):
	"""This function returns a tuple of the starting position(s) of a key string within a target string"""
	answer_tuple = () # initialize the tuple we will return
	start = 0 # use this initial the starting point for find()
	while find(target, key, start) >=0:
		start = find(target, key, start)
		answer_tuple += (start,)
		start+=1
	return answer_tuple

### 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

print subStringMatchOneSub(key11,target1)



#*****************************************************
#!/usr/local/bin/python
# Problem Set 3 (Problem 4)    ps3d.py
# Name: Scott Brenner
# Collaborators: None
# Date: 2011-02-01
# Time: 11:29 AM
#
#	This was so hard to solve.  I think it took over eight hours spread over a week.  I kept getting stuck on how to remove the perfect matches from the tuple that was returned.

#	If you remove the matches from the beginning of the tuple using positions, the positions, of course, shift.  If you try to do it by matching the perfect matches to the entire realm of matches, you can't just rely on comparison of the perfect start to a member of the entire realm.  You must test a conjunction of ALL perfect matches to the member of the entire realm.  I couldn't get this to work. Instead I built a list of the positions of the perfect matches and then removed those elements from the answer tuple.  The key was learning to iterate through a tuple backwards.  That way removing an element did not alter the position of the elements yet to be removed.

from string import *		#	used for find()

# Problem 4
# Write a function, called subStringMatchExactlyOneSub which takes two arguments: a target string and a key string. This function should return a tuple of all starting points of matches of the key to the target, such that at exactly one element of the key is incorrectly matched to the target. Complete the definition.  Save your answers in a file named ps3d.py.

target1 = 'atgacatgcacaagtatgcatatgacatgcacaagtatgcatatgacatgcacaagtatgcatatgacatgcacaagtatgcat'
#		   01234567890123456789
target2 = 'atgaatgcatggatgtaaatgcag'

# key strings

key10 = 'a'
key11 = 'atg' 	
key12 = 'atgc'
key13 = 'atgca'



def subStringMatchExactlyOneSub(target,key):
	"""
	This function takes two arguments: a target string and a key string. 
	
	It returns a tuple of all starting points of matches of the key to the target, such that at exactly one element of the key is incorrectly matched to the target.
	"""

	possible_answer = subStringMatchOneSub(key,target)
	answer = possible_answer
	perfect_matches = subStringMatchExact(target,key)
	to_remove_from_answer = ()

	#	this for loop identifies the positions in possible_answer that contain perfect matches.
	for i in range(0,len(possible_answer)):
		for j in range(0,len(perfect_matches)):
			if possible_answer[i] == perfect_matches[j]:
				#print "matches:", possible_answer[i]
				to_remove_from_answer += (i,)
				#print to_remove_from_answer

	# this for loop removes the items from the possible_answer tuple begin at the end and working forward. 
	for m in reversed(to_remove_from_answer):
		#print to_remove_from_answer[-m]
		#print m
		answer = answer[:m] + answer[m+1:]
		# print answer
	return answer


def constrainedMatchPair(firstMatch,secondMatch,length):  
	"""
	This function takes three arguments: a tuple representing starting points for the first substring, a tuple representing starting points for the second substring, and the length of the first substring.
	
	The function returns a tuple of all members (call it n) of the first tuple for which there is an element in the second tuple (call it k) such that n+m+1 = k, where m is the length of the first substring.
	"""
	
	answer = ()
	for i in firstMatch:
		for j in secondMatch:
			if i + length + 1 == j:
				answer += (i,)
	return answer

def subStringMatchExact(target, key):
	"""
	This function returns a tuple of the starting position(s) of a key string within a target string.
	"""
	answer_tuple = () # initialize the tuple we will return
	start = 0 # use this initial the starting point for find()
	while find(target, key, start) >=0:
		start = find(target, key, start)
		answer_tuple += (start,)
		start+=1
	return answer_tuple

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
    
#print subStringMatchOneSub(key12,target1)
print subStringMatchExactlyOneSub(target1,key11)
ochikobore (Self-grade: Could be better)
Submitted 4 months ago | Permalink | Time spent: 12 hours
#Problem Set 3
#Name: Andrew
#Time: 12 hours
#Collaborators: Curious Reef

from string import *

#  target strings

target1 = 'atgacatgcacaagtatgcat'
target2 = 'atgaatgcatggatgtaaatgcag'

# key strings

key10 = 'a'
key11 = 'atg'
key12 = 'atgc'
key13 = 'atgca'

#problem 1
def countSubStringMatch(target,key):
    count = 0
    for i in range(len(target)):
        if find(target,key,i) == i:
            count += 1
    return count

def countSubStringMatchRecursive (target, key):
    """ Recursively counts the number of times a term appears in a string"""
    if find(target, key) != -1:
        return 1 + countSubStringMatchRecursive(target[find(target, key)+1:], key)
    return 0

#problem 2
def subStringMatchExact(target, key):
    counts = ()
    for i in range(len(target)):
        if find(target,key,i) == i:
            counts += i,
    
    return counts

#problem 3
def constrainedMatchPair(firstMatch, secondMatch, length):
    places = ()
    for i in firstMatch:
        for j in secondMatch:
            if i + length + 1 == j:
                places += (i,)
    return places

def subStringMatchOneSub(key,target):
    """search for all locations of key in target, with one substitution"""
    allAnswers = ()
    print 'target:',target
    print 'key:',key
    print ''
    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: '+'\''+key1+'\''+' and key2: '+'\''+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))
        print 'here is filtered:',filtered
        allAnswers = allAnswers + filtered
        print 'match1:',match1
        print 'match2:',match2
        print 'possible matches for',key1,key2,'start at',filtered
        print ''
    return allAnswers

#problem 4
def subStringMatchExactlyOneSub(key, target):
    """search for all locations of key in target, with one substitution"""
    allAnswers = ()
    print 'target:',target
    print 'key:',key
    print ''
    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: '+'\''+key1+'\''+' and key2: '+'\''+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))
        cache = ()
        for i in filtered:
            if i not in subStringMatchExact(target,key):
                cache += i,
        del filtered
        filtered = ()
        filtered = cache
        allAnswers = allAnswers + filtered
        print 'match1:',match1
        print 'match2:',match2
        print 'possible matches for',key1,key2,'start at',filtered
        print ''
    return allAnswers
   
##if __name__ == "__main__":
##    main()
rohshall (Self-grade: Outstanding)
Submitted 7 months ago | Permalink | Time spent: 1 minute
tuckertuck (Self-grade: Pretty good)
Submitted 10 months ago | Permalink | Time spent: 6 hours

The recursive function was really making me mad, Still didn't quite get it as I couldn't get it to return the proper statement that I wanted.

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'

### 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 1

def countSubStringMatch(target,key):
    """counts number of times key appears withing the target"""
    count = 0
    for i in range(0,len(target)):
        sequence = find(target,key,i,i+len(key))
        if sequence != -1:
            count += 1
    print key, 'occurs', count, 'times in the string', target

    
def countSubStringMatchRecursive(target,key, count = 0):
    """Counts the number of times a key appears within the target"""
    index = rfind(target,key)
    if index != -1:
        count += 1
        countSubStringMatchRecursive(target[:-len(target)+index],key,count)
    else:
        print key, 'occurs', count, 'times'

# Problem 2

def subStringMatchExact(target,key):
    """returns tuple with locations where key appears within target"""
    startPoint = []
    for i in range(0,len(target)):
        sequence = find(target,key,i,i+len(key))
        if sequence != -1:
            startPoint.append(sequence)
    return tuple(startPoint)

# Problem 3

def constrainedMatchPair(firstMatch,secondMatch,length):
    """returns a tuple of locations in firstMatch + length + 1 == secondMatch"""
    matchPair = []
    for k in secondMatch:
        for n in firstMatch:
            if n + length + 1 == k:
                matchPair.append(n)
            elif n + length == k and firstMatch[-1] == k and len(secondMatch) == len(firstMatch):
                matchPair.append(n)
    return tuple(matchPair)

# Problem 4

def subStringMatchExactlyOneSub(target,key):
    """ returns all locations of key in target, with exactly one substitution"""
    exact = subStringMatchExact(target,key)
    oneSub = subStringMatchOneSub(key,target)
    exactlyOneSub = list(oneSub)
        
    for match in exact:
        for one in oneSub:
            if match == one:
                exactlyOneSub.remove(match)
    return tuple(exactlyOneSub)
jimmy (Self-grade: Pretty good)
Submitted 10 months ago | Permalink | Time spent: 15 minutes
##Problem Set 3
## Name: Jimmy
## Time: 15:00

from string import *

target1 = 'atgacatgcacaagtatgcat'
target2 = 'atgaatgcatggatgtaaatgcag'

targets = (target1, target2)

key10 = 'a'
key11 = 'atg'
key12 = 'atgc'
key13 = 'atgca'

keys = (key10, key11, key12, key13)


def subStringMatchOneSub(key,target):
    """search for all locations of key in target, with one substitution"""
    allAnswers = ()
    for miss in range(0,len(key)):
        key1 = key[:miss]
        key2 = key[miss+1:]
        match1 = subStringMatchExact(target,key1)
        match2 = subStringMatchExact(target,key2)
        filtered = constrainedMatchPair(match1,match2,len(key1))
        allAnswers = allAnswers + filtered
    return allAnswers


## problem 3a


def countSubStringMatch(target,key):
        counter = [-1]
        tryrange = range(0, len(target) + 1)
        for x in tryrange:
                location = find(target, key, x) 
                if location not in counter:
                        counter.append(location)
        if counter == -1:
                return 0
        else:
                counter.remove(-1)              
                return len(counter)

def helpcountSubStringMatchRecursive(target, key, incr, counter):
        position = find(target, key, incr)
        incr += 1       
        if position not in counter:
                counter.append(position)
                helpcountSubStringMatchRecursive(target, key, incr, counter)
        if incr -1 >= len(target):
                return counter
        else:
                helpcountSubStringMatchRecursive(target, key, incr, counter)

def countSubStringMatchRecursive(target, key):
        counter = [-1] 
        helpcountSubStringMatchRecursive(target, key, 0, counter)
        counter.remove(-1)
        return len(counter)

## problem 3b

def subStringMatchExact(target,key):
        counter = [-1]
        tryrange = range(0, len(target) + 1)
        for x in tryrange:
                location = find(target, key, x) 
                if location not in counter:
                        counter.append(location)
        if counter == -1:
                return 0
        else:
                counter.remove(-1)      
                return tuple(counter)

## problem 3c

def constrainedMatchPair(firstMatch, secondMatch, length):
        matches = []
        for n in firstMatch:
                for k in secondMatch:
                        m = length                      
                        if n + m + 1 == k:
                                matches.append(n)
        return tuple(matches)


## problem 3d

def subStringMatchExactlyOneSub(target,key):
        exact = subStringMatchExact(target,key) 
        close = subStringMatchOneSub(key,target)
        onesub = []
        for index in close:
                if index not in exact:
                        onesub.append(index)
        return tuple(onesub)

ariel0 (Self-grade: Pretty good)
Submitted 11 months ago | Permalink | Time spent: 5 days
# 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)
Gpyti (Self-grade: Pretty good)
Submitted 11 months ago | Permalink | Time spent: 1 day
from string import *
def subStringMatchExact(target,key):
i=0
c=0
tup=()
while c!=-1:
c = find(target,key,i)
if c!=-1:
tup = tup + (c,)
i=c+1
return tup

def subStringMatchOneSub1(target,key):
length=len(key)
for i in range(0,length):
key1=key[0:i]
key2=key[i+1:length]
starts1=subStringMatch(target,key1)
starts2=subStringMatch(target,key2)
print starts1, starts2

def constrainedMatchPair(firstMatch,secondMatch,length):
tup=()
m=len(secondMatch)
lenfirst=len(firstMatch)
lensecond=len(secondMatch)
for n in range(0,lenfirst):
for k in range(0,lensecond):
if firstMatch[n]+m+1 == secondMatch[k]:
tup = tup +(n,)
return tup


def subStringMatchOneSub(target,key):
"""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)
#print match1,match2
# 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

#def subStringMatchExactlyOneSub(target,key):
mjcuva (Self-grade: Could be better)
Submitted 1 year ago | Permalink | Time spent: 3 hours

pset3

from string import *

##Target Strings
target1 = 'atgacatgcacaagtatgcat'
target2 = 'atgaatgcatggatgtaaatgcag'

##Key Strings
key10 = 'a'
key11 = 'atg'
key12 = 'atgc'
key13 = 'atgca'

##Problem 1

def countSubStringMatch(key,target):
    """This function returns the number of instances of the key in the target string"""
    counter = 0 ##Set the counter to 0
    start = 0   ##Set the starting place in the string to 0

    while find(key,target,start) >=0: 
        counter += 1    ##Add one to the counter when a match is found
        start = find(key,target,start) + 1  ##Change the starting place to the next character in the string
    return counter  ##Return the final counter

def countSubStringMatchRecursive(target,key):
    """This function returns the number of instances of the key in the target string."""
    first = find(target,key) ##Find the first instance of the target string.
    if first == -1:     ##Exit if there is no instance of the key.
        return 0
    else:
        return 1 + countSubStringMatch(target[first+1:-1],key)  ##Return the result of the function with the new slice of the string.


##Problem 2
def subStringMatchExact(target,key):

    start = 0   ##Set the starting point to 0
    point = []  ##Initialize the list to hold the starting points

    while find(target,key,start) >= 0:
        point.append(find(target,key,start))    ##Add the starting point to the list
        start = find(target,key,start) + 1      ##Find the new starting place in the target string
    answertuple = tuple(point)                  ##Change the answer to a tuple
    return answertuple                          ##Return the tuple

#Problem 3

def subStringMatchOneSub(key,target): # this function was provided by the instructor
    """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

def constrainedMatchPair(firstMatch,secondMatch,length):

    match = []  ##Create the list to keep track of the matches

    for i in range(0,len(firstMatch)):  ##Test if they match up
        for j in range(0,len(secondMatch)):
            if secondMatch[j] == firstMatch[i] + length + 1:
                match.append(firstMatch[i])
                break 
    matchestuple = tuple(match)
    return matchestuple

def subStringMatchExactlyOneSub(target,key):

    exactmatch = subStringMatchExact(target,key)
    allmatch = subStringMatchOneSub(key,target)

    output = []
    for i in range(0,len(allmatch)):
        check = 0
        for j in range(0, len(exactmatch)):
            if exactmatch[j] == allmatch[i]:
                check = 1
                break
            if check == 0:
                output.append(allmatch[i])
        outputtuple = tuple(output)
        return outputtuple
fredgust (Self-grade: Pretty good)
Submitted 1 year ago | Permalink | Time spent: 1 day
# Problem Set 3
# Name: Fredrik Gustafsson
# Python version: 3.2
# Time: 3:00

##
## Problem 3a
##

def countSubStringMatch(target, key):
    """Takes a target and a key and returns an ingeger number of instances of key in target"""
    
    assert type(target) == str, 'target must be of type string, NOT of type:' + str(type(target))
    assert type(key) == str, 'key must be of type string, NOT of type:' + str(type(key))

    x = 0
    ctr = 0
    
    while x in range(len(target)):
        y = str.find(target, key, x)
        if y != -1:
            x = y + len(key)
            ctr += 1
        else:
            return ctr
    
def countSubStringMatchRecursive (target, key):
    """Takes a target and a key and returns an integer number of instances of key in target"""
    
    assert type(target) == str, 'target must be of type string, NOT of type:' + str(type(target))
    assert type(key) == str, 'key must be of type string, NOT of type:' + str(type(key))

    y = str.find(target, key)
    
    if y != -1:
        ctr = countSubStringMatchRecursive(target[y+len(key):], key)
    else:
        ctr = 0
        return ctr

    return ctr + 1
    
##
## Problem 3b
##

def subStringMatchExact(target, key):
    """Takes a target and a key and returns A TUPLE of the starting position
       of matches of the key string in the target strign"""
    
    assert type(target) == str, 'target must be of type string, NOT of type:' + str(type(target))
    assert type(key) == str, 'key must be of type string, NOT of type:' + str(type(key))
    
    ls = []
    x = 0

    while x in range(len(target)):
        y = str.find(target, key, x)
        if y != -1:
            if len(key) == 0:
                x += 1
            else:
                x = y + len(key)
            ls.append (y)
        else:
            return tuple(ls)

##
## Problem 3c
##

def constrainedMatchPair(firstMatch,secondMatch,length):
    """Takes two tuples of startingpoints and the length of the first keyword,
       and returns a tuple of all the members of n in k so that (n + length + 1 = k)"""

    if firstMatch != None:
        assert type(firstMatch) == tuple, 'firstMatch must be of type tuple, NOT of type:' + str(type(firstMatch))
    if secondMatch != None:
        assert type(secondMatch) == tuple, 'secondMatch must be of type tuple, NOT of type:' + str(type(secondMatch))
    assert type(length) == int, 'length of the first strign must be of type int, NOT of type:' + str(type(length))

    if firstMatch == None: return secondMatch
    if secondMatch == None: return tuple(firstMatch)
    
    x = 0
    z = []

    while x in range(len(firstMatch)):
        y = 0
        while y in range(len(secondMatch)):
            if (firstMatch[x] + length + 1) == secondMatch[y]:
                z.append(firstMatch[x])
            y += 1
        x += 1
    return tuple(z)

##
## Problem 3d
##

def subStringMatchExactlyOneSub(target,key):
    """ """

    assert type(target) == str, 'target must be of type string, NOT of type' + str(type(target))
    assert type(key) == str, 'key must be of type string, NOT of type' + str(type(key))

    # First step splits the key into two lists
    string1 = []
    string2 = []
    x = 1
    while x in range(len(key)+1):
        string1.append(key[:x-1])
        string2.append(key[x:])
        x += 1

    # Searches for matches between the strings in the two tuples and the target
    match1 = []
    match2 = []
    x = 0
    while x in range(len(string1)):
        match1.append(subStringMatchExact(target, string1[x]))
        match2.append(subStringMatchExact(target, string2[x]))
        x += 1

    # Searches for members of match1 that satisfy match1[x] + len(string1 in match1[x]) + 1 = match2[x]
    member = []
    x = 0
    while x in range(len(string1)):
        member.append(constrainedMatchPair(match1[x], match2[x], len(string1[x])))
        x += 1
    
    # Sorts and removes duplicate
    a = []                              
    for x in range(len(member)):        
        z = member[x]                   
        for y in range(len(z)):
            if z[y] not in a:
                a.append(z[y])
    a.sort()
    return tuple(a)
apoo (Self-grade: Outstanding)
Submitted 1 year ago | Permalink | Time spent: 1 minute
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)
    
    
    

mercutio22 (Self-grade: Pretty good)
Submitted 1 year ago | Permalink | Time spent: 1 minute
#!/usr/bin/env python
#encoding=utf-8

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 countSubStringMatch(targetstring,substring):
    """count ocurrences of substring in targetstring iteratively"""
        
    if type(targetstring) != type(str()) or type(substring) != type(str()) :# type checking (both arguments should be strings)
        return -1 # this indicates an error
    
    count = 0
    primeiro = find(targetstring, substring)
    if primeiro == -1:
        print targetstring, "não tem", substring
        return -1
    else: 
        while primeiro != -1:
            count += 1
            proximo = primeiro + 1   
            primeiro = find(targetstring, substring, proximo)
    print targetstring, "contém", count, substring

def countSubstringMatchRecursive(targetstring, keystring, count=0):
    """count ocurrences of substring in targetstring recursively.
    The count variable serves to keep track of the instances of the
    string through the recursion, and is essentially intialized externally by
    setting it to 0 above.
    This function should be called with 'print'    """  

    #if type(targetstring) != type(str()) or type(keystring) != type(str()) :# type checking (both arguments should be strings)
       # return -1 # this indicates an error  

    primeiro = find(targetstring, keystring)
    if primeiro != -1:
        proximo  = primeiro + 1
        resultado = 1 + countSubstringMatchRecursive(targetstring[proximo:], keystring)
        print resultado        
        return resultado              
    else:
        print 'não há', keystring, 'em', targetstring 
        return 0 #Base case (targetstring==keystring).

#=====================end of ps3a.py============

#  target strings

target1 = 'atgacatgcacaagtatgcat'
target2 = 'atgaatgcatggatgtaaatgcag'

# key strings

key10 = 'a'
key11 = 'atg'
key12 = 'atgc'
key13 = 'atgca'

def subStringMatchExact(targetstring, substring):
    """returns a tuple of the starting points of matches of the key string in the target
    string, when indexing starts at 0"""

    if type(targetstring) != type(str()) or type(substring) != type(str()) :# type checking (both arguments should be strings)
        return -1 # this indicates an error
    posicao = ()
    count = 0
    primeiro = find(targetstring, substring)
    if primeiro == -1:
        print targetstring, "não tem", substring
        return -1
    else: 
        while primeiro != -1:
            count += 1
            proximo = primeiro + 1
            posicao = posicao + (primeiro,)   
            primeiro = find(targetstring, substring, proximo)
    print targetstring, "contém", count, substring, 'nas posições: ', posicao


subStringMatchExact(target2, key10)
subStringMatchExact(target2, key11)
subStringMatchExact(target2, key12)
subStringMatchExact(target2, key13)
subStringMatchExact(target1, key10)
subStringMatchExact(target1, key11)
subStringMatchExact(target1, key12)
subStringMatchExact(target1, key13)

#===========================end of ps3b.py==============


target1 = 'atgacatgcacaagtatgcat'
target2 = 'atgaatgcatggatgtaaatgcag'

# key strings

key10 = 'a'
key11 = 'atg'
key12 = 'atgc'
key13 = 'atgca'


def subStringMatchExact(targetstring, substring):
    """returns a tuple of the starting points of matches of the key string in the target
    string, when indexing starts at 0"""

    if type(targetstring) != type(str()) or type(substring) != type(str()) :# type checking (both arguments should be strings)
        return -1 # this indicates an error
    posicao = ()
    count = 0
    primeiro = find(targetstring, substring)
    if primeiro == -1:
        print targetstring, "não tem", substring
        return -1
    else: 
        while primeiro != -1:
            count += 1
            proximo = primeiro + 1
            posicao = posicao + (primeiro,)   
            primeiro = find(targetstring, substring, proximo)
    return posicao
        
def constrainedMatchPair(FirstMatch, SecondMatch, Length):
    """filters which combinations of subkeys are possible near-matches in targetstring"""
    NearMatch = ()
    for n in FirstMatch:
        for k in SecondMatch: 
            if n + Length+ 1 == k:
                NearMatch += (n,)
            else:
                pass
    return NearMatch
    
### 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))#fc que preciso escrever - na verdade é só o filtro!!
        allAnswers = allAnswers + filtered
        print 'match1',match1
        print 'match2',match2
        print 'possible matches for',key1,key2,'start at',filtered
    return allAnswers


subStringMatchOneSub(key10, target2)
subStringMatchOneSub(key11, target2)
subStringMatchOneSub(key12, target2)
subStringMatchOneSub(key13, target2)
subStringMatchOneSub(key10, target1)
subStringMatchOneSub(key11, target1)
subStringMatchOneSub(key12, target1)
subStringMatchOneSub(key13, target1)
#match1 = 0, 4, 8, 12, 18
#match2 = 7, 21
#print constrainedMatchPair(match1, match2, len('a'))

#=================end of ps3c.py==============


target1 = 'atgacatgcacaagtatgcat'
target2 = 'atgaatgcatggatgtaaatgcag'

# key strings

key10 = 'a'
key11 = 'atg'
key12 = 'atgc'
key13 = 'atgca'

def subStringMatchExact(targetstring, substring):
    """returns a tuple of the starting points of matches of the key string in the target
    string, when indexing starts at 0"""

    posicao = ()
    count = 0
    primeiro = find(targetstring, substring)
    if primeiro == -1:
        #print targetstring, "não tem", substring
        return ()
    else: 
        while primeiro != -1:
            count += 1
            proximo = primeiro + 1
            posicao = posicao + (primeiro,)   
            primeiro = find(targetstring, substring, proximo)
    #print posicao
    return posicao

def constrainedMatchPair(FirstMatch, SecondMatch, Length):
    """filters which combinations of subkeys are possible near-matches in targetstring"""
    NearMatch = ()
    for n in FirstMatch:
        for k in SecondMatch: 
            if n + Length+ 1 == k:
                NearMatch += (n,)
            else:
                pass
    return NearMatch

def subStringMatchOneSub(target,key):
    """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))#fc que preciso escrever - na verdade é só o filtro!!
        allAnswers = allAnswers + filtered
        #print 'match1',match1
        #print 'match2',match2
        #print 'possible matches for','"',key1,'"', '&', '"',str(key2),'"','start at',filtered
    #print allAnswers
    return allAnswers

def subStringMatchExactlyOneSub(target,key):
    """returns a tuple of all starting points of matches of the key to target
     with exactly one substitution"""
    PerfectMatches = subStringMatchExact(target, key)
    NearMatches = subStringMatchOneSub(target, key)
    results = ()
    all = PerfectMatches + NearMatches
    for member in set(all):
        results += (member,)
    print results 
    return results


#subStringMatchExactlyOneSub(target2, key13)
#subStringMatchOneSub(target2, key13)

#subStringMatchExactlyOneSub(target2, key11)
#subStringMatchOneSub(target2, key11)


#subStringMatchOneSub(target2, key12)
#subStringMatchExact(target2, key12)
#subStringMatchExactlyOneSub(target2, key12)

#print subStringMatchExact(target2, key13)
#subStringMatchOneSub(target2, key13)
#subStringMatchExact(target2, key13)
subStringMatchExactlyOneSub(target2, key13)

#subStringMatchExactlyOneSub(target2, key13)

#subStringMatchExactlyOneSub(target1, key10)

#subStringMatchExactlyOneSub(target1, key11)

#subStringMatchExactlyOneSub(target1, key12)

#subStringMatchExactlyOneSub(target1, key13)
     
#=========================end of ps3d.py==============



           
dacorest (Self-grade: Outstanding)
Submitted 1 year ago | Permalink | Time spent: 1 minute
# Name: Waz
#Problem set 3

_______Problem 1__________
from string import*
def countSubStringMatch(target,key):
   counter =0
   whurr = find(target,key)
   while whurr!= -1:
      counter += 1
      whurr = find(target, key, whurr+1)
   return counter

_______Problem 2_____________
from string import*

def countSubStringMatchExact(target,key):
   stpoints = ()
   whurr = find(target,key)
   while whurr!= -1:
      stpoints += (whurr,)
      whurr = find(target, key, whurr+1)
   return stpoints

_________Problem 3_____________
def constrainedMatchPair(firstMatch,secondMatch,length):
    result = ()
    for n in firstMatch:
        for k in secondMatch:
            if n + length+ 1 == k:
                result += (n,)
    return result

_________Problem 4___________
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
        
conwayblue (Self-grade: Pretty good)
Submitted 1 year ago | Permalink | Time spent: 2 days
#Problem Set 3
#Name: conwayblue


from string import *

target1 = 'atgacatgcacaagtatgcat'
target2 = 'atgaatgcatggatgtaaatgcag'

key10 = 'a'
key11 = 'atg'
key12 = 'atgc'
key13 = 'atgca'

#Problem 1 - Iterative----------------------------------------------

def countSubStringMatch(target,key):
    """Counts the number of key strings in target string - Incremental"""
    
    count = 0       #Initialize count
    position = -1   #Initialize position at -1 to start at index 0
    
    while True:               
        position = find(target,key,position+1)  #Find index of 1st key in target   
        if position == -1:                      #Test if None
            break        
        count += 1                              #Increase count
    return count

#Problem 1 - Recursive-----------------------------------------------

def countSubStringMatchRecursive(target,key):
    """Counts the number of key strings in target string - Recursive"""

    count = 0                                   #Initialize count
    position = find(target,key)                 #Find initial 

    if position == -1:                          #Test if None
        return count
    else:
        count += 1                              #Increase count
        return count + countSubStringMatchRecursive(target[position+1:],key)  #Add count to count from recursion at last position + 1

#Problem 1a - Recursive-----------------------------------------------

def countSubStringMatchRecursive2(target,key):
    """Counts the number of key strings in target string - Recursive"""
    
    if find(target,key) == -1: return 0
    return 1 + countSubStringMatchRecursive(target[find(target,key)+1:],key)

#Problem 2 --------------------------------------------------------------

def subStringMatchExact(target,key):
    """Returns a tuple of index locations of each key in target"""

    answers = []
    position = -1

    while True:
        position = find(target,key,position+1)
        if position == -1:
            break
        answers.append(position)
    return tuple(answers)

#Problem 3---------------------------------------------------------------

def constrainedMatchPair(firstMatch,secondMatch,length):
    """Returns a tuple of indexes from firstMatch that are possible matches given indexes of secondMatch and length of key"""

    ##firstIndex + length + 1 = secondIndex
    ##secondIndex - firstIndex - length = 1
   
    n = []    
    for firstIndex in firstMatch:
        for secondIndex in secondMatch:
            if secondIndex - firstIndex - length == 1:
                n.append(firstIndex)
    return tuple(n)


#function from problem sheet-

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
        print "------------------------------------"
    return allAnswers


#Problem 4-----------------------------------------------------------------

def subStringMatchExactlyOneSub(target,key):
    """Returns indexes of key matches missing exactly one letter only"""

    exactMatch = subStringMatchExact(target,key)      #indexes of exact matches
    subMatch = subStringMatchOneSub(key,target)       #indexes of exact matches and one sub matches
    
    answer = []

    for each in subMatch:
        if each  not in exactMatch:
            answer.append(each)
    return tuple(answer)

    


    
4orty4 (Self-grade: Outstanding)
Submitted 1 year ago | Permalink | Time spent: 5 hours
# Problem Set 3
# Name: 4orty4
# Time: 5:00
from string import *

#  target strings
target1 = 'atgacatgcacaagtatgcat'
target2 = 'atgaatgcatggatgtaaatgcag'
# key strings
key10 = 'a'
key11 = 'atg'
key12 = 'atgc'
key13 = 'atgca'

# Problem 1
def countSubStringMatch(target, key):
    """Iteratively counts matches in the string"""
    i = 0 # tracks the postion of the slice
    n = 0 # tracks the number of matching results found
    while i < len(target):
        j = find(target[i:], key) #tracks position of match relative to slice
        if j == -1:
            print 'Search Complete'
            return n
        else:
            n += 1 #found match, add 1 to match counter
            print 'Match ', n, 'found at position ', j+i #j+i = slice position + relative match position
            i += j+1
    print 'Error'
    return None


def countSubStringMatchRecursive(target, key):
    """Recursively counts matches in the string"""
    n = 1
    i = find(target, key)
    print i
    if i == -1: #if no matches are found
        return 0
    else:
        n += countSubStringMatchRecursive(target[i+1:], key)
        return n

# Problems 2, 3 and 4
def subStringMatchExact(target,key):
    """Returns a tuple of the starting points of matches
    in the target string"""
    i = 0 # tracks the postion of the slice
    sols = () # Creates an empty tuple to return after search
    # Special case if the function is sent an empty string as key
    # Returns a tuple with all numbers 0-length of target
    if key == '': return tuple(range(len(target1)))
    while i < (len(target)+1):
        j = find(target[i:], key) #tracks position of match relative to slice
        if j == -1:
            # print 'Search Complete'
            return sols
        else:
            sols +=(j+i,) #adds slice location to solution tuple
            i += j+1

def constrainedMatchPair(firstMatch,secondMatch,length):
    """The function is given two tuples representing starting points 
    of two searches for a search string.  The function should return 
    a tuple of all members (call it n) of the first tuple for which 
    there is an element in the second tuple (call it k) such that 
    n+m+1 = k, where m is the length of the first substring."""
    ans = ()
    for n in firstMatch:
        matchTest = n + length + 1
        for k in secondMatch: #searches for a matching k value in the second tuple
            if k == matchTest: ans += (n,)
    return ans

#Target and key were reversed from the template 
#for consistency with other functions
def subStringMatchOneSub(target, key):
    """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 allAnswers
        print 'match1',match1
        print 'match2',match2
        print 'possible matches for',key1,key2,'start at',filtered
    return allAnswers

def removeDup(t):
    """Scans a tuple of integers and returns a new tuple
    with duplicate integers removed"""
    
    r = ()
    for i in range(0, len(t)):
        isMatch = False
        for j in range(i+1, len(t)):
            # print 't[i], t[j] = ', t[i], t[j]
            if t[i] == t[j]: 
                isMatch = True
        # print isMatch
        if isMatch == False: 
            r+=(t[i],)
            # print 'writing unique answer'
    return r

def removeExactFromAll(exactMatches, allMatches):
    """Receives two tuples of integers. Creates and returns a tuple of
    substitution matches that contains integers from allMatches not 
    included in exactMatches"""
    subMatches = ()
    for i in range (0, len(allMatches)):
        isMatch = False
        for j in range (0, len(exactMatches)):
            if allMatches[i] == exactMatches[j]:
                isMatch = True
        if isMatch == False:
            subMatches += (allMatches[i],)
    return subMatches


def subStringMatchExactlyOneSub(target, key):
    """Returns a tuple representing the starting point 
    of matches with one substitution. It is produced by comparing
    tuple containing the locations of exact matches with the 
    tuple containing location of substitution+exact matches,
    and leaves only the substitution matches."""
    
    #Get location of exact matches
    exactMatches = subStringMatchExact(target,key)
    
    #Get location of sub matches + exactMatches
    allMatches = subStringMatchOneSub(target, key)
    
    #Remove duplicate answers from allMatches
    allMatches = removeDup(allMatches)
    print '    Exact matches are at ', exactMatches
    print '    Substitution matches are at ', allMatches
    
    return tuple(sorted(removeExactFromAll(exactMatches, allMatches)))

x = key12
y = target2
answer = subStringMatchExactlyOneSub(target2, key13)
print 'Searched for ', x, 'in ', y
print 'Substitution matches are located at ', answer
DvorakAJS (Self-grade: Could be better)
Submitted 1 year ago | Permalink | Time spent: 30 days

didn't really understand what the last one was about and took me a long time to continue working on it so I put down a month (wich is probably true).

#Problem1
from string import *

def countSubStringMatch(target,key):
    count,x=0,0
    while x !=-1:
        if find(target,key,x)==-1:
            return count
        else:
            x=find(target,key,x)+1
            count+=1
    
def countSubStringMatchRecursive(target,key):
    count=0
    if find(target,key)==-1: return count
    else: return countSubStringMatchRecursive(target[find(target,key)+1:],key)+1

#Problem2

def subStringMatchExact(target,key):
 ourtuple=()
 x=0
 while x !=-1:
        if find(target,key,x)==-1:
            return ourtuple
        else:
            ourtuple=ourtuple[:]+(find(target,key,x),)
            x=find(target,key,x)+1

#Problem3
def constrainedMatchPair(firstMatch,secondMatch,length):
    matchedtuple=()
    for targetsfirst in firstMatch:
        for targetssecond in secondMatch:
            if (targetsfirst-targetssecond+length==-1):
                matchedtuple=matchedtuple[:]+(targetsfirst,)
    return matchedtuple

#Problem4

def subStringMatchExactlyOneSub(target,key):
    tupleForReturn=()
    tupleOfAll=subStringMatchOneSub(key,target)
    tupleOfExact=subStringMatchExact(target,key)
    print tupleOfAll
    print tupleOfExact
    for i in tupleOfAll:
        if i not in tupleOfExact: tupleForReturn+=(i,)
    return tupleForReturn



a3k3 (Self-grade: Pretty good)
Submitted 1 year ago | Permalink | Time spent: 6 hours

Problem Set 3 - DNA Sequencing problem

# Amy Karoline
# Course 600
# Problem Set 3, DNA Sequencing
# Time: ~6.5hours
# 01282011-02012011
######

from string import *

##Problem 1:
##    Write two functions, called countSubStringMatch and
##    countSubStringMatchRecursive that take two arguments, a key string and a
##    target string. These functions iteratively and recursively count the number
##    f instances of the key in the target string. You should complete definitions
##    for
##    def countSubStringMatch(target,key):
##    and
##    def countSubStringMatchRecursive (target, key):
##
##    don't use count function
##    target is big string
##    key is small string

print ''
print 'Problem 1!'
print ''

def countSubStringMatch(target, key):
    ctr = 0
    found = find(target, key)
    while found != -1:
        found = find(target, key, found+1)
        ctr += 1
    return ctr

## You're looking for # of instances of key in target - key is small, target is big

def countSubStringRecursive(target,key):
    foundAt = find(target, key)
    bigLength = len(target)
    smallLength = len(key)
    if foundAt == -1:
        return 0
    nextIndex = foundAt + 1
    return 1 + countSubStringRecursive(target[nextIndex:],key)

big = 'supercalifracalgilistic' #raw_input('key:')
small = 'cal' #raw_input('target:')
print countSubStringMatch(big,small)
print countSubStringRecursive(big,small)  # should return 2

##Problem 2:
##    Use recursive or iterative approaches.
##    Write the function subStringMatchExact.This function takes two arguments:
##        a target string, and a key string. It should return a tuple of the
##        starting points of matches of the key string in the target string,
##        when indexing starts at 0. Complete the definition for
##        def subStringMatchExact(target,key):
##
##    For example,
##        subStringMatchExact("atgacatgcacaagtatgcat","atgc")
##        would return the tuple (5, 15). The file ps3_template.py includes
##        some test strings that you can use to test your function. In particular,
##        we provide two target strings:
##            target1 = 'atgacatgcacaagtatgcat'
##            target2 = 'atgaatgcatggatgtaaatgcag'
##            and four key strings:
##            key10 = 'a'
##            key11 = 'atg'
##            key12 = 'atgc'
##            key13 = 'atgca'
##    Test your function on each combination of key and target string, as
##    well as other examples that you create.

print ''
print 'Problem 2!'
print ''

def subStringMatchExact(big,small):
    indSolns = ()
    foundAt = find(big, small)
    if foundAt == -1:
        print 'There are no matches. Empty Set:'
        return indSolns
    else:
        while foundAt != -1:
            indSolns += (foundAt,)
            new = foundAt + 1
            foundAt = find(big, small, new)
        return indSolns

a = 'atgacatgcacaagtatgcat'
b = 'atgc'
print subStringMatchExact(a,b)

target1 = 'atgacatgcacaagtatgcat'
target2 = 'atgaatgcatggatgtaaatgcag'
key10 = 'a'
key11 = 'atg'
key12 = 'atgc'
key13 = 'atgca'
print subStringMatchExact(target1, key10)
print subStringMatchExact(target1, key11)
print subStringMatchExact(target1, key12)
print subStringMatchExact(target1, key13)
print subStringMatchExact(target2, key10)
print subStringMatchExact(target2, key11)
print subStringMatchExact(target2, key12)
print subStringMatchExact(target2, key13)


##Problem 3:
##    Near matches of key strings in target strings
##    Write a function, which takes three arguments: a tuple representing starting points for
##    the first substring, a tuple representing starting points for the second substring, and the length
##    of the first substring. The function should return a tuple of all members (call it n) of the first
##    tuple for which there is an element in the second tuple (call it k) such that n+m+1 = k, where m is
##    the length of the first substring.

print ''
print 'Problem 3!'
print ''

def constrainedMatchPair(firstMatch,secondMatch,length):
# firstmatch is a tuple of the starting points for first string matches
# secondmatch is a tuple of the starting points for second string matches
# length is length of the first string, m
# return a tuple of all elements n of the first tuple for which there is an element of the second tuple k
# such that n+m+1 = k and m is the length
# i.e. firstMatch[i] + length + 1 = secondmatch[i]
    matchPairs = ()
    for n in firstMatch:
        k = n + length + 1
        if k in secondMatch:
            matchPairs += (n,)
    return matchPairs

## given function for testing problem 3 from template file
def subStringMatchOneSub(target,key):
    """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
        allAnswers=tuple(set(allAnswers))
    return allAnswers

print subStringMatchOneSub(target1, key10)
print subStringMatchOneSub(target1, key11)


##Problem 4:
##    Write a function, which takes two arguments: a target string and a key string.
##    This function should return a tuple of all starting points of matches of the key to the
##    target, such that exactly one element of the key is incorrectly matched to the target.

print ''
print 'Problem 4!'
print ''

def subStringMatchExactlyOneSub(target,key):
    oneElemOffMatch = ()
    allMatch = subStringMatchOneSub(target, key)
    print 'allmatch is ', allMatch
    exactMatch = subStringMatchExact(target, key)
    print 'exactMatch is ', exactMatch
    for p in range(0, len(allMatch)):
        if allMatch[p] in exactMatch:
            oneElemOffMatch += (allMatch[p],)
    return oneElemOffMatch

print subStringMatchExactlyOneSub(target1, key10)
print subStringMatchExactlyOneSub(target1, key11)

























rfh (Self-grade: Pretty good)
Submitted 1 year ago | Permalink

Modified the test function for problem 3 to provide better debugging feedback

from string import *

# Problem 1
#
# Write two functions, called countSubStringMatch and 
# countSubStringMatchRecursive that take two arguments, a key string and
# a target string. These functions iteratively and recursively count the
# number of instances of the key in the target string. You should
# complete definitions for
#
# def countSubStringMatch(target,key):
#
# and
#
# def countSubStringMatchRecursive (target, key):

# Iterative function counts number of instances of "key" in "target"
def countSubStringMatch(target, key):
    """
    Iterative function counts number of instances of "key" in "target"
    """

    found = 0
    current = find(target, key)
    while current != -1:
        found = found + 1
        current = find(target, key, current +1)
    return found

key = 'abc'
target = 'abcdabce'

print "Iterative implementation of 'countSubStringMatch' found '%s' %d times in '%s'" % (key, countSubStringMatch(target, key), target)

def countSubStringMatchRecursive(target, key):
    """
    Recursive function counts number of instances of "key" in "target"
    """

    index = find(target, key)
    if index == -1:
        return 0

    nextIndex = index + 1
    return 1 + countSubStringMatchRecursive(target[nextIndex:], key)

key = 'abc'
target = 'abcdabce'

print "Recursive implementation of 'countSubStringMatch' found '%s' %d times in '%s'" % (key, countSubStringMatchRecursive(target, key), target)

print

# Problem 2
#
# Write the function subStringMatchExact. This function takes two
# arguments: a target string, and a key string. It should return a tuple
# of the starting points of matches of the key string in the target
# string, when indexing starts at 0. Complete the definition for
#
# def subStringMatchExact(target,key):
#
# For example,
#
# subStringMatchExact("atgacatgcacaagtatgcat","atgc")
#
# would return the tuple (5, 15).

def subStringMatchExact(target, key):
    """
    Returns a tuple containing all starting indexes for found matches of
    key in target
    """

    foundIndexes = ()
    current = find(target, key)
    while current != -1:
        foundIndexes = foundIndexes + (current,)
        current = find(target, key, current + 1)

    return foundIndexes

#  target strings
target1 = 'atgacatgcacaagtatgcat'
target2 = 'atgaatgcatggatgtaaatgcag'

# key strings
key10 = 'a'
key11 = 'atg'
key12 = 'atgc'
key13 = 'atgca'

print "target: '%s', key: '%s', indexes: " % (target1, key10), subStringMatchExact(target1, key10)
print "target: '%s', key: '%s', indexes: " % (target1, key11), subStringMatchExact(target1, key11)
print "target: '%s', key: '%s', indexes: " % (target1, key12), subStringMatchExact(target1, key12)
print "target: '%s', key: '%s', indexes: " % (target1, key13), subStringMatchExact(target1, key13)
print

print "target: '%s', key: '%s', indexes: " % (target2, key10), subStringMatchExact(target2, key10)
print "target: '%s', key: '%s', indexes: " % (target2, key11), subStringMatchExact(target2, key11)
print "target: '%s', key: '%s', indexes: " % (target2, key12), subStringMatchExact(target2, key12)
print "target: '%s', key: '%s', indexes: " % (target2, key13), subStringMatchExact(target2, key13)
print

# Problem 3
#
# Write a function, called constrainedMatchPair which takes three
# arguments: a tuple representing starting points for the first
# substring, a tuple representing starting points for the second
# substring, and the length of the first substring. The function should
# return a tuple of all members (call it n) of the first tuple for which
# there is an element in the second tuple (call it k) such that
# n+m+1 = k, where m is the length of the first substring.

def constrainedMatchPair(firstMatches, secondMatches, length):
    """
    Returns a tuple of all starting indexes for the provided tuple of
    first matches that have a corresponding index in secondMatches such
    that the index in second match starts directly when the index from
    the first match ends
    """

    foundMatches = ()
    for firstMatch in firstMatches:
        for secondMatch in secondMatches:
            if (firstMatch + length + 1) == secondMatch:
                foundMatches = foundMatches + (firstMatch,)

    return foundMatches

# Test function
def subStringMatchOneSub(key,target):
    """
    search for all locations of key in target, with one substitution
    """

    allAnswers = ()
    print "key: '%s', target: '%s'" % (key, target)
    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 '%s' into '%s' and '%s'" % (key, 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 "matches for '%s': " % (key1,), match1
        print "matches for '%s': " % (key2,), match2
        print "possible matches for '%s*%s' start at: " % (key1, key2), filtered
    return allAnswers

subStringMatchOneSub(key11, target1)

print

# Problem 4
#
# Write a function, called subStringMatchExactlyOneSub which takes two
# arguments: a target string and a key string. This function should
# return a tuple of all starting points of matches of the key to the
# target, such that at exactly one element of the key is incorrectly
# matched to the target. Complete the definition

def subStringMatchExactlyOneSub(target, key):
    """
    Returns a tuple of all starting points for matches of key in target
    such that one character in the match is incorrect
    """

    print "Looking for instances of '%s' with exactly one substitution in '%s'" % (key, target)

    exactMatches = subStringMatchExact(target, key)
    partialAndExactMatches = ()

    for gap in range(0, len(key)):
        key1 = key[:gap]
        key2 = key[gap+1:]
        print "Splitting '%s' into '%s' and '%s'" % (key, key1, key2)

        match1 = subStringMatchExact(target, key1)
        print "Exact matches for '%s':" % (key1), match1
        match2 = subStringMatchExact(target, key2)
        print "Exact matches for '%s':" % (key2), match2

        currentPartialAndExactMatches = constrainedMatchPair(match1, match2, len(key1))
        print "Partial and exact matches for '%s*%s': " % (key1, key2), currentPartialAndExactMatches
        partialAndExactMatches = partialAndExactMatches + currentPartialAndExactMatches

    partialMatches = ()
    for candidate in partialAndExactMatches:
        if not candidate in exactMatches:
            partialMatches = partialMatches + (candidate,)

    return partialMatches

target = 'azcdabzdabcadcd'
key = 'abcd'

matchesExactlyOneSub = subStringMatchExactlyOneSub(target, key)
print "Matches in '%s' for '%s' with exactly 1 substitution: " % (target, key), matchesExactlyOneSub
pyBoogie (Self-grade: Pretty good)
Submitted 1 year ago | Permalink
# Problem Set 3 (Part 1)
# Name: Boogie
# Collaborators: None
# Time: 0:30

from string import *

def countSubStringMatchRecursion(target, key):
    """
    purpose: to count the no. of occurrences of key in target string
    parameters: target - target string, key - search string
    output: count
    """

    indexInTarget = 0
    startIndex    = 0

    while indexInTarget != -1:
        
        indexInTarget = find(target, key)
        if indexInTarget != -1:
            # reset startIndex of target to startIndex + length of the key
            startIndex = indexInTarget + len(key)
            # start new search, with modified target
            return countSubStringMatchRecursion(target[startIndex:],key) + 1

    return 0

def countSubStringMatch(target, key):
    ctr = 0
    indexInTarget = 0
  
    startIndex = 0

    while indexInTarget != -1:
        indexInTarget = find(target, key)
        if indexInTarget != -1:
            ctr = ctr + 1
            startIndex = indexInTarget + len(key)
            target = target[startIndex:]
    
    return ctr


# Problem Set 3 (Part 2)
# Name: Boogie
# Collaborators: None
# Time: 0:30

from string import *

def subStringMatchExact(target, key):

    indexInTarget = 0
    startIndex    = 0

    ans = ()

    while indexInTarget != -1:
        indexInTarget = find(target, key)
        if indexInTarget != -1:
            ans = ans + (startIndex + indexInTarget,)
            startIndex = indexInTarget + len(key)
            target = target[startIndex:]

    return ans



mrphud (Self-grade: Pretty good)
Submitted 1 year ago | Permalink
from string import *

##Problem #1
#We will define a function that returns all instances and appearances of a key string within a target string
#The """string""" provides the user with a short description of the function

def countSubStringMatch(target, key):
    """Returns the locations where the key (string) exists and number of occurances in the target (string)"""
    search = True                                       #Setting the state variables
    locations = ()
    place = 0
    while search == True:
        result = find(target, key, place)               #The find function returns where in the string the key appears
        if result != -1 and result != 0:                #testing to see if it appears anywhere
            locations += (result,)                      #If id does, add it to the tuple and shift the search
            place = result + 1                          #starting point
        elif result == -1 and locations == ():          #NOTE: find returns 0 if you search for an empty string ('') in any target          
            print key, 'does not exist in', target      #or result == 0: can be added to remove the case whe
            search = False                              
        elif result == -1 and locations != ():
            print 'There are', len(locations), 'places where', key, 'exists.'
            print 'They are', locations
            search = False

# The recursive function below will only count the number of occurances

def countSubStringMatchRecursive(target, key):
    """Returns the locations where the key (string) exists in the target (string)"""
    result = find(target, key)                                  #state variable every time the function is called.
    if result != -1:                                            #test for base case:
        target = target[result + 1:]                            #if not then shorten the target string and do it again
        return 1 + countSubStringMatchRecursive(target, key)    #return 1 + the result of the next iteration
    else:                                                       #the recursive 'nesting' in the return allows you to 'count' without a counting variable 
        return 0                                                #use return 0 vs. None because you can't mix 1
                                                                #(the integer result from the above return) with the 'type' None
##Problem #2
#This will modify the first function to return only the tuple with mactched locations

def subStringMatchExact(target, key):
    """Returns the locations where the key (string) exists"""
    search = True                                       #Setting the state variables
    locations = ()
    place = 0
    while search == True:
        result = find(target, key, place)               #The find function returns where in the string the key appears
        if result != -1:                                #testing to see if it appears anywhere. Can add "and result != 0" to remove '' search
            locations += (result,)                      #If id does, add it to the tuple and shift the search
            place = result + 1                          #starting point
        else:
            search = False                              #if result == -1, end the loop and return locations
    return locations

##Problem #3
#This will define a function that returns all places where 

def constrainedMatchPair(firstMatch, secondMatch, length):
    """returns tuple satisfying the conditions firstMatch + length + 1 = secondMatch"""
    replica = ()                                    #initializes the tuple variable 'replica'
    for i in firstMatch:                            #sets the value in firstMatch
        for j in secondMatch:                       #scans the values in secondMatch such that
            if 1 + length + i == j:                 #they satisfy this contition
                replica += (i,)                     #if they do, add it to replica. Otherwise continue the loop
    return replica                                  #returns the values that satisfy the if statement.



dimebucker (Self-grade: Could be better)
Submitted 1 year ago | Permalink
# 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)
chip (Self-grade: Outstanding)
Submitted 1 year ago | Permalink
#Problem Set 3
#Name: chip
#Time: 0:40


target1 = 'atgacatgcacaagtatgcat'
target2 = 'atgaatgcatggatgtaaatgcag'

key10 = 'a'
key11 = 'atg'
key12 = 'atgc'
key13 = 'atgca'

#Problem 1
def countSubStringMatch(target, key):
        result = target.find(key)
        count = 0
        while result != -1:
                count += 1
                result = target.find(key, result + 1)
        return count

def countSubStringMatchRecursive(target, key):
        result = target.find(key)
        if result == -1:
                return 0;
        else:
                return 1 + countSubStringMatchRecursive(target[result+len(key):], key)

#Problem 2
def subStringMatchExact(target,key):
        result = target.find(key)
        index = ()
        while result != -1:
                index += (result,)
                result = target.find(key, result + 1)
        return index

#Problem 3
def constrainedMatchPair(firstMatch, secondMatch, length):
        matches = ()
        for n in firstMatch:
                k = n + length + 1
                if k in secondMatch:
                        matches += (n,)
        return matches

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):
        return tuple(set(subStringMatchOneSub(key, target)) - set(subStringMatchExact(target, key)))
Joe (Self-grade: Outstanding)
Submitted 1 year ago | Permalink

Matching strings: a biological perspective

# Problem Set 3 (Part I)
# Name: Joe Li
# Time: 2:00
#

from string import *

def countSubStringMatch(target,key):
      count=0
      index=0
      while find(target,key,index)!=-1:         # while searching the target from index and find a key
            index=find(target,key,index)+1      # search from the next character
            count+=1
      return count



def countSubStringMatchRecursive (target, key):
      target=target[find(target,key)+1:]  # slice 'target'
      if find(target,key)==-1:            # if the substring doesn't contain the key
            return 1                      # the base case return 1 in order to count the last removed one
      else:
            recurse=countSubStringMatchRecursive(target,key)      # recerse
            return recurse+1                                      # count
                  

# Problem Set 3 (Part II)
# Name: Joe Li
# Time 1:00
#

from string import *

def subStringMatchExact(target,key):
      index=0
      match=()
      while find(target,key,index)!=-1:
            index=find(target,key,index)  # bind the position of found key to index
            match+=(index,)               # add to tuple
            index+=1                      # search from next character
      return match

target1 = 'atgacatgcacaagtatgcat'
target2 = 'atgaatgcatggatgtaaatgcag'
key10 = 'a'
key11 = 'atg'
key12 = 'atgc'
key13 = 'atgca'
print subStringMatchExact(target1,key10)
print subStringMatchExact(target1,key11)
print subStringMatchExact(target1,key12)
print subStringMatchExact(target1,key13)
print subStringMatchExact(target2,key10)
print subStringMatchExact(target2,key11)
print subStringMatchExact(target2,key12)
print subStringMatchExact(target2,key13)


# Problem Set 3 (Part III)
# Name: Joe Li
# Time 0:30
#

from string import *

def subStringMatchExact(target,key):
      index=0
      match=()
      while find(target,key,index)!=-1:
            index=find(target,key,index)  # bind the position of found key to index
            match+=(index,)               # add to tuple
            index+=1                      # search from next character
      return match

def constrainedMatchPair(firstMatch,secondMatch,length):
      filtered=()
      for n in range(0,len(firstMatch)):                          # for every element in 1st & 2nd Match
            for k in range(0,len(secondMatch)):
                  if firstMatch[n]+length+1==secondMatch[k]:      # if n+m+1=k holds
                        filtered+=(firstMatch[n],)                # add such n to result
      return filtered

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 Set 3 (Part IV)
# Name: Joe Li
# Time 0:30
#

from string import *

def subStringMatchExact(target,key):
      index=0
      match=()
      while find(target,key,index)!=-1:
            index=find(target,key,index)  # bind the position of found key to index
            match+=(index,)               # add to tuple
            index+=1                      # search from next character
      return match

def constrainedMatchPair(firstMatch,secondMatch,length):
      filtered=()
      for n in range(0,len(firstMatch)):                          # for every element in 1st & 2nd Match
            for k in range(0,len(secondMatch)):
                  if firstMatch[n]+length+1==secondMatch[k]:      # if n+m+1=k holds
                        filtered+=(firstMatch[n],)                # add such n to result
      return filtered

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
        allAnswers=tuple(set(allAnswers))
    return allAnswers

def subStringMatchExactlyOneSub(target,key):
      exact=list(subStringMatchExact(target,key))      
      one=list(subStringMatchOneSub(key,target))    
      for index in range(0,len(exact)):
            one.remove(exact[index])      #remove those elements occur in the 1st tuple from the 2nd tuple
      return tuple(one)
      
BTheMad (Self-grade: Could be better)
Submitted 1 year ago | Permalink

As I remember, I took a really straightforward way of solving these problems =/ Test functions do all the calls.

###############
## Problem 1 ##
###############
from string import *


def count_sub_string_match(target, key):
    """Find all occurrences of a string in another"""
    counter = 0
    position = 0
    pointer = 0
    while(pointer < len(target)):
        position = find(target, key, pointer)
        if position != -1:
            counter += 1
            pointer = position + 1
        else:
            pointer += 1
    return counter


def count_sub_string_match_recursive(target, key):
    """Find all occurrences of a string in another with recursion"""
    counter = 0
    position = 0
    position = find(target, key)
    if position == -1:
        return 0
    else:
        position += 1
        return 1 + count_sub_string_match_recursive(target[position:], key)


def test_count_sub_string():
    """testing our functions"""
    target1 = 'atgacatgcacaagtatgcat'
    target2 = 'atgaatgcatggatgtaaatgcag'
    key10 = 'a'
    key11 = 'atg'
    key12 = 'atgc'
    key13 = 'atgca'
    # Testing iterative version
    print "Iterative function"
    print count_sub_string_match(target1, key10)
    print count_sub_string_match(target1, key11)
    print count_sub_string_match(target1, key12)
    print count_sub_string_match(target1, key13)
    print count_sub_string_match(target2, key10)
    print count_sub_string_match(target2, key11)
    print count_sub_string_match(target2, key12)
    print count_sub_string_match(target2, key13)
    # Testing recursive version
    print "Recursive function"
    print count_sub_string_match_recursive(target1, key10)
    print count_sub_string_match_recursive(target1, key11)
    print count_sub_string_match_recursive(target1, key12)
    print count_sub_string_match_recursive(target1, key13)
    print count_sub_string_match_recursive(target2, key10)
    print count_sub_string_match_recursive(target2, key11)
    print count_sub_string_match_recursive(target2, key12)
    print count_sub_string_match_recursive(target2, key13)

test_count_sub_string()

###############
## Problem 2 ##
###############
from string import *


def count_sub_string_match_exact(target, key):
    """Find all occurrences of a string in another"""
    position_list = []
    position = 0
    pointer = 0
    while(pointer < len(target)):
        position = find(target, key, pointer)
        if position != -1:
            position_list.append(position)
            pointer = position + 1
        else:
            pointer += 1
    return tuple(position_list)


def test_count_sub_string():
    """testing our functions"""
    target1 = 'atgacatgcacaagtatgcat'
    target2 = 'atgaatgcatggatgtaaatgcag'
    key10 = 'a'
    key11 = 'atg'
    key12 = 'atgc'
    key13 = 'atgca'
    # Testing iterative version
    print "Iterative function"
    print count_sub_string_match_exact(target1, key10)
    print count_sub_string_match_exact(target1, key11)
    print count_sub_string_match_exact(target1, key12)
    print count_sub_string_match_exact(target1, key13)
    print count_sub_string_match_exact(target2, key10)
    print count_sub_string_match_exact(target2, key11)
    print count_sub_string_match_exact(target2, key12)
    print count_sub_string_match_exact(target2, key13)

test_count_sub_string()


###############
## Problem 3 ##
###############
from string import *


def sub_string_match_exact(target, key):
    """Find all occurrences of a string in another"""
    position_list = []
    position = 0
    pointer = 0
    while(pointer < len(target)):
        position = find(target, key, pointer)
        if position != -1:
            position_list.append(position)
            pointer = position + 1
        else:
            pointer += 1
    return tuple(position_list)


def constrained_match_pair(first_match, second_match, length):
    """ Find intersection """
    intersection = []
    for n in first_match:
        for m in second_match:
            if n + length + 1 == m:
                intersection.append(n)
    return tuple(intersection)


def sub_string_match_one_sub(target, key):
    """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 = sub_string_match_exact(target, key1)
        match2 = sub_string_match_exact(target, key2)
        # when we get here, we have two tuples of start points
        # need to filter pairs to decide which are correct
        filtered = constrained_match_pair(match1, match2, len(key1))
        allAnswers = allAnswers + filtered
        print 'match1', match1
        print 'match2', match2
        print 'possible matches for', key1, key2, 'start at', filtered
        print ''
    return allAnswers


def test_count_sub_string():
    """testing our functions"""
    target1 = 'atgacatgcacaagtatgcat'
    target2 = 'atgaatgcatggatgtaaatgcag'
    #key10 = 'a'
    key11 = 'atg'
    key12 = 'atgc'
    key13 = 'atgca'
    # Testing problem 3    
    # print sub_string_match_one_sub(target1, key10)
    print sub_string_match_one_sub(target1, key11)
    print sub_string_match_one_sub(target1, key12)
    print sub_string_match_one_sub(target1, key13)
    # print sub_string_match_one_sub(target2, key10)
    print sub_string_match_one_sub(target2, key11)
    print sub_string_match_one_sub(target2, key12)
    print sub_string_match_one_sub(target2, key13)

test_count_sub_string()


###############
## Problem 4 ##
###############
from string import *


def sub_string_match_exact(target, key):
    """Find all occurrences of a string in another"""
    position_list = ()
    position = 0
    pointer = 0
    while(pointer < len(target)):
        position = find(target, key, pointer)
        if position != -1:
            position_list += (position,)
            pointer = position + 1
        else:
            pointer += 1
    return position_list


def constrained_match_pair(first_match, second_match, length):
    """ Find intersection """
    intersection = ()
    for n in first_match:
        for m in second_match:
            if n + length + 1 == m:
                intersection += (n,)
    return intersection


def sub_string_match_one_exactly_one_sub(target, key):
    """search for all locations of key in target, with one substitution"""
    allAnswers = ()
    for miss in range(0, len(key)):
        print allAnswers
        # 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 = sub_string_match_exact(target, key1)
        match2 = sub_string_match_exact(target, key2)
        # when we get here, we have two tuples of start points
        # need to filter pairs to decide which are correct
        filtered = constrained_match_pair(match1, match2, len(key1))
        for one_sub in filtered:
            if one_sub not in match2:
                allAnswers = allAnswers + (one_sub,)
        print 'match1', match1
        print 'match2', match2
        print 'possible matches for', key1, key2, 'start at', filtered
        print ''
    return allAnswers


def test_count_sub_string():
    """testing our functions"""
    target1 = 'atgacatgcacaagtatgcat'
    target2 = 'atgaatgcatggatgtaaatgcag'
    key10 = 'a'
    key11 = 'atg'
    key12 = 'atgc'
    key13 = 'atgca'
    # Testing problem 3    
    # print sub_string_match_one_exactly_one_sub(target1, key10)
    # print sub_string_match_one_exactly_one_sub(target1, key11)
    # print sub_string_match_one_exactly_one_sub(target1, key12)
    # print sub_string_match_one_exactly_one_sub(target1, key13)
    # print sub_string_match_one_exactly_one_sub(target2, key10)
    # print sub_string_match_one_exactly_one_sub(target2, key11)
    # print sub_string_match_one_exactly_one_sub(target2, key12)
    print sub_string_match_one_exactly_one_sub(target2, key13)

test_count_sub_string()
scarolan (Self-grade: Pretty good)
Submitted 1 year ago | Permalink

Did they do this to make sure we were paying attention?

close = subStringMatchOneSub(key, target)

# Problem Set 3

from string import *

#  target strings
target1 = 'atgacatgcacaagtatgcat'
target2 = 'atgaatgcatggatgtaaatgcag'
target3 = 'accaccaccaccaccaccaccacca'

# key strings
key10 = 'a'
key11 = 'atg'
key12 = 'atgc'
key13 = 'atgca'
key14 = 'acca'

###################
# Problem 3a                   #
###################

def countSubStringMatch(target, key):
    """ Iteratively counts the number of times a term appears in a string"""
    count = next = 0
    while find(target, key, next) != -1:
        next = find(target, key, next) + 1
        count += 1
    return count

def countSubStringMatchRecursive (target, key):
    """ Recursively counts the number of times a term appears in a string"""
    index = find(target, key)
    if index == -1:
        return 0
    else:
        # Slice notation says "Everything except the first part that we already searched"
        # Eg, start next recursion one place after the previous match that we have found.
        return 1 + countSubStringMatchRecursive(target[index+1:], key)

###################
# Problem 3b                   #
###################

def subStringMatchExact(target,key):
    matchList = []
    startFrom = 0
    index = find(target, key, startFrom)
    while index != -1:
        matchList.append(index)
        startFrom = index + 1
        index = find(target, key, startFrom)
    return tuple(matchList)

###################
# Problem 3c                   #
###################

def constrainedMatchPair(firstMatch,secondMatch,length):
    # firstMatch and secondMatch are tuples
    # n is the starting point of the first substring match
    # m is the length of the first substring
    # k is the sum of n + m + 1
    matches = []
    for n in firstMatch:
        for k in secondMatch:
            m = length
            if n + m + 1 == k:
                matches.append(n)
    return tuple(matches)

def subStringMatchOneSub(key, target):
    """search for all locations of key in target, with one substitution"""
    # Uncomment the print statements to get more output.
    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 3d                   #
###################
   
def subStringMatchExactlyOneSub(key, target):
    """Returns tuple of ONLY partial matches"""
    onesub = () # One heart, lets get together and feel all right
    exact = subStringMatchExact(target, key)
    close = subStringMatchOneSub(key, target)
    for i in close:
        if i not in exact:
            onesub += (i,)
    return onesub

###################
# Code test output          #
###################

print "Testing code..."
print "The target strand of DNA is",(target2),"and the search key is",(key13)
print "Exact matches for",(key13),"were found at these positions:",subStringMatchExact(target2, key13)
print "Possible one-substitution matches were found at:",subStringMatchOneSub(key13, target2)
print "DNA sequences with only one different base pair found at:",subStringMatchExactlyOneSub(key13, target2)
jyen (Self-grade: Pretty good)
Submitted 1 year ago | Permalink
# Set 3
# jyen

from string import *

def countSubStringMatch (target, key):
    """ Iteratively counts the number of times a term appears in a string"""
    count = 0
    place = 0
    while find(target, key, place) != -1:
        place = find(target, key, place) + 1
        count += 1
    return count

def countSubStringMatchRecursive (target, key):
    """ Recursively counts the number of times a term appears in a string"""
    if find(target, key) != -1:
        return 1 + countSubStringMatchRecursive(target[find(target, key)+1:], key)
    return 0

def subStringMatchExact (target, key):
    """Iteratively finds the locations where a term appears in a string"""
    places = []
    index = 0
    while find(target, key, index) != -1:
        places.append(find(target, key, index))
        index = find(target, key, index) + 1
    return tuple(places)

 
def constrainedMatchPair (firstMatch, secondMatch, length):
    places = ()
    for x in firstMatch:
        for y in secondMatch:
            if x + length + 1 == y:
                places += (x,)
    return places

def subStringMatchOneSub(target,key):
    """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
        
def subStringMatchExactlyOneSub (target, key):
    nearmatches = ()
    exactmatches = subStringMatchExact(target, key)
    allmatches = subStringMatchOneSub(target, key)
    for x in allmatches:
        if x not in exactmatches:
            nearmatches += (x,)
    return nearmatches

target1 = 'atgacatgcacaagtatgcat'
target2 = 'atgaatgcatggatgtaaatgcag'

key10 = 'a'
key11 = 'atg'
key12 = 'atgc'
key13 = 'atgca'

print countSubStringMatch(target1,key12)
print countSubStringMatchRecursive(target1,key12)
print subStringMatchExact(target2,key12)
print subStringMatchOneSub(target2, key12)
print subStringMatchExactlyOneSub(target2, key12)
hendrix (Self-grade: Pretty good)
Submitted 1 year ago | Permalink
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'

#problem 1

def countSubStringMatch(target,key):
    """Given a target string and a given substring
        returns the number of occurances of the substring"""
    countOfMatches = 0
    currentPosition = 0
    while currentPosition  <= len(target):
        if find(target, key, currentPosition) == -1:
            if countOfMatches > 0:
                return countOfMatches
            else:                
                return 0
        else:
            countOfMatches += 1
            currentPosition = find(target, key, currentPosition) + 1
    return countOfMatches

def countSubStringMatchRecursive(target, key):
    """Given a target string and a given substring
        returns the number of occurances of the substring
        (recursive)"""
    currentPosition = find(target, key)
    if find(target, key) == -1:
        return 0
    else:
        return 1 + countSubStringMatchRecursive(target[currentPosition+1:], key)

#problem 2

def subStringMatchExact(target,key):
    """Given a target string and a given substring
        returns the number of occurances of the substring"""
    locationOfMatches = []
    countOfMatches = 0
    currentPosition = 0
    while currentPosition  <= len(target):
        if find(target, key, currentPosition) == -1:
            if countOfMatches > 0:
                result = tuple(locationOfMatches)
                return result
            else:                
                return ()
        else:
            countOfMatches += 1
            currentPosition = find(target, key, currentPosition)
            locationOfMatches.append(currentPosition)
            currentPosition += 1
    result = tuple(locationOfMatches)
    return result

#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
        
def constrainedMatchPair(first, second, length):
    locationOfMatches = []
    for position in first:
        for location in second:
            if position + length + 1 == location:
                locationOfMatches.append(position)
                #print "location of matches = "
                #print locationOfMatches
            #else:
                #print "position = %i, length = %i, location = %i" % (position, length, location)
    result = tuple(locationOfMatches)
    return result


#problem 4

def subStringMatchExactlyOneSub(key,target):
    result = list(subStringMatchOneSub(key, target))
    matchesWithOneSubOrLess = subStringMatchOneSub(key, target)
    matchesExact = subStringMatchExact(target, key)
    for match in matchesWithOneSubOrLess:
        if match in matchesExact:
            result.remove(match)
    readiedResult = tuple(result)
    return readiedResult
zpritchard (Self-grade: Pretty good)
Submitted 2 years ago | Permalink
from string import *

def countSubStringMatch(target, key):
    """counts the instances of key in target iteratively (exact matches only)"""
    count = 0
    while find(target,key) != -1:
        count += 1
        target = target[find(target,key)+1:]
    return count

def countSubStringMatchRecursive(target,key):
    """counts the instances of key in target recursively (exact matches only)"""
    idx = find(target,key)
    if idx == -1: return 0
    else: return 1 + countSubStringMatchRecursive(target[idx+1:],key)

def subStringMatchExact(target, key):
    """searches for instances of key in target (exact matches only)"""
    out = ()
    idx = -1
    # The target is searched from one past the previously found index
    # (initialized at -1 so as to start searching at 0) to find
    # each instance of the key in the target.
    while find(target,key,idx+1) != -1:
        idx = find(target,key,idx+1)
        out += (idx,)
    return out

def constrainedMatchPair(firstMatch, secondMatch, length):
    """for each element in firstMatch, searches for an element in secondMatch
    that represents an instance of the key with one substution"""
    out = ()
    for i in firstMatch:
        for j in secondMatch:
            if i + length + 1 == j:
                out += (i,)
    return out

#####Provided with assignment##############################################
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:]
        # 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
    return allAnswers
###########################################################################

def subStringMatchExactlyOneSub(target,key):
    """searches for instances of key in target with exactly one substitution"""
    exact = str(subStringMatchExact(target,key))
    both = subStringMatchOneSub(key, target) # has exact matches & single subs
    out = ()
    # The element is added to the output if it (1) is not in the
    # list of exact matches and (2) has not already been added
    # to the output.
    for i in both:
        if countSubStringMatch(exact,str(i)) == 0 and\
           countSubStringMatch(str(out),str(i)) == 0:
            out += (i,)
    return out
jayd (Self-grade: Pretty good)
Submitted 2 years ago | Permalink
#Problem Set 3a
#jayd
from string import *
def countSubStringMatch(target,key):
    count=0
    while target:
        location=find(target,key)
        if location == -1:
            break
        count += 1
        target=target[location + 1:]
    return count    

print countSubStringMatch("ABXXABXXAB","AB")
#Result = 3

def countSubStringMatchRecursive(target,key):
    location = find(target,key)
    if location == -1:
        return 0
    else:    
        return 1 + countSubStringMatchRecursive(target[location+len(key):],key)   

print countSubStringMatchRecursive("ABXXABXXAB","AB")
#Result = 3   

--------------------------------------------------------------

Problem set 3b
#jayd
from string import *
def subStringMatchExact(target,key):
    matchLocation = []
    start = 0
    while True:
        location = find(target,key,start)
        #break loop if no more matches found
        if location == -1:
            break
        matchLocation.append(location)
        start = location + 1
    return tuple(matchLocation)

    
#TESTS    
targets = ('atgacatgcacaagtatgcat','atgaatgcatggatgtaaatgcag')
keys = ('a','atg','atgc','atgca')
for target in targets:
    for key in keys:
        print "Target: " + target
        print "Key: " + key
        print "Solution: " + str(subStringMatchExact(target,key))    
        
#Output:
# Target: atgacatgcacaagtatgcat
# Key: a
# Solution: (0, 3, 5, 9, 11, 12, 15, 19)
# Target: atgacatgcacaagtatgcat
# Key: atg
# Solution: (0, 5, 15)
# Target: atgacatgcacaagtatgcat
# Key: atgc
# Solution: (5, 15)
# Target: atgacatgcacaagtatgcat
# Key: atgca
# Solution: (5, 15)
# Target: atgaatgcatggatgtaaatgcag
# Key: a
# Solution: (0, 3, 4, 8, 12, 16, 17, 18, 22)
# Target: atgaatgcatggatgtaaatgcag
# Key: atg
# Solution: (0, 4, 8, 12, 18)
# Target: atgaatgcatggatgtaaatgcag
# Key: atgc
# Solution: (4, 18)
# Target: atgaatgcatggatgtaaatgcag
# Key: atgca
# Solution: (4, 18)

------------------------------------------------------------


#Problem set 3d
#jayd
from string import *
#  target strings
target1 = 'atgacatgcacaagtatgcat'
target2 = 'atgaatgcatggatgtaaatgcag'
# key strings
key10 = 'a'
key11 = 'atg'
key12 = 'atgc'
key13 = 'atgca'
def subStringMatchExact(target,key):
    """Returns a tuple of the exact matches"""
    matchLocation = []
    start = 0
    while True:
        location = find(target,key,start)
        #break loop if no more matches found
        if location == -1:
            break
        matchLocation.append(location)
        start = location + 1
    return tuple(matchLocation)

    
def constrainedMatchPair(firstMatch,secondMatch,length):
    matches=[]
    for match in firstMatch:
        for match2 in secondMatch:
            if match + length + 1 == match2:
                matches.append(match)
    return tuple(matches)
    

def subStringMatchOneSub(target,key):
    """search for all locations of key in target, with one substitution"""
    allAnswers = ()
    for miss in range(0,len(key)):
        key1 = key[:miss]
        key2 = key[miss+1:]
        print 'breaking key',key,'into',key1,',',key2
        match1 = subStringMatchExact(target,key1)
        match2 = subStringMatchExact(target,key2)
        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
 
def subStringMatchExactlyOneSub(target,key):
    """Returns tuple of ONLY partial matches"""
    partialMatch = []
    exactMatch = subStringMatchExact(target,key)
    subMatch = subStringMatchOneSub(target,key)
    for match in subMatch:
        # Check to make sure the value isn't an exact match or already in our list
        if match not in exactMatch and match not in partialMatch:
            partialMatch.append(match)    
    return tuple(partialMatch)
         
matches = subStringMatchExactlyOneSub(target2,key13)
print "The partial only matches are:",matches

electracool (Self-grade: Could be better)
Submitted 2 years ago | Permalink
# 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))
#================================================================
mattijle (Self-grade: Pretty good)
Submitted 2 years ago | Permalink
def countSubStringMatch(target,key):
    count = 0
    pos = -1
    while True:
        pos = target.find(key,pos+1)
        if pos >= 0:
            count += 1
        if pos == -1:
            break
    return count

def countSubStringMatchRecursive(target,key):
    if target.find(key) >=0:
        bef,sep,target = target.partition(key)
        return 1 + countSubStringMatchRecursive(target,key)
    return 0

def subStringMatchExact(target,key):
    pos = target.find(key)
    allpos = []
    allpos.append(pos)
    while True:
        if pos <= -1:
            return tuple(allpos)
        pos = target.find(key,pos+1)
        if pos >= 0:
            allpos.append(pos)           
    return tuple(allpos)


def constrainedMatchPair(firstMatch, secondMatch, length):
    allMembers = []
    for i in firstMatch:
        for j in secondMatch:
            if i + length + 1 == j:
                allMembers.append(i)
    return tuple(allMembers)

Comments:

shaggorama
2 years ago

I like your use of the partition attribute the recursive function; very clean.

def countSubStringMatchRecursive(target,key):
    if target.find(key) >=0:
        bef,sep,target = target.partition(key)
        return 1 + countSubStringMatchRecursive(target,key)
    return 0

Sign up or log in to comment

klaymen (Self-grade: Outstanding)
Submitted 2 years ago | Permalink

copied from lecture 4's page

ps3a.py

from string import *

def countSubStringMatch(target, key):
    """counts the number of times 'key' appears in 'target'"""
    counter = 0
    next = 0
    while find(target, key, next) != -1:
        next = find(target, key, next) + 1
        counter += 1
    return counter
        
        
def countSubStringMatchRecursive(target, key):
    """counts the number of times 'key' appears in 'target'"""
    counter = 0
    if find(target, key) != -1:
        counter = 1 + countSubStringMatchRecursive(target[find(target, key)+1:], key)
    return counter

Comments:

klaymen
2 years ago

ps3b-d.py

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'



### the following procedure you will use in Problem 3
def subStringMatchExact(target,key):
    counter = []
    next = 0
    while find(target, key, next) != -1:
        counter += [find(target, key, next)]
        next = find(target, key, next) + 1
    return tuple(counter)
    

def constrainedMatchPair(firstMatch, secondMatch, length):
    counter = []
    for x in firstMatch:
        for y in secondMatch:
            if x + length + 1 == y:
                counter += [x]
    return tuple(counter)

def subStringMatchExactlyOneSub(target,key):
    exactMatch = subStringMatchExact(target, key)
    subMatch = subStringMatchOneSub(key, target)
    counter = list(subMatch)

    for x in subMatch:
        for y in exactMatch:
            if x == y:
                counter.remove(x)
    return tuple(counter)


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
anakindice
2 years ago

I tried out your constrainedMatchPair, it's a little different from mine, but seems to work better. Maybe you can help me figure out where mine is wrong. The problem with yours is if I try subStringMatchOneSub("atgc","atgc") I'm still not getting all the answers I want. I've come to the conclusion the problem is we need the size of key2 which we're not given so there's a bug in the given code. I definitely was overthinking this.

def constrainedMatchPair(firstMatch,secondMatch,length):
    pairList = []
    for n in range(0,len(firstMatch)):
       for k in range(0,len(secondMatch)):
             if n + length + 1 == k:    
                pairList.append(n)
             else:
                pass
    return tuple(pairList)
anakindice
2 years ago

I found the problem in mine. I was using range and in the for statements.

jayd
2 years ago

The use of the variable name "counter" in constrainedMatchPair() and other functions when it is storing a list of matches is a bit confusing for readers.

Sign up or log in to comment

shaggorama (Self-grade: Outstanding)
Submitted 2 years ago | Permalink

I found this assignment really hard at first. I found it became easier the more closely I read the instructions on the handout (I think I psyched myself out about what the problem required).

I did not like my solution to countSubStringMatchRecursive() (problem 1). I'm looking forward to seeing other people's solutions.

Also, the professor gave us a program whose parameters were (key, target), but then advised that we make all the other programs (target, key). Did anyone else find this strange? It almost tripped me up at the end. Did anyone just tweak the program we were given to make it follow the convention in the module?

#! /usr/bin/env python
# -*- coding: utf8 -*-

# these are some example strings for use in testing your code

#  target strings

target1 = 'atgacatgcacaagtatgcat'
target2 = 'atgaatgcatggatgtaaatgcag'

targets = (target1, target2)

# key strings

key10 = 'a'
key11 = 'atg'
key12 = 'atgc'
key13 = 'atgca'

keys = (key10, key11, key12, key13)

### 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 1

from string import *

def countSubStringMatch(target,key):
	counter = [-1]
	tryrange = range(0, len(target) + 1)
	for x in tryrange:
		location = find(target, key, x) 
		if location not in counter:
			counter.append(location)
	if counter == -1:
		return 0
	else:
		counter.remove(-1)		
		return len(counter)

def helpcountSubStringMatchRecursive(target, key, incr, counter):
	position = find(target, key, incr)
	incr += 1	
	if position not in counter:
		counter.append(position)
		helpcountSubStringMatchRecursive(target, key, incr, counter)
	if incr -1 >= len(target):
		return counter
	else:
		helpcountSubStringMatchRecursive(target, key, incr, counter)

def countSubStringMatchRecursive(target, key):
	counter = [-1] 
	helpcountSubStringMatchRecursive(target, key, 0, counter)
	counter.remove(-1)
	return len(counter)

## PROBLEM 2

def subStringMatchExact(target,key):
	counter = [-1]
	tryrange = range(0, len(target) + 1)
	for x in tryrange:
		location = find(target, key, x) 
		if location not in counter:
			counter.append(location)
	if counter == -1:
		return 0
	else:
		counter.remove(-1)	
		return tuple(counter)

## PROBLEM 3

def constrainedMatchPair(firstMatch, secondMatch, length):
	matches = []
	for n in firstMatch:
		for k in secondMatch:
			m = length			
			if n + m + 1 == k:
				matches.append(n)
	return tuple(matches)


## PROBLEM 4

def subStringMatchExactlyOneSub(target,key):
	exact = subStringMatchExact(target,key)	
	close = subStringMatchOneSub(key,target)
	onesub = []
	for index in close:
		if index not in exact:
			onesub.append(index)
	return tuple(onesub)

##TEST FUNCTION
for target in targets:
	for key in keys:
		print "\nTarget: ", target
		print "Key: ", key		
		print subStringMatchExactlyOneSub(target, key)

Comments:

anakindice
2 years ago

I've been working on constrainedMatchPair and had the exact smae code as yours. When I put subStringMatchOneSub("atgc","atgc") In the idle, I feel like i should get back 4 possibles matches at 0, but don't get any.

electracool
2 years ago

I dont think you would need that check for counter if in the list , you are incrementing the index anyways, recursive function can be done better :). Have a look at list comprehensions, generators and sets. Just my two cents.

shaggorama
2 years ago

Yeah, i have trouble writing good recursive functions. At least I know my weakness!

Sign up or log in to comment