Simple types and strings (1) P56724


Statement
 

pdf   zip

html

You have to program several functions. In each case, few lines of code are enough.

  1. Write a function drawH(n) that given an odd integer n≥ 3 prints a letter H of size n formed with symbol *. Follow the pattern of the example below.
  2. Write a function area_circle(r) that given a float number r≥ 0 returns, rounded to the hundredth, the area of the circle of radius r. Use the pi constant defined in the math module.
  3. Write a function slow_pi_aprox(n) that given a non negative integer n computes 4∑k = 0n (−1)k/2k + 1 rounded to the hundredth.
  4. Write a function is_univariate_number(n) that given a non negative integer returns a boolean pointing out whether n is represented using only one digit. For instance 22222 is univariate but 22322 is not.
  5. Write a boolean function is_univariate_word(s) that given an string s returns True if and only if s is formed using only one letter. For instance word xxXxXXx is univariate but xxXxy is not. We assume s is non empty and all characters of s are letters.

Scoring

Every function counts 20 points.

Sample session
>>> drawH(5)
*   *
*   *
*****
*   *
*   *
>>> area_circle(2.5)
19.63
>>> slow_pi_aprox(50)
3.16
>>> is_univariate_number(22322)
False
>>> is_univariate_word("xxXxXXx")
True
Information
Author
Jorge Castro
Language
English
Translator
Original language
Catalan
Other languages
Catalan Spanish
Official solutions
Python
User solutions