Advertisement
makispaiktis

Project Euler 15 - Lattice Paths

Aug 21st, 2020 (edited)
1,203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | None | 0 0
  1. '''
  2. Starting in the top left corner of a 2×2 grid, and only being able
  3. to move to the right and down, there are exactly 6 routes to the bottom right corner.
  4. '''
  5.  
  6. # The solution is a string made up with 40 letters, but we have to use
  7. # exactly 20 '0's ---> right move and exactly 20 '1's ---> down move
  8. # So, we have n = 40 letters and two teams with n1 = 20 letters and n2 = 20
  9. # To produce all the combinations, I use the formula: combinations = n! / (n1! * n2!),
  10. # where n = n1 + n2
  11.  
  12. def factorial(n):
  13.     if n < 0 or n != int(n):
  14.         print("Error using the factorial function.")
  15.         return -1000
  16.     elif n == 0 or n == 1:
  17.         return 1
  18.     else:
  19.         fact = list()
  20.         fact.append(1)
  21.         for i in range(1, n+1):
  22.             fact.append(i * fact[i-1])
  23.         return fact[n]
  24.  
  25.  
  26. def produceCombinationsOfTwoTeams(n, n1, n2):
  27.     if n == n1 + n2 and n == int(n) and n1 == int(n1) and n2 == int(n2) and n > 0 and n1 > 0 and n2 > 0:
  28.         return factorial(n) / factorial(n1) / factorial(n2)
  29.     else:
  30.         print("Error using the fucntion produceCombinationsOfTwoTeams")
  31.         return -1000
  32.  
  33. # MAIN FUNCTION
  34. print(produceCombinationsOfTwoTeams(40, 20, 20))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement