Skip to content

Instantly share code, notes, and snippets.

@mahendrakalkura
Created November 25, 2014 09:21
Show Gist options
  • Save mahendrakalkura/699fa243149bb9ac4c0f to your computer and use it in GitHub Desktop.
Save mahendrakalkura/699fa243149bb9ac4c0f to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
def get_next_direction(direction):
if direction == 'North':
return 'East'
if direction == 'East':
return 'South'
if direction == 'South':
return 'West'
if direction == 'West':
return 'North'
return 'North'
def solution(A):
co_ordinates = []
co_ordinate = [0, 0]
direction = 'North'
for index, steps in enumerate(A):
for _ in xrange(0, steps):
co_ordinates.append(co_ordinate[:])
if direction == 'North':
co_ordinate[1] += 1
if direction == 'East':
co_ordinate[0] += 1
if direction == 'West':
co_ordinate[0] -= 1
if direction == 'South':
co_ordinate[1] -= 1
if co_ordinate in co_ordinates:
return index + 1
direction = get_next_direction(direction)
return 0
print solution([
1,
3,
2,
5,
4,
4,
6,
3,
2,
])
print solution([
2,
2,
1,
1,
2,
2,
1,
1,
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment