Programming with Python¶

Test 2¶

Note: Don't forget to write the programs that allow the examiners to test the functions that you write.

Problem 1. Write a function is_sorted_sum(L) that returns True if the list L of integers is sorted ascendingly by the sum of its elements' digits.

For example, the list $13, 11111, -17, 9, 1719$ is sorted as requested because the respective sums of digits are as follows:

Numbers 13 11111 -17 9 1719
Sums of the digits 4 5 8 9 18

Your function should not use the in-build sort or sorted commands: checking whether or not a list is sorted does not require any sorting.

Problem 2. Write a function even_slice(L, a, b) that returns a new list containing all the even numbers among the elments of L between indices a and b (including both of them) in their original order. You may assume that 0 <= a <= b < len(L).

For example,

even_slice([11, 12, 2, 17, 20, 22, 2, 19, 7, 42, 18, 20], 2, 10)


should return the list of all the even numbers in the list [2, 17, 20, 22, 2, 19, 7, 42, 18]. In other words, it should return

[2, 20, 22, 2, 42, 18]