AI/Python
[python] 접두사 startswith
brave_sol
2024. 12. 20. 12:36
프로그래머스 알고리즘 kit 해시
1. 해시- 완주하지 못한 선수
def solution(participant, completion):
answer = {}
for i in participant:
answer[i] = answer.get(i,0) + 1
for i in completion:
answer[i] = answer.get(i) - 1
for key,value in answer.items():
if value==1:
return key
2. 접두사 startswith
text = "Hello, world!"
# "Hello"로 시작하는지 확인
print(text.startswith("Hello")) # 출력: True
# "world"로 시작하는지 확인
print(text.startswith("world")) # 출력: False
text = "example.py"
# 여러 접두사 중 하나로 시작하는지 확인
print(text.startswith(("ex", "sample", "test"))) # 출력: True
print(text.startswith(("test", "py"))) # 출력: False
text = "Hello, world!"
# 인덱스 7부터 시작
print(text.startswith("world", 7)) # 출력: True
print(text.startswith("Hello", 7)) # 출력: False
text = "Hello, world!"
# 인덱스 7부터 12까지 확인
print(text.startswith("wor", 7, 12)) # 출력: True
print(text.startswith("world", 7, 12)) # 출력: False
text = "Hello, World!"
# 대소문자 구분
print(text.startswith("hello")) # 출력: False
print(text.startswith("Hello")) # 출력: True
3. 전화번호 목록
def solution(phone_book):
answer = True
phone_book.sort()
for i in range(len(phone_book) - 1) :
if phone_book[i+1].startswith(phone_book[i]) : return False
return answer
반응형