首页 > 解决方案 > Python Problem: applied problem for building function for retrieving the elements from the list

问题描述

Problem: There is a marathon, and you have a list of the participants. (ex: ["jack", "mike", "john"]) The number of participants is random integer between 1 to 100000. You also have list of participants who completed the marathon. The number of the participants who completed is 1 less than the total participants. There could be participants who have same name. You have to build a function that would print the participant who didn't complete the marathon.

My attempt:

def solution(participant, completion):
    for i in completion:
        if i in participant:
            participant.remove(i)
    answer = print ('"' + str(*participant) + '"')
    return(answer)

For example, if participant = ["john", "mike"], completion = ["john"], The expected answer is "mike" I think there is nothing wrong with the code I attempted. If there is, could you guys point out the part I missed? Most importantly, is there a more elegant way of building this function?

标签: pythonfunction

解决方案


    for i in completion:
        if i in participant:
            participant.pop(i)
    answer = print ('"' + str(*participant) + '"')
    return(answer)

推荐阅读