[Solved] CS5007 Homework 1

$25

File Name: CS5007_Homework_1.zip
File Size: 160.14 KB

SKU: [Solved] CS5007 Homework 1 Category: Tag:
5/5 - (1 vote)

1 Arithmetic expressions

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.

The values of the three rational variables a, b and c are assigned at the beginning of the template module and should not be modified.

  1. 4ab + bc
  2. b3
  1. ab
  2. (ac + ba)/bc
  3. a2 b4 + 2ab

2 Expressions, Python operators

Write the following Python expressions into the designated places in the template module. Run the module.

  1. (8//6) 6
  2. (8/6) 6

Explain in a comment line why the value of expressions a. and b. are different.

3 Boolean expressions

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.

The values of the Boolean variables a, b and c are assigned at the beginning of the template module and should not be modified. Recall that is or, is and, is not and is implication.

  1. a (b c)
  2. a (b c)
  3. (a b) (c)
  4. a (b (c))

4 Function

Wind chill is defined by the following formula:

Wind Chill = 13.13 + 0.621 T 12.1 V 0.15 + 0.3967 T V 0.16

In this formula, T is the temperature in Celsius and V is the wind velocity in kilometers per hour.

  • 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.1156286189848C

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.

  • 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

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

This part is not mandatory but may provide extra credits, up to 5 points. Consider the following Python function.

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.

B You may print the result of the function call with values 3, 4 and 5 (i.e., successive calls), for instance. In addition, observe that the function is recursively called by itself in the else statement. What is the effect of this instruction?

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.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] CS5007 Homework 1[Solved] CS5007 Homework 1
$25