lab2

New topics: if-then-else, math module, PEP8

Relevant supplementary videos :

Relevant Socratica video:

signum(x)

Write a function signum(x) that:

  • returns 1 if x \(> 0\)

  • returns 0 if x \(= 0\)

  • returns -1 if x \(< 0\)

Example:

In [ ]: signum(42)
Out[ ]: 1

seconds2days(n)

Implement a function seconds2days(n) which accepts a number n of seconds (either as int or float) and converts the number of seconds in the corresponding number of days. The function should return the number of days as a floating point variable.

Example:

In [ ]: seconds2days(43200)
Out[ ]: 0.5

For your entertainment: how many days are 10! seconds? (10!=3628800).


box_surface(a, b, c)

Implement a function box_surface(a, b, c) that computes and returns the surface area of a box (i.e. cuboid) with the edge lengths a, b, and c. Imagine we need to paint the surface of the box and thus need to know its area to buy the right amount of paint. (Remember that the surface of one rectangle with sides \(a\) and \(b\) is given by \(ab\).)

Example:

In [ ]: box_surface(1, 1, 1)
Out[ ]: 6

In [ ]: box_surface(2, 2, 0)
Out[ ]: 8

triangle_area(a, b, c)

Write a function triangle_area(a, b, c) that computes and returns the area \(A\) of a triangle with edge lengths \(a\), \(b\), and \(c\). You can use the equation

\[A = \sqrt{s(s-a)(s-b)(s-c)} \qquad \mathrm{with} \qquad s = \frac{a + b + c}{2}\]

Examples:

In [ ]: triangle_area(1, 1, 1)
Out[ ]: 0.4330127018922193

In [ ]: triangle_area(2, 1, 1)
Out[ ]: 0.0

In [ ]: triangle_area(6, 5, 5)
Out[ ]: 12.0

Implement the functions in a file lab2.py and submit this to the advertised computing feedback email address with subject lab2.

For the following labs, use the same naming scheme.

Additional (voluntary) tasks are available in lab2-extra.

End of lab2.