관리 메뉴

A seeker after truth

exercism pangram solution 본문

Algorithm/문제풀이

exercism pangram solution

dr.meteor 2020. 1. 24. 19:00

P)

Determine if a sentence is a pangram. A pangram (Greek: παν γράμμα, pan gramma,

"every letter") is a sentence using every letter of the alphabet at least once.

The best known English pangram is:

> The quick brown fox jumps over the lazy dog.

 

The alphabet used consists of ASCII letters `a` to `z`, inclusive, and is case

insensitive. Input will not contain non-ASCII symbols.

 

 

mysol)

def is_pangram(sentence):
    alpha_dic = {'a':0,'b':0,'c':0,'d':0,'e':0,'f':0,'g':0,'h':0,'i':0,'j':0,'k':0,'l':0,'m':0,'n':0,'o':0,'p':0,'q':0,'r':0,'s':0,'t':0,'u':0,'v':0,'w':0,'x':0,'y':0,'z':0}
    for i in sentence:
        if  65 <= ord(i) <= 90: # upper char
            alpha_dic[chr(ord(i)+32)] += 1
        elif 97 <= ord(i) <= 122: # lower char
            alpha_dic[i] += 1
        else:
            continue 

    for value in alpha_dic.values():
        if value >= 1:
            continue
        else:
            return False
    return True

ord가 아스키 코드 상의 번호를 반환하는 기능이라는 것을 알면 좋다.

 

sol2)

from string import ascii_lowercase

ALPHABET = set(ascii_lowercase)

def is_pangram(string):
    return ALPHABET.issubset(string.lower())

이런 라이브러리가 있다는 것 알면 참 편하게 풀 수 있다. 그리고 issubset 함수를 활용하는 대표적인 사례

 

sol3)

from string import ascii_lowercase

def is_pangram(sentence):
    return all(letter in sentence.lower() for letter in ascii_lowercase)

all이라는 기능...

 

sol4)

from string import ascii_lowercase


def is_pangram(sentence):
    sentence_set = set(sentence.lower())
    alpha_set = set(ascii_lowercase)

    if alpha_set - sentence_set == set([]):
        return True

    return False

2번째 풀이의 issubset 함수 기능을 풀어쓴 것이라 생각하면 된다.

 

sol5)

def is_pangram(string):
    return set('abcdefghijklmnopqrstuvwxyz') <= set(string.lower())

길이가 짧은걸로는 압도적,..

'Algorithm > 문제풀이' 카테고리의 다른 글

exercism raindrops solution  (0) 2020.01.24
exercism RNA-transcription solution  (0) 2020.01.24
exercism leap solution  (0) 2020.01.24
exercism reverse-string solution  (0) 2020.01.24
프로그래머스 정렬 문제풀이(1)  (0) 2020.01.24