본문 바로가기

스크립트

[코딩테스트 입문] 모음 제거

반응형

모음 제거

1안) solution.py

def solution(my_string):
    answer = ''
    
    vowels = ["a", "e", "i", "o", "u" ]
    for vowel in vowels:
        my_string = my_string.replace(vowel, '')
    answer = my_string
    
    return answer

2안) solution.py

import re

def solution(my_string):
    answer = ''
    
    answer = re.sub(r"a|e|i|o|u", "", my_string)
    
    return answer

3안) solution.py

def solution(my_string):
    answer = ''
    
    vowels = ['a','e','i','o','u']
    for word in my_string:
        if word not in vowels:
            answer += word

    return answer

출처 - 프로그래머스(코딩테스트 연습) : https://school.programmers.co.kr/learn/courses/30/lessons/120849


python RegEx(Regular Expression) sub() Function

모든 공백 문자를 숫자 "9"로 바꿉니다.

import re

#Replace all white-space characters with the digit "9":

txt = "The rain in Spain"
x = re.sub("\s", "9", txt)
print(x)
The9rain9in9Spain

공백 문자의 처음 두 항목을 숫자 9로 바꿉니다.

import re

#Replace the first two occurrences of a white-space character with the digit 9:

txt = "The rain in Spain"
x = re.sub("\s", "9", txt, 2)
print(x)
The9rain9in Spain

 

참고URL

- w3schools : https://www.w3schools.com/python/python_regex.asp

- w3schools : https://www.w3schools.com/python/gloss_python_regex_functions.asp#sub

 

728x90
반응형