Skip to content

Instantly share code, notes, and snippets.

@aldraco
Created October 1, 2015 20:40
Show Gist options
  • Save aldraco/8864ee2566b487624b00 to your computer and use it in GitHub Desktop.
Save aldraco/8864ee2566b487624b00 to your computer and use it in GitHub Desktop.
compare points problem on codeeval
import sys
test_cases = open(sys.argv[1], 'r')
def north_south(src, dest):
if src > dest:
return "S"
elif src < dest:
return "N"
else:
return ""
def east_west(src, dest):
if src > dest:
return "W"
elif src < dest:
return "E"
else:
return ""
for test in test_cases:
#ignore if test is an empty line
points = test.strip().split(' ')
bob, viewpoint = (points[0], points[1]), (points[2], points[3])
print (north_south(bob[0], viewpoint[0]) + east_west(bob[1], viewpoint[1])) or 'here'
test_cases.close()
@aldraco
Copy link
Author

aldraco commented Oct 1, 2015

I don't feel this is very Pythonic. I originally had the function defs lower in the file, but the program complained that it didn't know what 'north_south' was so clearly they aren't hoisted like in JS?

Also, the two functions are so similar they should be re-written to something DRYer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment