A function to extract multiples of five in a list

Screen Link: https://app.dataquest.io/m/1018/lists/5/multiples-of-5

Dataquest’s Code:

def mult_five(some_list):
    return some_list[0::5]

What I expected to happen:
if add in this line of code:
print(mult_five([0, 1, 5, 5, 10, 11, 16]))

I expect an output of:
[0, 5, 5, 10]

What actually happened:
[0, 11]

The task is to create a function, mult_five , that takes a list as input and returns a list consisting of the multiples of 5 in the input, in the same order. Here’s the code I came up with:

def mult_five(a_list):
    mf = []
    for item in a_list:
        if item % 5 == 0:
            mf.append(item)
    
    return mf

print(mult_five([0, 1, 5, 5, 10, 11, 16]))

Output:
[0, 5, 5, 10]

Is my understanding correct? Thanks in advance.

4 Likes

Hello @lorelynr

Welcome to DQ Community!

As per the Case 1 - The task is to create a function, mult_five , that takes a list as input and returns a list consisting of the multiples of 5 in the input, in the same order. When we return some_list[0::5] and print the function mult_five([0,1,5,5,10,11,16])) The output will indeed print the value [0,11] only because Python sequence slice addresses can be written as a [start:end:step] and any of start, stop or end can be dropped. It means ‘0’ for the first argument, nothing for the second, and jump by five’.[0::5]` is every fifth element of the sequence sliced.(In our case after 0 the next fifth element is 11 ).

Case 2- The function which you’ve written is different from the one which you have mentioned above. Here we are finding the multiples of five in the given list by going through each element.

Hope this helps.

Best
K!

2 Likes

Hello @lorelynr,

Your understanding is correct. You solved the problem correctly.

DataQuest’s code will fail when the numbers do not occur sequentially because it returns the number indexed as multiple of 5. If it happens that the number appears randomly, it will not pass the test. While your code will always pass the test.

Otherwise, we do not understand the question correctly. If it does ask us to return the index position as a multiple of 5. Then DataQuest code solves it.

3 Likes

Thanks for the reply, @monorienaghogho.

Thank you for your reply, @prasadkalyan05. I am fully aware of the mechanism of Dataquest’s code. It’s just that the instruction given is somewhat ambiguous – skip counting by five vs. multiples of five.

Hello @lorelynr.

As said, your understanding is correct. The instructions do not match the outcome of Dataquest’s solution. I’m tagging @Sahil so he can have this logged.

Thank you for reporting the problem.

2 Likes

Hi @lorelynr,

Thank you for letting us know about this bug. The solution is mixing up screens 4 and 5.

Screen 4 Instruction:

  • Create a function, skip_one , that takes a list as input and returns the same list skipping every other element and including the first.

Screen 4 Solution:

def skip_one(some_list):
    return some_list[::2] 

Screen 5 Instruction:

  • Create a function, mult_five , that takes a list as input and returns a list consisting of the multiples of 5 in the input, in the same order.

Screen 5 Solution:

def mult_five(some_list):
    return some_list[0::5]

I will get it logged.

Best,
Sahil

3 Likes