Skip to content
Snippets Groups Projects
Commit 34e432d6 authored by Bertrand  NÉRON's avatar Bertrand NÉRON
Browse files
Conflicts:
	source/Collection_Data_Types.rst
	source/Control_Flow_Statements.rst
parents 150ca035 925831e4
No related branches found
No related tags found
No related merge requests found
.. sectnum::
:start: 4
.. _Collection_Data_types:
*********************
......@@ -165,6 +161,7 @@ solution ::
>>> list(set(l))
<<<<<<< HEAD
Exercise
......@@ -249,12 +246,11 @@ solution ::
>>> for item in l:
>>> if item not in uniq:
>>> uniq.append(item)
=======
>>>>>>> 925831e40bd67e747a3aee7320d83c92e5b6fdc1
solution ::
>>> uniq_items = set()
>>> l_uniq = [x for x in l if x not in uniq_items and not uniq_items.add(x)]
Exercise
--------
......
......@@ -5,6 +5,8 @@
Control Flow Statements
***********************
Exercises
=========
Exercise
--------
......@@ -22,21 +24,10 @@ The fibonacci suite can be defined as following:
|
| F\ :sub:`n` = F\ :sub:`n-1` + F\ :sub:`n-2`
::
#This program calculates the Fibonacci sequence
a = 0
b = 1
count = 0
max_count = 10
while count < max_count:
count = count + 1
print a,
new_number = a + b
#set new a and b for the next iteration
a = b
b = new_number
.. literalinclude:: _static/code/fibonacci_iteration.py
:linenos:
:language: python
We will see another way more elegant to implement the fibonacci suite in :ref:`Advance Programming Techniques` section.
......@@ -52,7 +43,7 @@ display the largest element in list (containing float or integer only)?::
highest = i
print i
Exercise
--------
......@@ -82,19 +73,27 @@ and the 2 dna fragments: ::
agcagcttaaatcggagagaattccatttactggccagggtaagagttttggtaaatatatagtgatatctggcttg"""
| which enzymes cut the dna_1 get the name of the enzymes and all their positions of binding site?
| do the same for dna_2
| give the name of the enzymes that cut the dna_1 but not the dna_2?
| the dna_2 ?
| the dna_1 but not the dna_2?
::
dna_1 = dna_1.replace('\n', '')
dans_2 = dna_2.replace('\n', '')
enzymes = [ecor1, ecor5, bamh1, hind3, taq1, not1, sau3a1, hae3, sma1]
digest_1 = []
for enz in enzymes:
pos = dna_1.find(enz.sequence)
if pos != -1:
digest_1.append(enz)
We looking for the first a cutting site, then we search again starting at the first nucleotid after the begining of the match
with this first algorithm we find if an enzyme cut the dna but we cannot find all cuts in the dna for an enzyme.
If we find a cutting site, we must search again starting at the first nucleotid after the begining of the match
until the end of the the dna, for this we use the start parameter of the find function, and so on.
As we don't know how many loop we need to scan the dna until the end we use a ``while`` loop testing for the presence of a cutting site.::
enzymes = [ecor1, ecor5, bamh1, hind3, taq1, not1, sau3a1, hae3, sma1]
digest_1 = []
for enz in enzymes:
pos = dna_1.find(enz.sequence)
......@@ -113,6 +112,7 @@ As we don't know how many loop we need to scan the dna until the end we use a ``
cut_dna_2 = set(digest_2)
cut_dna_1_not_dna_2 = cut_dna_1 - cut_dna_2
but we want also the position, for instance to compute the fragments of dna. ::
digest_1 = []
......@@ -126,6 +126,7 @@ but we want also the position, for instance to compute the fragments of dna. ::
from operator import itemgetter
digest_1.sort(key=itemgetter(1))
print [(e.name, pos) for e, pos in digest_1]
digest_2 = []
for enz in enzymes:
......@@ -141,4 +142,28 @@ but we want also the position, for instance to compute the fragments of dna. ::
cut_dna_2 = set([e.name for e, pos in digest_2])
cut_dna_1_not_dna_2 = cut_dna_1 - cut_dna_2
\ No newline at end of file
Exercise
--------
From a list return a new list without any duplicate, but keeping the order of items.
For example: ::
>>> l = [5,2,3,2,2,3,5,1]
>>> uniqify_with_order(l)
>>> [5,2,3,1]
solution ::
>>> uniq = []
>>> for item in l:
>>> if item not in uniq:
>>> uniq.append(item)
solution ::
>>> uniq_items = set()
>>> l_uniq = [x for x in l if x not in uniq_items and not uniq_items.add(x)]
fib_suite = []
n = 0
while n < 10:
if n == 0:
fib_suite.append(0)
elif n == 1:
fib_suite.append(1)
else:
res = fib_suite[n-1] + fib_suite[n-2]
fib_suite.append(res)
n += 1
print ', '.join([str(i) for i in fib_suite])
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment