Programmers / 이상한 문자 만들기 / Python

2022. 4. 1. 16:14·coding test - python/Programmers
728x90
반응형

 

*문제 출처는 프로그래머스에 있습니다.

문제 제목: 이상한 문자 만들기

문제 사이트: https://programmers.co.kr/learn/courses/30/lessons/12930


나의 풀이(오답)

def solution(s):
	s = list(str(S))
    cnt = 0
    s[0] = s[0].upper()
    
    for i in range(1,len(s)):
        #공백을 만나면 cnt 초기화
        if s[i-1] == ' ' and s[i] != ' ':
            cnt = 0
        else:
             cnt += 1
                
        if cnt % 2 == 0:
            s[i] = s[i].upper()
            
    return "".join(s)

테스트는 성공하나, 채점에서 자꾸만 틀렸다..

cnt를 이용해서 해당 문자가 홀수일 때와 짝수일 때 해당 조건에 맞도록 변환되도록 하였다.

공백을 만나면  cnt 값을 초기화 하였다.

 

모범답안

def solution(s):
    answer = ''
    count = 0
    for i in range(len(s)):
        if s[i] == ' ':
            count = 0
            answer += s[i]
            
        elif count % 2 == 0:
            answer += s[i].upper()
            count += 1
        else:
            answer += s[i].lower()
            count += 1
    return answer
[출처] 이상한 문자 만들기 (Python) [프로그래머스 문제풀이]|작성자 소울치킨

s[i]를 조건문과 반복문을 통하여 answer이라는 빈 문자열에 추가해주는 방법을 사용하셨다..

나의 풀이와 구조는 비슷하나 더욱 깔끔하고, 문제의 조건에 잘 맞는 코드인 거 같다.


 

728x90
반응형

'coding test - python > Programmers' 카테고리의 다른 글

Programmers / 예산 / Python  (0) 2022.04.07
Programmers / 소수 만들기 / Python  (0) 2022.04.05
Programmers / 내적 / Python  (0) 2022.04.01
Programmers / *행렬의 덧셈 / Python  (0) 2022.04.01
Programmers / 음양 더하기 / Python  (0) 2022.04.01
'coding test - python/Programmers' 카테고리의 다른 글
  • Programmers / 예산 / Python
  • Programmers / 소수 만들기 / Python
  • Programmers / 내적 / Python
  • Programmers / *행렬의 덧셈 / Python
sillon
sillon
꾸준해지려고 합니다..
    반응형
  • sillon
    sillon coding
    sillon
  • 전체
    오늘
    어제
    • menu (614)
      • notice (2)
      • python (68)
        • 자료구조 & 알고리즘 (23)
        • 라이브러리 (19)
        • 기초 (8)
        • 자동화 (14)
        • 보안 (1)
      • coding test - python (301)
        • Programmers (166)
        • 백준 (76)
        • Code Tree (22)
        • 기본기 문제 (37)
      • coding test - C++ (5)
        • Programmers (4)
        • 백준 (1)
        • 기본기문제 (0)
      • 공부정리 (5)
        • 신호처리 시스템 (0)
        • Deep learnig & Machine lear.. (41)
        • Data Science (18)
        • Computer Vision (17)
        • NLP (40)
        • Dacon (2)
        • 모두를 위한 딥러닝 (강의 정리) (4)
        • 모두의 딥러닝 (교재 정리) (9)
        • 통계 (2)
      • HCI (23)
        • Haptics (7)
        • Graphics (11)
        • Arduino (4)
      • Project (21)
        • Web Project (1)
        • App Project (1)
        • Paper Project (1)
        • 캡스톤디자인2 (17)
        • etc (1)
      • OS (10)
        • Ubuntu (9)
        • Rasberry pi (1)
      • App & Web (9)
        • Android (7)
        • javascript (2)
      • C++ (5)
        • 기초 (5)
      • Cloud & SERVER (8)
        • Git (2)
        • Docker (1)
        • DB (4)
      • Paper (7)
        • NLP Paper review (6)
      • 데이터 분석 (0)
        • GIS (0)
      • daily (2)
        • 대학원 준비 (0)
      • 영어공부 (6)
        • job interview (2)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    소수
    programmers
    Python
    백준
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
sillon
Programmers / 이상한 문자 만들기 / Python
상단으로

티스토리툴바