Skip to content

Instantly share code, notes, and snippets.

@flavienbwk
Created January 24, 2019 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flavienbwk/8da8542a64c65cad8905e6dbeeeccbfb to your computer and use it in GitHub Desktop.
Save flavienbwk/8da8542a64c65cad8905e6dbeeeccbfb to your computer and use it in GitHub Desktop.
Coding interview problem : king and queen on a chessboard.
# 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