Exercise 01.1 - Solution

Create a function that receives two inputs a and b, and returns the product of the a decimal of pi and the b decimal of pi.

i.e, 
pi = 3.14159
if a = 2 and b = 4
result = 4 * 5
result = 20

Caveats:

  • a and b are between 1 and 15
  • decimals positions 1 and 2 are 1 and 4, respectively. (remember that python start indexing in 0)

Crear una funcion que recibe dos parametros a y b. Y evalua la multiplicacion del a decimal de pi y el b decimal de pi.


In [1]:
from math import pi
def mult_dec_pi(a, b):
    
    # Check that a and b are within range
    if (a<1) or (a>15) or (b<1) or (b>15):
        return "Error a or b are out of the expected ranges"
    
    # Get pi decimals
    pi_dec = str(pi)[2:]
    
    return float(pi_dec[a-1]) * float(pi_dec[b-1])

In [2]:
mult_dec_pi(a=2, b=4)
# 20.0


Out[2]:
20.0

In [3]:
mult_dec_pi(a=5, b=10)
# 45.0


Out[3]:
45.0

In [4]:
mult_dec_pi(a=14, b=1)
# 9.0


Out[4]:
9.0

In [5]:
mult_dec_pi(a=6, b=8)
# 10.0


Out[5]:
10.0

In [6]:
# Bonus
mult_dec_pi(a=16, b=4)
# 'Error'


Out[6]:
'Error a or b are out of the expected ranges'