본문 바로가기
프로그래밍 문제/[Python] CheckIO

[CheckIO] pawn-brotherhood

by UltraLowTemp-Physics 2021. 1. 2.
728x90

Problems: py.checkio.org/en/mission/pawn-brotherhood/

My solution:

def safe_pawns(pawns: set) -> int:
    
    position_data = list(pawns)
    
    def check_safe(position):
        x,y = list(position)
        defence_x1 = chr(ord(x) - 1)
        defence_x2 = chr(ord(x) + 1)
        defence_y  = int(y) - 1
        defence_pos1 = defence_x1 + str(defence_y)
        defence_pos2 = defence_x2 + str(defence_y)
        
        if defence_pos1 in pawns or defence_pos2 in pawns: return 1 
        else: return 0 
    
    return sum(map(check_safe, position_data))

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
    print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")

 

A good solution that I see 

def getdiags(pawn):
    c, r = map(ord, pawn)
    return chr(c - 1) + chr(r - 1), chr(c + 1) + chr(r - 1)

def safe_pawns(pawns):
    return len([p for p in pawns if any(d in pawns for d in getdiags(p))])
728x90

'프로그래밍 문제 > [Python] CheckIO' 카테고리의 다른 글

[CheckIO] Solve split list  (0) 2021.01.05
[CheckIO] Sun angle  (0) 2021.01.03
[CheckIO] sort array by element frequency  (0) 2020.12.30
[CheckIO] Second Index  (0) 2020.12.24
[CheckIO] Popular words  (0) 2020.12.23

댓글