This assignment is due on Wednesday, September 12 before midnight.
Please see the tutoring
schedule for the schedule of tutoring hours. You may also send e-mail to
cs60help@cs.hmc.edu for
help.
Unless otherwise specified you can use any rex built-in functions, arithmetic
operators, or comparison operators mentioned in Chapter 3 (e.g., sort,
reverse, and append), including higher-order functions
such as map and reduce. Even if you've read ahead
to Chapter 4, your functions must not use recursion or pattern-matching.
This assignment covers material in Chapter 3 of the course text.
assign2.rex. To receive credit
for your answer, the functions must have exactly the names specified. You
may define other functions to be used by your code, if this seems useful. Your
file must be loadable into into the rex system without generating any errors,
so make sure you have comments around all the non-rex parts of your answers. As
before you should run cs60submit assign2.rex add42 and add42list
Construct a function add42 that returns the number 42 greater
than its argument. Then construct a function add42list which takes
a list of numbers as input and returns a list in which each element of the
original list has been incremented by 42. For example:
add42list([1, -7, -42, 8]) ==> [43,35,0,50]
sum and average
Construct a function sum that, given a list of numbers, returns their sum.
sum([1, -7, -42, 8]) ==> -40
average that returns the average (arithmetic mean)
of the numbers in its list argument.average([1, -7, -42, 8]) ==> -10.0
float, that transforms an integer argument
into an equivalent floating-point value. In rex arithmetic, if either argument
to an arithmetic operation is floating-point the result will be floating-point;
otherwise the result will be an integer. For example,
22 / 3 ==> 7
22 / float(3) ==> 7.33333
superreverse
Construct a function superreverse, that takes a list of lists as
outputs and returns a new list of lists containing the reversals of the
input lists, in reverse order. For example:
superreverse([[1,2,3],[4,5,6],[7,8,9]]) ==> [[9,8,7],[6,5,4],[3,2,1]]superreverse([[["e","x"],"r"], ["e","v"],
["o","l",["I"]]])
==> [[["I"], "l", "o"], ["v","e"],
["r",["e","x"]]]inner_productinner_product
that computes the inner product of two vectors; the inner product is the sum
of the element-wise products. For example, the inner product of [2,5,7] with
[3,2,8] is 2*3 + 5*2 + 7*8 = 72. You may assume that the two list inputs have
the same length (i.e., that the vectors represented by the lists have the
same dimension).gathergather that takes a key and an association
list and returns a list of all elements paired with this key in the
the association list. For example,gather(3, [[1,'a'], [2,'b'], [3,'c'], [1,'d'], [3,'e'],[3,'f'],[2,'g']])
==> ['c','e','f']
[c, e, f].) compose_alistscompose_alists that returns
the association list corresponding to the composite of two input functions
represented as association lists. For example: compose_alists([[0,3],[1,2],[2,1],[3,0]] , [[0,1],[1,2],[2,3],[3,2]])
==> [[0,2],[1,1],[2,0],[3,1]]
is_palindrome and all_palindromes and all_palindromesexplode and/or implode, which convert a string to
a list of characters and a list of characters to a string, respectively. Recall
that characters are written with single quotes in rex, so the space character
would be ' '.is_palindrome("able was i ere i saw elba") ==> 1
is_palindrome("a man a plan a canal panama") ==> 1
is_palindrome("almost a palindromeordnilap a tsomla") ==>
0
all_palindromes, which given a list of
strings returns a list containing all the palindromes in the argument list.
all_palindromes such
that given an integer N and a list of strings L,
all_palindromes(N,L) N
non-space letters.equal_as_setsequal_as_sets([],[1]) ==> 0
equal_as_sets([1],[]) ==> 0
equal_as_sets([1],[1,1,1]) ==> 1
equal_as_sets([1,2,3],[3,2,1]) ==> 1
equal_as_sets([1,2,3],[1,2,2,3]) ==> 1
equal_as_sets([1,2,3],[1,2,4,3]) ==> 0