Due: Thursday, April 26

The homework is to be done individually. You are free to do it manually and/or to use the computer. Hand in your answers (both the code and the result of running it) at the beginning of the lab on Thursday. For test data, use the examples included with each problem and add others if you want.

- (15 points) [easy, just type this in and see the results!]

Try these in Python and show the results of each expression:a = ['ako','of','fun'] a b = a * 2 # check how * is overloaded b c = [a]*3 c a[-1] a[:-1] a[::-1] a[::2] z=a[2:] z[0] = 'pain' a z z= a + z z a a+=z[::-1] z[2]='weird' z z[0:0]=['this','is'] z z[2:4]=['uncommon'] z z[0:1]='that'

- (5 points) [easy, just type this in and see the results!]

Lists comprehensions are used often in Python instead of filter and map. Givenstr = "this was a nice try"

evaluate the following:- [x*2 for x in range(4)]
- [x*2 for x in str]
- [str.count(x) for x in str]
- [(x,str.count(x)) for x in str]
- [str[i]*2 for i in range(len(str))]

- (10 points) [this requires some thinking but it is easy]

Givend=[[1,'a'],[3,'h'],[5,'o']]

write the python expressions you need to obtain from the variable d the following:- the number 5
- the list [1,'a']
- the string 'h'
- the sum of the first elements of each sublist (i.e. 1+3+5)
- the second element of the last element in the list (i.e. 'o')

- (10 points) [this requires some thinking but it is easy]

You are given the following list:d=[[1,'a'],[3,'h'],[5,'o']]

Use the python methods for lists to write the python expressions to:- insert the list [0, 'x'] as the first element of d
- extend the list d by adding to its end the elements [7,'i'] and [3,'e']
- attach the element [2,'m'] to the end of the list d
- remove the element [1,'a'] from d
- remove the third element of d (i.e. the element in position 2)

- (10 points) [similar to what we did in Scheme]

Write in Python a procedure to add the integers that are multiple of 3 in a given range (lower extreme included, upper extreme excluded). It should work like this:>>> add_multipleof3(1,9) 9 >>> add_multipleof3(7,9) 0

- (10 points) [similar to what we did in Scheme]

Write a function add-lists(l1, l2) that takes two lists of the same length and adds the elements of the two lists together (same positions are added), creating a new list of the same length. For example:>>> add-lists([2, 3, 4, 5],[7, 8, 9, 10]) [9, 11, 13, 15]

- (10 points) [similar to what we did in Scheme]

Write a procedure`(replace_even lst)`

that adds 1 to every element of a list that is an even number. It should work like this:>>>replace_even([3,6,1,20,1]) [3,7,1,21,1] >>>replace_even([6,20,6]) [7,21,7]

- (15 points)

Write in Python a function thats adds each element of a list with the next element and returns a list of the results. The returned list should contain one less element than the original list. It should work like this:>>> add_pairs([1,2,3,4]) [3,5,7]

- (15 points) [similar to what we did in Scheme]

Write a function`running_sum`

that given a list produces a new list that contains, as each element in the new list, the sum of all corresponding elements up to that point in the original list. It should work like this:>>> running_sum([1, 3, 9, 2]) [1, 4, 13, 15] #where 4 = 1+3, 13 = 1+3+9, 15=1+3+9+2 >>> running_sum([2, 2, 2, 2, 2]) [2, 4, 6, 8, 10]

Department of Computer Science and Engineering. All rights reserved.

Changes and corrections are in red.