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




    Python checkio Chapter .2




    House Password



    본문


    패스워드가 들어 왔을 때 패스워드의 길이 및 대/소문자 존재 여부 및 숫자 존재 여부를 판단하여 올바른 패스워드를 작성하였는지 판단하는 프로그램을 작성한다.


    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
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    checkio(data):
        ret = True
     
        if len(data) < 10:
            ret = False
     
        if ret != False:
            for i in range(len(data)):
                if data[i].isdigit() == True:
                    ret = True
                    break
                else:
                    ret = False
     
        if ret != False:
            for i in range(len(data)):
                if data[i].islower() == True:
                    ret = True
                    break
                else:
                    ret = False
     
        if ret != False:
            for i in range(len(data)):
                if data[i].isupper() == True:
                    ret = True
                    break
                else:
                    ret = False
     
        #replace this for solution
        return ret
     
    #Some hints
    #Just check all conditions
     
     
    if __name__ == '__main__':
        #These "asserts" using only for self-checking and not necessary for auto-testing
        assert checkio('A1213pokl'== False, "1st example"
        assert checkio('bAse730onE4'== True, "2nd example"
        assert checkio('asasasasasasasaas'== False, "3rd example"
        assert checkio('QWERTYqwerty'== False, "4th example"
        assert checkio('123456123456'== False, "5th example"
        assert checkio('QwErTy911poqqqq'== True, "6th example"
     
    cs


     str.isdigit( )  문자열에 숫자가 있는지 없는지 판단하여 bool 타입으로 리턴해주는 함수이다.


     str.islower( )  문자열에 소문자가 있는지 없는지 판단하여 bool 타입으로 리턴해주는 함수이다.


     str.isupper( )  문자열에 대문자가 있는지 없는지 판단하여 bool 타입으로 리턴해주는 함수이다.


    다음은 1등 한사람의 코드이다.



     str.isalpha( )  문자열에 알파벳이 있는지 없는지를 판단하여 bool 타입으로 리턴해주는 함수이다.


    이 조건을 위 코드에 추가해 주면 좋을 듯 하다.




    Monkey Typing



    본문


    문장과 단어를 입력받아 문장안에 해당 단어가 있으면 그 개수를 반환하는 프로그램을 작성한다.


    규칙

    1. 대/소문자를 구분하지 않는다.

    2. 입력 받은 단어는 단어전체중의 일부분이 될 수있다.

    3. 입력 받은 단어가 반복될 경우 한번만 계산한다.


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    def count_words(text, words):
        ret = 0
        for word in words:
            if word in text.lower():
                ret += 1
     
        return ret
     
    if __name__ == '__main__':
        #These "asserts" using only for self-checking and not necessary for auto-testing
        assert count_words("How aresjfhdskfhskd you?", {"how""are""you""hello"}) == 3"Example"
        assert count_words("Bananas, give me bananas!!!", {"banana""bananas"}) == 2"BANANAS!"
        assert count_words("Lorem ipsum dolor sit amet, consectetuer adipiscing elit.",
                           {"sum""hamlet""infinity""anything"}) == 1"Weird text"
    cs


    다음은 1등 유저의 코드이다.




    Xs and Os Referee



    본문


    수평 수직 또는 대각선 행 을 차지한 X 또는 O 에게 wins 판정을 해주는 게임 심판 프로그램을 작성한다.


    규칙

    1. X 선수가 이기면 "X"를 반환한다.

    2. O 선수가 이기면 "O"를 반환한다. 

    3. 무승부인 경우 "D"를 반환한다.

    4. "."은 빈 셀이다.


    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
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    def checkio(game_result):
        xWinCnt = 0
        yWinCnt = 0
        if game_result[0][0== game_result[1][1== game_result[2][2and game_result[0][0!= ".":
            if game_result[0][0== "X":
                xWinCnt += 1
            elif game_result[0][0== "O":
                yWinCnt += 1
     
        if game_result[0][2== game_result[1][1== game_result[2][0and game_result[0][2!= ".":
            if game_result[0][2== "X":
                xWinCnt += 1
            elif game_result[0][2== "O":
                yWinCnt += 1
     
        for row in game_result:
            if row[0== row[1== row[2and row != ".":
                if row[0== "X":
                    xWinCnt += 1
                elif row[0== "O":
                    yWinCnt += 1
     
        game_result = list(zip(*game_result))
        for col in game_result:
            if col[0== col[1== col[2and col != ".":
                if col[0== "X":
                    xWinCnt += 1
                elif col[0== "O":
                    yWinCnt += 1
     
        if xWinCnt > yWinCnt:
            result = "X"
        elif xWinCnt < yWinCnt:
            result = "O"
        else:
            result = "D"
     
        return result
     
    if __name__ == '__main__':
        #These "asserts" using only for self-checking and not necessary for auto-testing
        assert checkio([
            "X.O",
            "XX.",
            "XOO"]) == "X""Xs wins"
        assert checkio([
            "OO.",
            "XOX",
            "XOX"]) == "O""Os wins"
        assert checkio([
            "OOX",
            "XXO",
            "OXX"]) == "D""Draw"
        assert checkio([
            "O.X",
            "XX.",
            "XOO"]) == "X""Xs wins again"
     

    cs


    짧은 코딩으로 해결한 유저의 코드이다.




    Pawn Brotherhood


    본문


    아래그림과 같이 safe 존에 위치한 자표 개수를 합하여 반환하는 프로그램을 작성한다.



    규칙

    1. 배치 줄 좌표 문자열의 집합으로 입력받는다.

    2. 정수로 반환한다.


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    def safe_pawns(pawns):
        pawns_indexes = set()
        for p in pawns:
            row = int(p[1])-1
            col = ord(p[0])-97
            pawns_indexes.add((row, col))
        count = 0
        for row, col in pawns_indexes:
            is_safe = ((row-1, col-1in pawns_indexes) or ((row-1, col+1in pawns_indexes)
            if is_safe:
                count += 1
     
        return count
     
    if __name__ == '__main__':
        #These "asserts" using only for self-checking and not necessary for auto-testing
        assert safe_pawns({"b4""d4""f4""c3""e3""g5""d2"}) == 6
        assert safe_pawns({"b4""c4""d4""e4""f4""g4""e5"}) == 1
     
    cs


    짧은 코딩으로 해결한 유저의 코드이다.




    Min and Max



    본문


    min, max 함수를 사용하지 않고 같은 기능을 하는 함수를 구현해 보자.


    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
    36
    37
    38
    39
    40
    41
    def min(*args, **kwargs):
        key = kwargs.get("key", None)

        if len(args) == 1:
            args = list(args[0])
     
        result = args[0]
     
        for i in range(1len(args)):
     
            if key != None and key(result) > key(args[i]):
                result = args[i]
            elif key == None and result > args[i] :
                result = args[i]
     
        return result
     
    def max(*args, **kwargs):
        key = kwargs.get("key", None)
     
        if len(args) == 1:
            args = list(args[0])
     
        result = args[0]
        for i in range(1len(args)):
            if key != None and key(args[i]) > key(result):
                result = args[i]
            elif key == None and args[i] > result:
                result = args[i]
     
        return result
     
    if __name__ == '__main__':
        #These "asserts" using only for self-checking and not necessary for auto-testing
        assert max(32== 3"Simple case max"
        assert min(32== 2"Simple case min"
        assert max([12034]) == 4"From a list"
        assert min("hello"== "e""From string"
        assert max(2.25.65.9, key=int== 5.6"Two maximal items"
        assert min([[12], [34], [90]], key=lambda x: x[1]) == [90], "lambda key"
     
    cs


    다음은 다른 유저의 코드이다. 







    728x90
    반응형

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

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