Hello,how can I make the 2 for`s to work at the same time to find out the diferences between A and B ? Thank you.

``````A = "gtggcaacgtgc"
B = "gtagcagcgcgc"
C = "gcggcacagggt"
D = "gtgacaacgtgc"

def cromo(a, b):
inc = 0
for i in a:
for j in b:
if i != j:
inc = inc + 1;
print inc

cromo(A, B)``````
3
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by woooee

You can use this

``````# python 2
A = "gtggcaacgtgc"
B = "gtagcagcgcgc"
C = "gcggcacagggt"
D = "gtgacaacgtgc"

def cromo(a, b):
assert len(a) == len(b)
return sum(a[i] != b[i] for i in xrange(len(a)))

print cromo(A, B)``````

Boolean values can be summed, with True=1 and False=0.

Only one for loop would be used because you want to access the same relative position in each string. Also, do not use "i" as a variable name as it looks too much like the number 1.

``````def cromo(a, b):
inc = 0
if len(a) != len(b):
print "both strings must be the same length"
return -1

for ctr in range(len(a)):
if a[ctr] != b[ctr]:
inc = inc + 1
print inc
return inc

A = "gtggcaacgtgc"
B = "gtagcagcgcgc"
print cromo(A, B)``````

Edited by woooee: n/a