Skip to content

Commit

Permalink
Pascal coordinate calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
omrihq committed May 28, 2015
1 parent 02e3c9f commit 0838ffd
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions Hard Challenges/8PascalCoordinates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#Write a program that will take coordinates, and tell you the corresponding number in pascals triangle. For example:
#Input: 1, 1
#output:1
#=======
#input: 4, 2
#output: 6
#=======
#input: 1, 19
#output: error/nonexistent/whatever
import re

def factorial(n):
if n == 0:
return 1
else:
fac = 1
for i in xrange(1,n+1):
fac*=i
return fac

#If it is row 4, column 2, we want 4 choose 2
#The formula for this combination is (row!)/((row!)*(row-column)!)
#Also, remember that the first row/column in the triangle is row/column 0, not 1

def prompt():
print "Hello, this will give you the number of a coordinate in pascals triangle"
coordinates = raw_input("Enter the coordinates as follows (row, column) ")
coordinates = re.findall('\d+', coordinates)
return coordinates

def calculate_number(coordinates):
row = int(coordinates[0])
column = int(coordinates[1])
pascals_number = (factorial(row)/((factorial(column))*factorial(row-column)))
if pascals_number < 1:
return -1
return pascals_number

def main():
coords = prompt()
number = calculate_number(coords)
if number > 0:
print number
else:
print "Non-existant"

if __name__ == '__main__':
main()

0 comments on commit 0838ffd

Please sign in to comment.