lab5

In this assignment we repeat and excercise skills from the lectures and previous labs.

count_vowels(s)

Write a function count_vowels(s) that returns the number of letters a, e, i, o, u, A, E, I, O, U in a given string s (the return value is of type integer).

Examples:

In [ ]: count_vowels('This is a test')
Out[ ]: 4

In [ ]: count_vowels('aoeui')
Out[ ]: 5

In [ ]: count_vowels('aoeuiAOEUI')
Out[ ]: 10

In [ ]: count_vowels('N0 v0w3ls @t @ll ln thls strlng')
Out[ ]: 0

vector_product3(a, b)

Write a function vector_product3(a, b) that takes two sequences of numbers. Both sequence a and sequence b have three elements. With inputs a=[ax, ay, az] and b=[bx, by, bz], the function should return a list which contains the vector product of 3d-vectors a and b, i.e. the return value is the list:

[ay * bz - az * by, az * bx - ax * bz, ax * by - ay * bx].

Examples:

In [ ]: vector_product3([1, 0, 0], [0, 1, 0])
Out[ ]: [0, 0, 1]

In [ ]: vector_product3([1, 2, 4], [3, 5, 6])
Out[ ]: [-8, 6, -1]

powers(n, k)

A function powers(n, k) that returns the list [1, n, n^2, n^3, ..., n^k] where k is an integer. Note that there should be k + 1 elements in the list.

Example:

In [ ]: powers(2, 3)
Out[ ]: [1, 2, 4, 8]

In [ ]: powers(0.5, 2)
Out[ ]: [1.0, 0.5, 0.25]

seq_mult_scalar(a, s)

Write a function seq_mult_scalar(a, s) which takes a list of numbers a and a scalar (i.e. a number) s. For the input a = [a0, a1, a2,.., an] the function should return [s * a0, s * a1, s * a2, ..., s * an].

Example:

In [ ]: seq_mult_scalar([-4, 9, 1], 10)
Out[ ]: [-40, 90, 10]

traffic_light(load)

A function traffic_light(load) that takes a floating point number load. The function should return the string:

  • “green” for values of load below 0.7.

  • “amber” for values of load equal to or greater than 0.7 but smaller than 0.9

  • “red” for values of load equal to 0.9 or greater than 0.9

Example:

In [ ]: traffic_light(0.5)
Out[ ]: 'green'

Please submit your file lab5.py for this assignment.

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

End of lab5.