서울에서 김서방 찾기

문제 설명

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

 

제한 사항

  • seoul은 길이 1 이상, 1000 이하인 배열입니다.
  • seoul의 원소는 길이 1 이상, 20 이하인 문자열입니다.
  • Kim은 반드시 seoul 안에 포함되어 있습니다.

 

입출력 예

seoul return
[Jane, Kim] 김서방은 1에 있다

 

Python

Custom String Formatting

 

string — Common string operations — Python 3.9.1 documentation

string — Common string operations Source code: Lib/string.py String constants The constants defined in this module are: string.ascii_letters The concatenation of the ascii_lowercase and ascii_uppercase constants described below. This value is not locale-

docs.python.org

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

JavaScript

function solution(seoul) {
    let i = 0;
    while(true){
        if(seoul[i] === "Kim")
            break;
        i++;
    }
    return "김서방은 " + i + "에 있다";
}
function solution(seoul) {
  var idx = seoul.indexOf('Kim');
  return "김서방은 " + idx + "에 있다";
}

C++

#include <string>
#include <vector>
using namespace std;

string solution(vector<string> seoul) {
    int i = 0;
    for(string temp : seoul){
        if(temp == "Kim")
            break;
        i++;
    }
    string answer1 = "김서방은 ";
    string answer2 = "에 있다";
    string answer = answer1 + to_string(i) + answer2;
    return answer;
}
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>

using namespace std;

string solution(vector<string> seoul) {
    string answer = "";
    int pos=find(seoul.begin(),seoul.end(),"Kim")-seoul.begin();
    answer="김서방은 "+to_string(pos)+"에 있다";
    return answer;
}
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기