Last active
December 15, 2015 14:49
-
-
Save fcamel/5277193 to your computer and use it in GitHub Desktop.
A strait-forward implementation for the problem: http://www.careercup.com/question?id=14968041
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
# Problem description: http://www.careercup.com/question?id=14968041 | |
def find_coil(n, x, y, dxs, dys): | |
nsteps = [] | |
for nstep in xrange(2, n - 1, 2): | |
nsteps.append(nstep) | |
nsteps.append(nstep) | |
nsteps.append(nstep + 1) | |
rs = [y * n + x + 1] | |
idx = 0 | |
for nstep in nsteps: | |
for _ in xrange(nstep): | |
x += dxs[idx] | |
y += dys[idx] | |
rs.append(y * n + x + 1) | |
idx = (idx + 1) % len(dxs) | |
return rs | |
def coil(n): | |
# find coils | |
print find_coil(4 * n, 2 * n - 1, 2 * n, [0, 1, 0, -1], [-1, 0, 1, 0]) | |
print find_coil(4 * n, 2 * n, 2 * n - 1, [0, -1, 0, 1], [1, 0, -1, 0]) | |
coil(2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment