CS5007 – Solved

$ 20.99
Category:

Description

HomeWork 1 – CS5007
Thierry Petit
TPetit@WPI.edu
Learning objectives
Learn about variables and expressions.
Understand basic mathematical and logical operators.
Define a function and call it to perform a calculation.
Print information on the output window.
First use of If/else statement.
Dates
Write your code and comments on the template HW1.py and save your file as FIRSTNAME-LASTNAME-HW1.py. Upload it on the course website (no delay accepted).
1 Arithmetic expressions (15 points)
Convert each of the following mathematical expression in an equivalent Python expression. Write the expression in the designated space in your template module a line of Python code.
a. 4ab + bc
b. b3

c. ab
d. (ac + ba)/bc
e. a2 − c4 + 2ab
2 Expressions, Python operators (5 points)
Write the following Python expressions into the designated places in the template module. Run the module.
a. (8//6) ∗ 6
b. (8/6) ∗ 6
Explain in a comment line why the value of expressions a. and b. are different.
3 Boolean expressions (20 points)
Convert each of the following logical expression in an equivalent Python expression.
Write the expression in the designated space in your template module a line of Python code.
a. a ∧ (b ∨ c)
b. b → (a ∨ c)
c. (a ∧ b) ∨ (¬c)
d. a → (b → (¬c))
4 Function (30 points)
Wind chill is defined by the following formula:
Wind Chill = 13.13 + 0.628 × T − 12.1 × V 0.15 + 0.3967 × T × V 0.155
In this formula, T is the temperature in Celsius and V is the wind velocity in kilometers per hour.
1) Define a function WindChill in your template module to calculate the wind chill for any temperature and velocity. For instance, a call to WindChill(−20,30) prints the following result:
At -20C and 30 kph winds it feels like -33.02508884265753C
Your function should NOT return anything, but PRINT the result on IDLE. Call your windChill function with arguments -20 for T and 30 for V.
2) Write a similar function, named WindChill2, that does not print but returns the result. Write an appropriate statement using this second function, so as to print this returned result.
5 Errors in a Python program (30 points)
def euc(x1,y1,x2,y1): lambda = (x1-x2)**2 gamma = (y1-y2)**2
return((lambda+gamma)**(1//2)) result = euc(1,3,2,7) print(“result”)
Function euc was written to calculate a distance with following formula:
p((x1 − x2)2 + (y1 − y2)2
Unfortunately, the function definition, call and print statement contain 5 errors. Explain in a command line each error, within the template module. Re-write correctly the function and the call/print statement in the template module.
6 Extra credit
def myFct(value): if(value==1):
return value
else: return value+myFct(value-1)
Explain in a comment line the result returned by the function (2 points). You should not paraphrase the code (this will not give you points), but rather explain with one simple sentence what is the result of a call to this function.
Write a simpler function myFct in the template module, that returns exactly the same result, but encoded without using if and else and without the recursion call myFct(value-1). Your function must NOT use more advanced concepts such as while and for loops. (3 points).

Reviews

There are no reviews yet.

Be the first to review “CS5007 – Solved”

Your email address will not be published. Required fields are marked *