본문 바로가기

Python_Matter/Solve

Python Learn the basics Quiz 61

반응형

Q>

You are given a text, which contains different english letters and punctuation symbols.

(다른 영어 문자와 구두점 기호가 포함 된 텍스트가 제공됩니다.)

You should find the most frequent letter in the text.

(본문에서 가장 빈번한 편지를 찾아야합니다.)

The letter returned must be in lower case.

(반환 된 서신은 소문자 여야합니다.)
While checking for the most wanted letter, casing does not matter, so for the purpose of your search, "A" == "a".

(가장 원하는 편지를 확인하는 동안 대소 문자는 중요하지 않으므로 검색의 목적 상 "A"== "a"입니다.)

Make sure you do not count punctuation symbols, digits and whitespaces, only letters.

구두점 기호, 숫자 및 공백을 제외한 글자 만 입력해야합니다.)

If you have two or more letters with the same frequency, then return the letter which comes first in the latin alphabet.

(동일한 빈도로 두 개 이상의 글자가있는 경우 라틴 알파벳으로 된 글자를 반환하십시오.)

For example -- "one" contains "o", "n", "e" only once for each, thus we choose "e".

(예를 들어 "one"은 "o", "n", "e"를 각각 한 번만 포함하므로 "e"를 선택합니다.)

 

Input: A text for analysis as a string.

        (문자열로 분석 할 텍스트입니다.)

 

Output: The most frequent letter in lower case as a string.

           (소문자로 된 가장 빈번한 문자.)

 

Example:

checkio("Hello World!") == "l"
checkio("How do you do?") == "o"
checkio("One") == "e"
checkio("Oops!") == "o"
checkio("AAaooo!!!!") == "a"
checkio("abe") == "a"

 

How it is used: For most decryption tasks you need to know the frequency of occurrence for various letters in a section of text.

(대부분의 암호 해독 작업의 경우 텍스트 섹션의 다양한 문자에 대한 발생 빈도를 알아야합니다.)

For example: we can easily crack a simple addition or substitution cipher if we know the frequency in which letters appear.

(예를 들어, 글자가 나타나는 빈도를 알면 간단한 덧셈이나 치환 암호를 쉽게 해독 할 수 있습니다.)

This is interesting stuff for language experts!

(이것은 언어 전문가들에게 흥미로운 것들입니다!)

 

Precondition:
A text contains only ASCII symbols.
0 < len(text) ≤ 105

 

A>

def checkio(text: str) -> str:

    #replace this for solution
    # text를 모두 소문자로
    text_lower = text.lower()

    # 빈 리스트 생성
    array = []

    # text_lower를 리스트로 만들어 cha에 대입
    # cha에서 하나씩 대입하여 found가 False인지 True인지 판단
    # found가 False 이면 array안에 있는 것을 i에 대입 판단
    # 만약에 cha가 i 인덱스 0이랑 같으면 arrary에 있는 i를 제거하고
    # 다시 cha와 i에 1번 인덱스 값 + 1을 추가로 저장
    # 판단후 True면 한 글자 종료
    for cha in list(text_lower):
        if 'a' <= cha <= 'z':
            found = False
            for i in array:
                if cha == i[0]:
                    array.remove(i)
                    array.append((cha, i[1] + 1))
                    found = True
                    break
            if not found:
                array.append((cha, 1))

    # 결과를 정렬
    array = sorted(array, key=lambda x: (-x[1], x[0]))

    return array[0][0]

if __name__ == '__main__':
    print("Example:")
    print(checkio("Hello World!"))

    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert checkio("Hello World!") == "l", "Hello test"
    assert checkio("How do you do?") == "o", "O is most wanted"
    assert checkio("One") == "e", "All letter only once."
    assert checkio("Oops!") == "o", "Don't forget about lower case."
    assert checkio("AAaooo!!!!") == "a", "Only letters."
    assert checkio("abe") == "a", "The First."
    print("Start the long test")
    assert checkio("a" * 9000 + "b" * 1000) == "a", "Long."
    print("The local tests are done.")

 

O>

Example:
l
Start the long test
The local tests are done.

Process finished with exit code 0

 

S>

https://py.checkio.org

 

H>

< http://pythontutor.com >

반응형

'Python_Matter > Solve' 카테고리의 다른 글

Python Learn the basics Quiz 63  (0) 2019.06.14
Python Learn the basics Quiz 62  (0) 2019.06.14
Python Learn the basics Quiz 60  (0) 2019.06.12
Python Learn the basics Quiz 59  (0) 2019.06.12
Python Learn the basics Quiz 58  (0) 2019.06.12