Simple types and strings (2) P16327


Statement
 

pdf   zip

You have to program several functions. In each case, few lines of code are enough. Warning: do not use the split string method.

  1. Write a function @um_count(s)@ that given an string ss returns the number of times character u is followed by character m in string ss.

  2. Write an integer function @word_count(s)@ that returns the number of words in string ss. We assume all characters of ss are letters and spaces.

  3. Write a function @kth_word(s, k)@ that given a string ss and an integer k1k\ge 1 returns the $k{\it th}$ word in string ss. If ss has less than kk words it returns the empty string. We assume all characters of ss are letters and spaces.

  4. Write a function @suc_word(s)@ that given a string ss and returns the first word in string ss that has some uppercase letter. If all the letters in ss are lowercase it returns the empty string. We assume all characters of ss are letters and spaces.

  5. Write a function @drawA(n)@ that given an odd integer n3n\ge 3 prints a letter A of size nn formed with symbol .

Scoring

Every function counts 20 points.

Sample session

Sample session
>>> um_count("Qui invenit amicum invenit thesauruM")
1
>>> word_count("Alea iacta          est")
3
>>> kth_word("Alea iacta est", 3)
est
>>> suc_word("qui invenit amiCum invenit thesauruM")
amiCum
>>> drawA(5)
    *
   * *
  *****
 *     *
*       *
Information
Author
Jorge Castro
Language
English
Translator
Original language
Catalan
Other languages
Catalan Spanish
Official solutions
Python
User solutions