Hello Everyone,
I am really sorry, if I don't know yet what a "PSEUDO-CODE" is! It's my first semester, I am not acquinted with those TERMs yet. So, forwarding here my entire code in order to find a better algorithm, would be better since I am not acquinted with pseudo-code yet. The reason why I forward the code is to find a better algorithm than mine of how to compute polynomials; because, as you would see in the forwarded code below, I have spent a lot of time trying to figure out how to get a short-cut algorithm to the last exercise(5), that is, polynomials of N-DEGREE (eg. power 5 or higher). @spognardi, @di_ciccio
The othe exercises were pretty easy, except the polynomial of 2nd-degree (it even get harder, when I try to compute the solution of higher degrees). The code is below:
"""1). Write a python script that takes in input a float number,
calculates its cube root and prints it on screen"""
def cube_root(c):
d = c ** (1/3) # d is the cube root of c
print(d, 'is the cube root of', c,': for exercise 1')
cube_root(64)
cube_root(27)
cube_root(8)
# 2). Write a python script that takes a person name (name)
# and the name of a fruit (fruit) and prints: "(name) loves to eat (fruit)
def eater(name, fruit):
print(name, 'loves to eat', fruit, ': for exercise 2')
eater('YUSUPHA', 'MANGO')
eater('MATTEO', 'MELA')
eater('ELLA', 'BANANA')
# 3). Write a python script that takes in input two floats a and b and
# calculates and prints c, corresponding to the hypotenuse of the rectangle
# triangle having for cathets two sides of length a and b, respectively
def hypotenuse(a, b):
c = (a ** 2 + b ** 2)
c = c ** (1/2)
print('the hypotenuse of a triangle of sides', a, ', ', b, 'is', c, ': for exercise 3')
hypotenuse(4, 5)
hypotenuse(3, 4)
hypotenuse(12, 5)
#4). Write a python script that takes in input three floats a, b, c,
# and calculates and prints the expression:
def expression(a, b, c):
d = (b**3 * c**4) / (3 * a)
y = a**2 + d -b + a * c # y is asighned with the original expression
print(y, 'is the answer to the above expression: exercise 4')
expression(9, 5, 2)
expression(89, 1, 5)
expression(2, 4, 8)
#5). Write a python script that takes three floats a, b, c, and calculates
# and prints the two x roots of the quadratic equation:
def roots_of_quad(a, b, c):
d = b**2 - 4 * a * c
d = d ** (1/2)
x1 = (-b + d) / (2 * a) # first or the positive(+) root
x2 = (-b - d) / (2 * a) # second or the negative(-) root
print(x1, 'is the positive root: for exercise 5')
print(x2, 'is the negative root: for exercise 5')
roots_of_quad(1, 2, 1)
roots_of_quad(2, 5, 3)
roots_of_quad(4, -10, 6)
3.9999999999999996 is the cube root of 64 : for exercise 1
3.0 is the cube root of 27 : for exercise 1
2.0 is the cube root of 8 : for exercise 1
YUSUPHA loves to eat MANGO : for exercise 2
MATTEO loves to eat MELA : for exercise 2
ELLA loves to eat BANANA : for exercise 2
the hypotenuse of a triangle of sides 4 , 5 is 6.4031242374328485 : for exercise 3
the hypotenuse of a triangle of sides 3 , 4 is 5.0 : for exercise 3
the hypotenuse of a triangle of sides 12 , 5 is 13.0 : for exercise 3
168.07407407407408 is the answer to the above expression: exercise 4
8367.340823970037 is the answer to the above expression: exercise 4
43706.666666666664 is the answer to the above expression: exercise 4
-1.0 is the positive root: for exercise 5
-1.0 is the negative root: for exercise 5
-1.0 is the positive root: for exercise 5
-1.5 is the negative root: for exercise 5
1.5 is the positive root: for exercise 5
1.0 is the negative root: for exercise 5