def solution(babbling):
count = 0
for string in babbling:
for word in ["aya", "ye", "woo", "ma"]:
if string.count(word) < 2:
string = string.replace(word, ' ')
if len(string.strip()) == 0:
count += 1
return count
babbling = ["aya", "yee", "u", "maa", "wyeoo"]
>> 1
초기 변수 count를 0으로 설정한다. 이 변수는 조건을 충족하는 문자열의 개수를 추적한다.
babbling 리스트를 반복하면서 각 문자열을 string 변수에 할당한다.
string을 반복하면서 다음 단어들을 찾아내어 처리한다: "aya", "ye", "woo", "ma".
처리된 string이 모두 공백으로 이루어져 있다면, 즉, string을 .strip()한 결과가 비어있다면, count를 증가시킨다.
마지막으로, count를 반환한다. 이는 조건을 충족하는 문자열의 개수이다.
replace()
replace() 함수는 문자열 내에서 특정 패턴을 찾아 지정된 값으로 대체하는 메서드이다. 문자열 내에 해당 패턴이 여러 번 등장할 경우, 모든 등장을 대체한다.
replace() 함수의 구문은 다음과 같다:
new_string = original_string.replace(old_pattern, new_pattern)
다음은 replace() 함수의 사용 예시이다:
string = "Hello, world!"
new_string = string.replace("o", "*")
print(new_string)
출력:
Hell*, w*rld!
strip()
strip() 메서드는 문자열의 앞과 뒤에서 공백(whitespace) 문자를 제거하는 함수이다.
strip() 메서드의 구문은 다음과 같다:
new_string = original_string.strip()
다음은 strip() 메서드의 사용 예시이다:
string = " Hello, world! "
new_string = string.strip()
print(new_string)
출력:
Hello, world!
strip(chars): 문자열의 양쪽 끝에서 chars 매개변수에 지정된 문자들을 제거한다. 만약 chars를 지정하지 않으면 공백 문자를 제거한다.lstrip(chars): 문자열의 왼쪽(시작 부분)에서 chars 매개변수에 지정된 문자들을 제거한다.rstrip(chars): 문자열의 오른쪽(끝 부분)에서 chars 매개변수에 지정된 문자들을 제거한다.