ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 문제 04. 원소 기호
    NLP/NLP100 2021. 1. 4. 18:09

    Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can 라는 문장을 단어 단위로 분할하고, 1,5,6,7,8,9,15,16,19 번째 단어는 앞글자, 그 밖의 단어는 앞에서 두 글자씩 추출하여 꺼낸 문자열에서 단어의 위치순으로 나열한 배열을 만드시오.

    def sol(i, word):
        if i in [1, 5, 6, 7, 8, 9, 15, 16, 19]:
            return (word[0], i)
        else:
            return (word[:2], i)
    
    
    text = 'Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can.'
    re_text = text.replace('.', '').replace(',', '')
    answer = [sol(i, w) for i, w in enumerate(re_text.split(), 1)]
    print(dict(answer))

     

    함수를 조금 더 줄여서 한 줄로 만들어 보았다. 

    text = 'Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can.'
    
    text = text.split(" ")
    
    num = [1,5,6,7,8,9,15,16,19]
    
    def sol(): return {i+1: (w[0] if i+1 in num else w[:2]) for (i,w) in enumerate(text)}
    
    print(sol())

     

    [출력 결과]

    {1: 'H', 2: 'He', 3: 'Li', 4: 'Be', 5: 'B', 6: 'C', 7: 'N', 8: 'O', 9: 'F', 10: 'Ne', 11: 'Na', 12: 'Mi', 13: 'Al', 14: 'Si', 15: 'P', 16: 'S', 17: 'Cl', 18: 'Ar', 19: 'K', 20: 'Ca'}
    

    'NLP > NLP100' 카테고리의 다른 글

    문제 06. 집합  (0) 2021.01.04
    문제 05. n-gram  (0) 2021.01.04
    문제 03. 원주율  (0) 2021.01.04
    문제 02. "shoe" + "cold" = "schooled"  (0) 2021.01.04
    문제 01. "schooled"  (0) 2021.01.04
Designed by Tistory.