Created
July 16, 2018 19:19
-
-
Save TomColBee/e3d81bf068efacf1bb902f6fe69b65fe to your computer and use it in GitHub Desktop.
Coderbyte Python Challenge: ChessboardTravelling
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
# Function input is a string that are the coordinates of a space on a 8x8 | |
# chess board. The structure of str will be (x y)(a b) where (x y) is the | |
# current position and (a b) is the new position. | |
# a and b will be strictly greater than x and y respectively. | |
# Program will determine the number of ways there are of travelling from | |
# (x y) to (a b) by only travelling up and right. | |
def ChessboardTraveling(str): | |
# import math to get factorial function | |
import math | |
# convert the string into integer to define x, y, a and b | |
x = int(str[1]) | |
y = int(str[3]) | |
a = int(str[6]) | |
b = int(str[8]) | |
# make sure and b is greater than x and y | |
if (a <= x) or (b <= y): | |
print "Please provide us coorindates where a > x and b > y such that str = (x y)(a b)..." | |
# create var solutions to track the number of solutions available | |
solutions = 0 | |
# define differences, if differene is 2 then solutions is 2q | |
# otherwise get nth solutions | |
if a > x and b > y: | |
diff = (a-x) + (b-y) | |
diff_a = a - x | |
diff_b = b - y | |
if diff == 2: | |
solutions = 2 | |
else: | |
solutions = math.factorial(diff_a+diff_b) / (math.factorial(diff_a) * math.factorial(diff_b)) | |
return int(solutions) | |
print ChessboardTraveling(raw_input()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment