diff --git a/source/Collection_Data_Types.rst b/source/Collection_Data_Types.rst index 2584f1c09d34bb2dd7a886517ad3b00a179ba6f9..768217c94253cc3de44ed96eaa79f171a1a711a0 100644 --- a/source/Collection_Data_Types.rst +++ b/source/Collection_Data_Types.rst @@ -1,7 +1,3 @@ -.. 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 -------- diff --git a/source/Control_Flow_Statements.rst b/source/Control_Flow_Statements.rst index eaa6f367997369073647c02e77b4577da947c1c2..87b9f0dafc21467321cb54dded2a8826025dd77c 100644 --- a/source/Control_Flow_Statements.rst +++ b/source/Control_Flow_Statements.rst @@ -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)] + + diff --git a/source/_static/code/fibonacci_iteration.py b/source/_static/code/fibonacci_iteration.py new file mode 100644 index 0000000000000000000000000000000000000000..bb8ee8d878916c47c9500adb357f65ce67381b9c --- /dev/null +++ b/source/_static/code/fibonacci_iteration.py @@ -0,0 +1,13 @@ + +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