Created
November 5, 2017 14:19
-
-
Save jerrypy/0a6c0f6a4fc82ede1dd04fd56c74325c to your computer and use it in GitHub Desktop.
MIT 6.00 Assignments solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from ps3b import subStringMatchExact | |
def constrainedMatchPair(firstMatch, SecondMatch, lenth): | |
ans = () | |
for i in firstMatch: | |
for j in SecondMatch: | |
if i + 1 + lenth == j: | |
ans += (i, ) | |
return ans | |
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("ATGC", "ATGACATGCA")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment