데이터와 코드로 세상을 바라봅니다.

[Python 3] - List, sorted(), pop(), index() 본문

Code/Python

[Python 3] - List, sorted(), pop(), index()

코드우드 2020. 11. 6. 13:50

[풀이 코드]

def solution(participant, completion):
    answer = ''
    
    participant=sorted(participant)
    completion=sorted(completion)
    answer = participant[len(completion)]

    for i in range(0,len(completion)):
        if participant[i]!=completion[i]:
            answer=participant[i]
            break

    return answer
def solution(participant, completion):
    answer = ''
    
    for i in range(0, len(completion)) :
        index_pos = participant.index(completion[i])
        participant.pop(index_pos)
        
    answer = participant.pop()
    
    return answer

[문제] - 완주하지 못한 선수

 

[문제 설명]

수많은 마라톤 선수들이 마라톤에 참여하였습니다. 단 한 명의 선수를 제외하고는 모든 선수가 마라톤을 완주하였습니다.

마라톤에 참여한 선수들의 이름이 담긴 배열 participant와 완주한 선수들의 이름이 담긴 배열 completion이 주어질 때, 완주하지 못한 선수의 이름을 return 하도록 solution 함수를 작성해주세요.

제한사항

  • 마라톤 경기에 참여한 선수의 수는 1명 이상 100,000명 이하입니다.
  • completion의 길이는 participant의 길이보다 1 작습니다.
  • 참가자의 이름은 1개 이상 20개 이하의 알파벳 소문자로 이루어져 있습니다.
  • 참가자 중에는 동명이인이 있을 수 있습니다.

입출력 예

participantcompletionreturn

[leo, kiki, eden] [eden, kiki] leo
[marina, josipa, nikola, vinko, filipa] [josipa, filipa, marina, nikola] vinko
[mislav, stanko, mislav, ana] [stanko, ana, mislav] mislav

[참고 자료]

stackoverflow.com/questions/4843158/check-if-a-python-list-item-contains-a-string-inside-another-string

 

Check if a Python list item contains a string inside another string

I have a list: my_list = ['abc-123', 'def-456', 'ghi-789', 'abc-456'] and want to search for items that contain the string 'abc'. How can I do that? if 'abc' in my_list: would check if 'abc' ex...

stackoverflow.com

www.geeksforgeeks.org/python-list-pop/

 

Python list | pop() - GeeksforGeeks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

www.geeksforgeeks.org

stackoverflow.com/questions/14849293/python-find-index-position-in-list-based-of-partial-string

 

python - find index position in list based of partial string

mylist = ["aa123", "bb2322", "aa354", "cc332", "ab334", "333aa"] I need the index position of all items that contain 'aa'. I'm having trouble combining enumerate() with partial string matching. I'...

stackoverflow.com

thispointer.com/python-how-to-find-all-indexes-of-an-item-in-a-list/

 

Python: Find index of element in List (First, last or all occurrences) – thispointer.com

In this article we will discuss different ways to get index of element in list in python. We will see how to find first index of item in list, then how to find last index of item in list, or then how to get indices of all occurrences of an item in the list

thispointer.com