Functions

  • Fibonacci numbers Definition:     

f0 = 0

f1 = 1

fn = fn-1 + fn-2 ∀n > 2

  • This can be written as recursive
    function ( a function which calls
    itself again)
def fib(x):
    if (x == 0):
       return 0
    elif (x == 1):
       return 1
    else:
       return fib(x-1) + fib(x-2)

 

for i in range(10):
    print(fib(i))

Example: Similarity of two DNA sequences

Biological intuition:

Sequence = string of characters

Informal:

Arrange sequences in such a way that
similarity becomes visible

GATCGTTCG

    | |     | | |

CATGGTTGA

 

Distance function:

LaTeX: d\left(a,b\right)\:=\:\begin{cases}
    0, & \text{if } a= b\\
    1,              & \text{otherwise}
\end{cases}d(a,b)={0,if a=b1,otherwise

Example:

A = GATCGTTCG, B = CATGGTTGA

 

G A T C G T T C G

     |  |      |  |  |

C A T G G T T G A

     |  |      |  |  |

           1  0 0  1  0 0  0  1  1 

distance = 4

# Simple calculation of distances

A = "GATCGTTCG"

B = "CATGGTTGA"

distance = 0

# Calculate the distance for each position and sum it up


for i in range(len(A)):
if (A[i] == B[i]):
  d = 0
else:

  d = 1

distance = distance + d

print("Distance of sequences A and B: ", distance)

Distance of Sequences with Function

# Simple calculation of distances
A = "GATCGTTCG"
B = "CATGGTTGA"


def d(a, b):

distance = 0

for i in range(len(a)):

  if(a[i] != b[i]):

   distance += 1

return distance


distance
= d(A, B)

print("Distance of sequences A and B: ", distance)