Week 1, Problem 4: Fun with Functions
[30 points; individual or pair]

 

 

This problem asks you to write the following Python functions using recursion. Be sure to test these functions carefully. Be sure to include a docstring under the signature line of each function. The docstring should indicate what the function computes (outputs) and what its inputs are or what they mean.

Please put all of your functions for this problem in a single hw1pr4.py file. Thus, all of the parts of this problem will be submitted in a single file. Be sure to name your functions exactly as specified.

Also, the mult, dot, ind, and scrabbleScore functions should all be done using recursion. Compare them to the power, mysum, mylen, and sajak functions we did in class this week.

  1. mult( n, m ) should output the product of the two integers n and m. Since this would be a bit too easy if the multiplication operator * were used, for this function, you are limited to using addition/subtraction/negation operators, along with recursion. (Use the power function we did in class as a guide.) Some examples:

>>> mult( 6, 7 )

42

 

>>> mult( 6, -3 )

-18

 

  1. dot( L, K ) should output the dot product of the lists L and K. If these two input lists are not of equal length, dot should output 0.0. If these two lists are both empty, dot also should output 0.0. You should assume that the input lists contain only numeric values. (Compare this with the mysum example we did in class, but be sure to use both lists...!) Recall that the dot product of two vectors or lists is the sum of the products of the elements in the same position in the two vectors. for example, the first result is 5*6 plus 3*4, which is 42.0 (if we use a float).

>>> dot( [5,3], [6,4] )

42.0

 

>>> dot( [1,2,3,4], [10,100,1000,10000] )

43210.0

 

>>> dot( [5,3], [6] )

0.0

 

  1. Write ind(e, L), which takes in a sequence L and an element e. L might be a string or, more generally, a list. Your function ind should return the index at which e is first found in L. Counting begins at 0, as is usual with lists. If e is NOT an element of L, then ind(e, L) should return any integer larger than or equal to len(L). It can be len(L) exactly, as shown below in these examples:

>>> ind(42, [ 55, 77, 42, 12, 42, 100 ])

2

 

>>> ind(42, range(0,100))

42

 

>>> ind('hi', [ 'hello', 42, True ])

3

 

>>> ind('hi', [ 'well', 'hi', 'there' ])

1

 

>>> ind('i', 'team')

4

 

>>> ind(' ', 'outer exploration')

5

 

  1. letterScore( let ) should take as input a single-character string and produce as output the value of that character as a scrabble tile. If the input is not one of the letters from 'a' to 'z', the function should return 0.

    To write this function you will need to use this mapping of letters to scores


What!? Do I have to write 25 or 26 if elif or else statements? No! Instead, use the
in keyword:

 

>>> 'a' in 'this is a string including a'

True

 

>>> 'q' in 'this string does not have the the letter before r'

False

 

Phew!

This problem does not require recursion. But it's used in the next one... .

  1. scrabbleScore( S ) should take as input a string S, which will have only lowercase letters, and should return as output the scrabble score of that string. Ignore the fact that, in reality, the availability of each letter tile is limited. Hint: use the above letterScore function and recursion. (Compare this with the the mylen example we did in class.)

    Here are some examples:

>>> scrabbleScore('quetzal')

25

 

>>> scrabbleScore('jonquil')

23

 

>>> scrabbleScore('syzygy')

25

 

 

Submitting your file

You should submit your hw1pr4.py file at Canvas.