Hackbright Code Challenges

Check Detection: Solution

Check Detection: Solution

Problem

Check Detection

Whiteboard

Harder

Challenge

Medium

Concepts

Data Structures, General

Download

check-solution.zip


def check(king, queen):
    """Given a chessboard with one K and one Q, see if the K can attack the Q.

    This function is given coordinates for the king and queen on a chessboard.
    These coordinates are given as a letter A-H for the columns and 1-8 for the
    row, like "D6" and "B7":
    """

    # START SOLUTION

    king_col = col_to_num(king[0])
    king_row = int(king[1])

    queen_col = col_to_num(queen[0])
    queen_row = int(queen[1])

    # Easy check: on same row or column

    if king_col == queen_col or king_row == queen_row:
        return True

    # Diagonal check

    return abs(king_col - queen_col) == abs(king_row - queen_row)