Created
January 24, 2019 17:16
-
-
Save flavienbwk/8da8542a64c65cad8905e6dbeeeccbfb to your computer and use it in GitHub Desktop.
Coding interview problem : king and queen on a chessboard.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Given a chessboard, how would you find whether the king is threatened by the queen ? | |
# | |
# King position : kx, ky | |
# Queen position : qx, qy | |
# | |
# There's only a queen and a king on the chessboard. | |
# Output is True or False. | |
def endangeredKing(k_pos, q_pos): | |
if (q_pos[0] - k_pos[0] == 0): | |
return True | |
slope = abs((q_pos[1] - k_pos[1]) / (q_pos[0] - k_pos[0])) | |
return True if (slope == 1 or slope == 0) else False | |
print endangeredKing((2, 2), (5, 5)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment