The exercise says the following:
"""
Pairwise comparision of DNA sequences is a popular technique used in Bioinformatics. It usually involves some scoring scheme to express the degree of similarity. Write a function that compares two DNA sequences based on the following scoring scheme: +1 for a match, +3 for each consecutive match and -1 for each mismatch.

Examples

>>> print pairwiseScore("ATTCGT", "ATCTAT")
    ATTCGT
    ||   |
    ATCTAT
    Score: 2 
    >>> print pairwiseScore("GATAAATCTGGTCT", "CATTCATCATGCAA")
    GATAAATCTGGTCT
     ||  |||  |   
    CATTCATCATGCAA
    Score: 4 
    >>>

"""
so i wrote the following, which was continuously working until i started playing with it, now i cant find whats wrong with it! please help me by pointing out where i went wrong?

def pariwiseScore(seqA, seqB):
	h = []
	c = []
	for x in range(0,len(seqA)):
		if seqA[x] == seqB[x]:
			h.append('|')
		else:
			h.append(' ')
		if len(h) > 1:
			if h[x] == '|':
				try:
					if h[x] == h[x-1]:
						c.append(3)
				except IndexError:
					c.append(1)

			elif h[x] == ' ':
				c.append(-1)
		else:
			if h[x] == '|':
				c.append(1)
			elif h[x] == ' ':
				c.append(-1)
	a = "".join(h)
	return seqA, '\n', a, '\n', seqB, '\n', 'Score: %I' % sum(c)

Any help is much appreciated.

P.s i had it before and it was returning the correct sum, but i copied it into a notepad and accidentally closed it. Now i cant get it working

noticed my problem, renamed it to pariwise and was still testing for pairwise, also flawed format specifier

its still not returning right, why is it that when i use print it works fine but when return and print the results of the function it wont?

def pairwiseScore(seqA, seqB):
	h = []
	c = []
	for x in range(0,len(seqA)):
		if seqA[x] == seqB[x]:
			h.append('|')
		else:
			h.append(' ')
		if len(h) > 1:
			if h[x] == '|':
				if h[x] == h[x-1]:
					c.append(3)
				else:
					c.append(1)
			elif h[x] == ' ':
				c.append(-1)
		else:
			if h[x] == '|':
				c.append(1)
			elif h[x] == ' ':
				c.append(-1)
	a = "".join(h)
	return seqA, '\n', a, '\n', seqB, '\n', 'Score: %d' % sum(c)

is my code and it results as follows:

>>> print pairwiseScore("ATTCGT", "ATCTAT")
('ATTCGT', '\n', '||   |', '\n', 'ATCTAT', '\n', 'Score: 2')

how can i remedy this?

It seems like you're returning a tuple and not a string.
try this and see what happens:

for item in pairwiseScore("ATTCGT", "ATCTAT"):
    print(item)

or maybe you could try returning str(return value) in the function itself.
Good Luck.

Try

return "".join((seqA, '\n', a, '\n', seqB, '\n', 'Score: %d' % sum(c)))

edit: don't use tabs to indent python code, use 4 spaces.

Try

return "".join((seqA, '\n', a, '\n', seqB, '\n', 'Score: %d' % sum(c)))

edit: don't use tabs to indent python code, use 4 spaces.

Thanks, very helpful! i wish i wasn't so bad at this stuff, usually im a fairly quick learner but i guess im just not suited to programming!

Hi, I am having trouble with this exercise too. For some reason I can get this to work in console, but when I submit the answer to pyschool it doesn't work.

def pairwiseScore(seqA, seqB): 
    length = len(seqA)
    counter = 0
    sequence = ""
    score = 0
    while counter < 1:
        if seqA[counter] == seqB[counter]:
            sequence = sequence + "|"
            counter = counter + 1
            score = score + 1
    while counter < length:
        if seqA[counter] == seqB[counter] and sequence[-1] == "|":
            sequence = sequence + "|"
            counter = counter + 1
            score = score + 3
        elif seqA[counter] == seqB[counter] and sequence[-1] != "|":
            sequence = sequence + "|"
            counter = counter + 1
            score = score + 1    
        elif seqA[counter] != seqB[counter]:
            sequence = sequence + " "
            counter = counter + 1
            score = score - 1
    print "%s \n %s \n %s \nScore: %d" % (seqA, sequence, seqB, score)

You seem to have spaces between the parts of answer, probably answer should be direct concatenation of parts.

You are not returning value, so value of your function will be None, also the result does not correspond to the test print when I run the code, first test wrong and hangup for second.

ATTCGT
 ||   |
 ATCTAT
Score: 2
None

Here my little advanced way:

def pairwiseScore(seqA, seqB):
    matches = ''.join('|' if a==b else ' ' for a,b in zip(seqA, seqB))
    # count does not deal with overlapping matches so must use zip for two extra points for consecutive matches
    score = matches.count('|') - matches.count(' ') + 2 * sum( a==b=='|' for a,b in zip(matches, matches[1:]))
    return '\n'.join((seqA, matches, seqB, 'Score: %i' % score))

print(pairwiseScore("ATTCGT", "ATCTAT"))
print('')
print(pairwiseScore("GATAAATCTGGTCT", "CATTCATCATGCAA"))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.