Seite - 129 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 129 -
Text der Seite - 129 -
5.7 Exercises 129
gene[i], and a substring from index i up to, but not including j, is created by
gene[i:j].
a) Write a functionfreq(letter, text) that returns the frequency of the letter
letter in thestringtext, i.e., thenumberofoccurrencesofletterdividedby
the lengthoftext.Call the functiontodeterminethefrequencyofCandG in the
gene stringabove.Compute the frequencybyhand too.
b) Write a function pairs(letter, text) that counts how many times a pair
of the letterletter (e.g.,GG)occurswithin thestringtext.Use the function to
determinehowmanytimesthepairAAappears in thestringgeneabove.Perform
a manualcounting too tocheck theanswer.
c) Write a functionmystruct(text) that counts thenumberofacertain structure
in thestringtext.Thestructure isdefinedasG followedbyAorTuntil adouble
GG.Performamanualsearchfor thestructure too tocontrol thecomputationsby
mystruct.
Filename:count_substrings.py.
Remarks Youaresupposedtosolvethetasksusingsimpleprogrammingwithloops
andvariables.Whilea)andb)arequitestraightforward,c)quicklyinvolvesdemand-
ing logic. However, there are powerful tools available in Python that can solve the
tasks efficiently in very compact code: a) text.count(letter)/len(text); b)
text.count(letter*2);c)len(re.findall(’G[AT]+?GG’, text)).That is,
thereis richfunctionalityforanalysisof text inPythonandthis isparticularlyuseful
inanalysisofgenesequences.
Exercise5.8:ComputeCombinationsofSets
Consideran IDnumberconsistingof two lettersand threedigits, e.g.,RE198.How
many different numbers can we have, and how can a program generate all these
combinations?
If a collection of n things can have m1 variations of the first thing, m2 of
the second and so on, the total number of variations of the collection equals
m1m2 · · ·mn. In particular, the ID number exemplified above can have 26 · 26 ·
10 ·10 ·10 = 676,000variations. To generate all the combinations,we must have
five nested for loops. The first two run over all letters A, B, and so on to Z, while
thenext three runoverall digits0,1,.. .,9.
To convince yourself about this result, start out with an ID number on the form
A3 where the first part can vary amongA, B, and C, and the digit can be among1,
2, or 3. We must start with A and combine it with 1, 2, and 3, then continue with
B, combinedwith 1, 2, and 3, and finally combineC with 1, 2, and3. A double for
loopdoes thework.
a) In a deck of cards, each card is a combinationof a rankand a suit. There are 13
ranks: ace (A), 2, 3, 4, 5, 6, 7, 8, 9, 10, jack (J), queen (Q), king (K), and four
suits: clubs(C),diamonds(D),hearts (H),andspades(S).Atypicalcardmaybe
D3.Write statements thatgeneratea deckofcards, i.e., all thecombinationsCA,
C2,C3, andso on toSK.
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Band
- Second Edition
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2020
- Sprache
- englisch
- Lizenz
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 356
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik