Python 연습 13: 스코틀랜드의 비명
의문
예시
to_scottish_screaming("hello world") ➞ "HELLE WERLD"
to_scottish_screaming("Mr. Fox was very naughty") ➞ "MR. FEX WES VERY NEEGHTY"
to_scottish_screaming("Butterflies are beautiful!") ➞ "BETTERFLEES ERE BEEETEFEL!"
내 시도
실패 시도
>Turn a sentence to Scottish accent
>>replace all the vowel in the string with e and capitalise the whole sentence
>>>find every vowel in the string
set up a vowel list
for every letter equals to one of the vowel list it is a vowel
>>>replace the vowel e
>>>capitalise the string
def to_scottish_screaming(string: str):
vowel_list = ["a", "e", "i", "o", "u"]
string = string.lower()
for letter in string:
if letter in vowel_list:
scottish_scraming = string.replace(letter, "e")
scottish_scraming = scottish_scraming.upper()
return scottish_scraming
시도 2(성공)
>Turn a sentence to Scottish accent
>>replace all the vowel in the string with e and capitalise the whole sentence
>>>find every vowel in the string
initialise a vowel list
check the letter in the string one by one
if the letter is exist in the vowel list, then it is vowel
>>>replace the vowel with e and add to a new string
initialise an empty string
if the letter is a vowel, add "e" to the new string at same position
otherwise, add the original letter to the new string at same position
>>>capitalise the new string
call upper() method on the string
>>>print out the result
def to_scottish_screaming(string: str):
string = string.lower()
vowel_list = ["a", "e", "i", "o", "u"]
scottish_scraming = str()
for letter in string:
scottish_scraming += "e" if letter in vowel_list else letter
scottish_scraming = scottish_scraming.upper()
return scottish_scraming
print(to_scottish_screaming("A, wonderful, day, don't, you, think?"))
기타 솔루션
re.sub() 메서드 사용
[]
는 문자 집합을 나타내는 데 사용되며,import re
def to_scottish_screaming(string: str):
scottish_scraming = re.sub("[AIOU]", "E", string.upper())
return scottish_scraming
내 반성
신용 거래
Reference
이 문제에 관하여(Python 연습 13: 스코틀랜드의 비명), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/mathewchan/python-exercise-13-scottish-screaming-m6l텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)