관리 메뉴

솜씨좋은장씨

[BaekJoon] 26587번 : Reverse (Python) 본문

Programming/코딩 1일 1문제

[BaekJoon] 26587번 : Reverse (Python)

솜씨좋은장씨 2023. 1. 10. 13:15
728x90
반응형

코딩 1일 1문제! 오늘의 문제는 백준의 Reverse 입니다.

 

26587번: Reverse

At a contest you have been asked to write a program that reads in a line of text and reverses the order of all words that begin with vowels. Words that begin with consonants will keep their position in the line of text. You are not sure why you would ever

www.acmicpc.net

👨🏻‍💻 코드 ( Solution )

def reverse(string):
    vowels = ['a', 'e', 'i', 'o', 'u']
    
    reverse_result = []
    
    reverse_dict = {}
    
    words = string.split()
    
    check_vowels = [word[0] for word in enumerate(words) if word[1][0].lower() in vowels]
    
    reverse_check = check_vowels[::-1]

    
    for idx, check_idx in enumerate(check_vowels):
        reverse_dict[check_idx] = words[reverse_check[idx]]
        
    for word_idx, word in enumerate(words):
        if word_idx in reverse_dict.keys():
            reverse_result.append(reverse_dict[word_idx])
        else:
            reverse_result.append(word)
            
    return " ".join(reverse_result)
    
    
if __name__ == "__main__":
    while True:
        try:
            string = input()
            print(reverse(string=string))
        except EOFError:
            break
 

GitHub - SOMJANG/CODINGTEST_PRACTICE: 1일 1문제 since 2020.02.07

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

Comments