알파벳 소문자로만 이루어진 단어 S가 주어진다. 각각의 알파벳에 대해서, 단어에 포함되어 있는 경우에는 처음 등장하는 위치를, 포함되어 있지 않은 경우에는 -1을 출력하는 프로그램을 작성하시오.
code - 1차
import string
import sys
lowerList = list(string.ascii_lowercase)
input = sys.stdin.readline
word = input()
answer = []
for l in lowerList:
if l not in word:
answer.append("-1")
else:
answer.append(str(word.index(l)))
print(' '.join(answer))
code - 2차
import sys
import string
input = sys.stdin.readline
lowerList = string.ascii_lowercase
s = input().strip()
result = []
for chr in lowerList:
result.append(str(s.find(chr)))
print(' '.join(result))
- find 함수 특성(없는 값에 대해선 -1을 반환함)을 이용하여 코드 리팩토링하였습니다.
https://github.com/seonmin5/codingtest_Python
GitHub - seonmin5/codingtest_Python
Contribute to seonmin5/codingtest_Python development by creating an account on GitHub.
github.com
반응형
'Python' 카테고리의 다른 글
[Python/Bronze V] 팩토리얼 3 - 27434, math.factorial (0) | 2024.08.31 |
---|---|
[Python/level 0] 세로 읽기 - 181904, 리스트 슬라이싱 (0) | 2024.08.31 |
[Python/level 0] 날짜 비교하기 - 181838, int(date1 > date2) (0) | 2024.08.31 |
[Python/level 0] 빈 배열에 추가, 삭제하기 - 181860, enumerate, extend, del (1) | 2024.08.31 |
[Python/level 1] 두 정수 사이의 합 - 12912, sum + 리스트 컴프리헨션 vs. for 반복문 (0) | 2024.08.31 |