Advertisement
benzid_wael

Grid walk problem

Jun 2nd, 2014
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3.  
  4. def sum_of_digits(number):
  5.     """
  6.    Calculate the sum of the digits of the specified parameter.
  7.    """
  8.     return sum(map(int, str(number)))
  9.  
  10.  
  11. def is_accessible_point(x, y):
  12.     """
  13.    Verify if the given point is accessible to the monkey or not.
  14.    """
  15.     return (sum_of_digits(abs(x)) + sum_of_digits(abs(y))) <= 19
  16.  
  17.  
  18. def append_neighbors(points, x, y):
  19.     """
  20.    Append accessible neighbors to the accessible points `points`
  21.    if does not exists.
  22.    """
  23.     for (i, j) in ((x+1, y), (x, y+1), (x-1, y), (x, y-1)):
  24.         # print "%d, %d" % (i, j)
  25.         if is_accessible_point(i, j) and ((i, j) not in points):
  26.             points.append((i, j))
  27.  
  28.  
  29. if __name__ == '__main__':
  30.     points = [(0, 0)]
  31.     i = 0
  32.     while i < len(points):
  33.         append_neighbors(points, points[i][0], points[i][1])
  34.         i += 1
  35.     print "Number of accessible points is: %d" % len(points)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement