[프로그래머스 Lv1] 서울에서 김서방 찾기 / 파이썬 Python

Gayoung Lee·2022년 5월 17일
0

Algorithm

목록 보기
16/39

문제

String형 배열 seoul의 element중 "Kim"의 위치 x를 찾아, "김서방은 x에 있다"는 String을 반환하는 함수, solution을 완성하세요. seoul에 "Kim"은 오직 한 번만 나타나며 잘못된 값이 입력되는 경우는 없습니다.

내 풀이

def solution(seoul):
    res=0
    for idx, x in enumerate(seoul):
        if x=="Kim":
            res=idx
    answer="김서방은 "+str(res)+"에 있다"
    return answer

다른 사람의 풀이

def findKim(seoul):
    return "김서방은 {}에 있다".format(seoul.index('Kim'))

문법 정리

  • 파이썬 list에서 값에 대한 index 찾을땐 index() 함수 쓰자
profile
삽질하며 성장하는 gayoungee

0개의 댓글