5
I need to reduce the length of this code in Python 3 as much as possible (even if it will be less readable):
a,b,x,y=[int(i) for i in input().split()]
while 1:
r=''
if y<b:r='S';y+=1
if y>b:r='N';y-=1
if x<a:r+='E';x+=1
if x>a:r+='W';x-=1
print(r)
It's a map: you are on (x,y) and you need to go to (a,b). S for South, N for North, NE for North East and so on. After each turn I must tell where to go using print.
Update: This is what I've got now, but is there any way to shorten it further?
a,b,x,y=map(int,input().split())
while 1:c,d=(y>b)-(y<b),(x>a)-(x<a);print((' NS'[c]+' WE'[d]).strip());y-=c;x-=d
2This never terminates. Is that OK? – xnor – 2015-05-07T19:08:03.743
Yes. In fact, it will automatically end when x=a and y=b – xaxa – 2015-05-07T19:11:24.363
How strict is the form of the input? For example if you are allowed to take input as
"[4,5,1,2]"
you could change the first line to a much shortera,b,x,y=input()
– KSab – 2015-05-08T13:13:06.203it's really too bad they got rid of
cmp()
in python 3, this is a perfect use case. – sirpercival – 2015-05-09T11:35:22.920