Dev Ops/Algorithm Python checkio 풀이1
  • 728x90
    반응형




    Python checkio Chapter .1





    The Most Wanted Letter



    본문


    텍스트 문자열을 분석하여 가장 빈번히 사용되는 문자를 반환한다. 


    규칙

    1. 빈번히 사용되는 문자가 없을 경우, 가장 낮은 순번의 문자를 반환한다.

    2. 텍스트 문자는 대/소 문자의 구분이 없으며 반환되는 문자는 소문자로 한다.

    3. 기호 나 공백 문자는 분석하지 않는다.


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    def checkio(text):
        text = text.lower()
        count = 0
        retText = ''
     
        for i in range(len(text)):
            if (ord(text[i]) > 96and (ord(text[i]) < 123):    # 기호나 공백 문자는 포함하지 않음
                if retText != text[i]:                          # 현재 Text 와 이전 Text 가 다를 때
                    currentCount = text.count(text[i])          # Text 개수 카운팅
     
                    if count == currentCount:                   # 현재 Text 개수와 이전 Text 개수가 같을때
                        if retText > text[i]:                   # 현재 Text ASCII 번호가 이전 ASCII 번호보다 작을때
                            retText = text[i]                   # Text 갱신
                    elif count < currentCount:                  # 현재 Text 개수가 이전 Text 개수보다 클때 (갱신)
                        count = currentCount                    # Text 개수 갱신
                        retText = text[i]                       # Text 갱신
     
        #replace this for solution
        return retText
     
    if __name__ == '__main__':
        #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.")
     
    cs


     assert  는 예외를 발생 시키며 에러가 발생되면 뒤에 따라오는 문자열을 에러로 발생시킨다.


    결과는 다음과 같다.



    checkio에서 이 문제를 단 두줄만에 해결한 사람의 코드는 아래와 같다.





    Non-unique Elements



    본문


    리스트로 작성된 값 중 비-고유한 숫자만 반환하고, 고유한 숫자는 삭제한다. 


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    #Your optional code here
    #You can import some modules or create additional functions
    def checkio(data):
        #Your code here
        #It's main function. Don't remove this function
        #It's used for auto-testing and must return a result for check.
        i = 0
     
        while True:
            loopCnt = int(len(data))
            if i < loopCnt:
                count = data.count(data[i])
                if count == 1:
                    del data[i]
            else:
                break
     
            if count == 1:
                i = 0
            else:
                i += 1
        #replace this for solution
        return data
     
    #Some hints
    #You can use list.count(element) method for counting.
    #Create new list with non-unique elements
    #Loop over original list
    if __name__ == "__main__":
        #These "asserts" using only for self-checking and not necessary for auto-testing
        assert list(checkio([12313])) == [1313], "1st example"
        assert list(checkio([12345])) == [], "2nd example"
        assert list(checkio([55555])) == [55555], "3rd example"
        assert list(checkio([109101098])) == [10910109], "4th example"
        print("It is all good. Let's check it now")
    cs


    결과는 아래와 같다.



    checkio 에서 한줄로 해결한 사람의 코드는 아래와 같다.






























    728x90
    반응형

    'Dev Ops > Algorithm' 카테고리의 다른 글

    Python checkio 문제풀이4  (0) 2017.08.07
    Python checkio 문제풀이 3  (0) 2017.07.28
    Python checkio 풀이2  (0) 2017.07.27
    Python Fizz-Buzz 문제풀이  (0) 2017.07.26
    구구단 소스코드  (0) 2017.07.18
상단으로