job 인터뷰/코테(Matroid) 준비

Matroid: Write a program to make every third word uppercase

hyunkookim 2025. 4. 15. 10:17

Write a program to make every third word uppercase

 

"세 번째 단어마다 대문자로 변환" 문제는 실제 Matroid 인터뷰 후기에서 지원자가 직접 언급한 프로그래밍 질문 중 하나입니다:

“Write a program to make every third word uppercase.”
— Matroid 지원자 인터뷰 후기 중


🧩 문제 요약

주어진 문자열에서 세 번째마다 등장하는 단어대문자로 바꾸는 함수를 작성하라.
(단어는 공백으로 구분되며, 인덱스는 1부터가 아닌 0부터 시작할지 1부터 시작할지는 명시되지 않았지만, 일반적으로 1-based로 처리)


🧪 예시

python
복사편집
Input: "hello world this is a test message for matroid" Output: "hello world THIS is a TEST message FOR matroid"

위 예시에서 "this", "test", "for" 가 3, 6, 9번째 단어로 대문자 처리된 모습이에요.

 

🧩 Problem: Capitalize Every Third Word

Description
Given a string s consisting of words separated by single spaces, return a new string where every third word is converted to uppercase.

Assume the first word is the first (i.e., indexing is 1-based). A word is defined as any sequence of non-space characters.

🧩 문제: 세 번째 단어마다 대문자로 변환

설명
하나의 문자열 s가 주어집니다. 이 문자열은 단어들이 공백 하나로 구분되어 있는 형태입니다.
이 문자열에서 세 번째마다 등장하는 단어대문자로 바꾼 새 문자열을 반환하세요.

단어의 번호는 1부터 시작한다고 가정합니다.
단어란, 공백이 아닌 연속된 문자들의 집합을 의미합니다.

 

Example 1:

Input: s = "this is test message for the matroid challenge"
Output: "this is A test message FOR the matroid CHALLENGE"

 

Example 2:

Input: s = "a b c d e f"

Output: "a b C d e F"

 

Constraints:

  • 1 <= s.length <= 10^4
  • s contains only lowercase and uppercase English letters and spaces.
    • s는 오직 영어 소문자, 대문자, 공백만 포함합니다.
  • Words are separated by a single space with no leading or trailing spaces.
    • 단어들은 공백 하나로만 구분되며, 문자열 앞뒤에 공백은 없습니다.
def changeThirdWord(s: str):
    # 입력된 문자열 s를 공백(" ") 기준으로 나누어 단어 리스트로 변환
    s_list = s.split(" ")

    # 결과 문자열을 저장할 리스트 초기화
    new_str = []

    # 단어 리스트를 인덱스와 함께 순회
    for i, word in enumerate(s_list):    	
        # 인덱스 i가 2, 5, 8, ...일 경우 (즉, 3번째마다 해당하는 단어)
        if i % 3 == 2:
            # 해당 단어를 대문자로 변환하여 결과 리스트에 추가
            new_str.append(word.upper())
        else:
            # 나머지 단어는 그대로 추가
            new_str.append(word)

    # 결과 리스트를 공백으로 join 하여 다시 하나의 문자열로 반환
    return " ".join(new_str)