Quick Looping Exercises
Do each exercise twice, first with for and
then with while. When using for
on a list, do not access the elements of the list by position (as in
L[i]). Make sure all
the test cases below actually work, and so leave the test cases
(commented out) in your code.
1. Create the function AddSequence(low,high)
which will add up all the whole numbers between a given low integer
and a given high integer (including the low and high
integers). Examples:
AddSequence(2,3) # returns 5
AddSequence(-1,2) # returns 2
AddSequence(1,5) # returns 15
2. Create the function Select23(low,high)
that returns a list of all of the numbers between low and high (including low and high) that are evenly divisible
by 2 and/or 3. Examples:
Select23(1,6) # returns [2, 3, 4, 6]
Select23(-4,4) # returns (-4, -3, -2, 0, 2, 3, 4]
Select23(15,20) # returns [15, 16, 18, 20]
3. Create the function Esrever(L)
which will be given a list of strings, and will reverse each string
(rearranging the characters backwards) and will return a list each
of them. Use slicing to reverse each string. Example:
Esrever(['abcd','derF']) # returns ['dcba', 'Fred']