Don't understand questions 4 and 5 in Advanced Python Code Challenges: Lists

I’m having trouble with questions 4 and 5 in the Advanced Python Code Challenges: Lists. Mainly because I’m not good at word problem questions :sweat_smile:
Here’s the link: https://www.codecademy.com/courses/learn-python-3/articles/advanced-python-code-challenges-lists

If you understand, please explain the questions to me. Thank u

Could you post. screenshot of them, please? (I don’t have PRO so I can’t see the questions).

Question 4 is asking you to create a function with two parameters.
The first parameter is a list, the second a number(integer)
The function should return an almost identical copy of the list pass as a parameter, except for the element at the index that was passed as the second parameter. The index value should be double the value so index * 2.

So:

  • Create a function with two parameters
  • Multiply the value of the 2nd parameter by 2, and then put back that new value into his original place in the list
  • Then return the new modified list(the list given as a parameter WITH the index value given in the 2nd parameter * 2)
  • the function also needs to check if the index that was passed is bigger than the size of the list or if it is an invalid index( index < 0).

I hope this make sense…

QUESTION 4

QUESTION 5

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).
2 Likes