Web-Books
in the Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Page - 129 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

Page - 129 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Image of the Page - 129 -

Image of the Page - 129 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Text of the Page - 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.
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Title
Programming for Computations – Python
Subtitle
A Gentle Introduction to Numerical Simulations with Python 3.6
Volume
Second Edition
Authors
Svein Linge
Hans Petter Langtangen
Publisher
Springer Open
Date
2020
Language
English
License
CC BY 4.0
ISBN
978-3-319-32428-9
Size
17.8 x 25.4 cm
Pages
356
Keywords
Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
Category
Informatik
Web-Books
Library
Privacy
Imprint
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python