Here’s a brief rewording/summary of each of the two challenges.
Question 4
- Make a function named
double_index
.
- The function should take in 2 parameters:
lst
(which is a list) and index
(which is an integer).
- The function should return a new list.
- This new list should be the same as
lst
(the list that was passed into the function) except for one thing.
- The one difference should be that the element at the
index
index of the list should be doubled. For example, let’s say double_index([2, 4, 6, 8], 1)
was called. [2, 8, 6, 8]
should be returned.
[2, 4, 6, 8] <-- list
| | | |
v v v v
0 1 2 3 <-- indices
Since 4
is the element at index 1
, we should double it, making it 8
. The new list with the doubled value should be returned.
Question 5
- Make a function named
middle_element
.
- The function should take in one parameter:
lst
(a list).
- The function should return one of two things based on the number of elements in
lst
.
- If there are an odd number of elements in
lst
, the function should return the element in the middle of the list.
- If there are an even number of elements in
lst
, the function should return the average of the middle two elements in the list.
Odd number of elements example:
[2, 4, 6] <-- there are an odd number of elements (3)
|
v
middle
The function should return 4 since it is the middle element.
Even number of elements example:
[2, 4, 6, 8] <-- there are an even number of elements (4)
| |
v v
middle 2
The function should return 5 since it is the average of the
middle 2 elements (which are 4 and 6).