Python Golfing Suggestions [closed]

I have read: Tips for golfing in Python

I would like to golf this code further. Any more tips?

x,y,q,w=[int(i) for i in raw_input().split()]
while 1:
a=int(input())
s=d=""
if q>x:
s="W"
q-=1
elif q<x:
s="E"
q+=1
if w>y:
d="N"
w-=1
elif w<y:
d="S"
w+=1
print d+s

The program: You move on a map which is 40 wide by 18 high. Thor is initially placed on a random spot on the map and must reach the light of power as quickly as possible. You need to have the shortest code possible.

During each turn, you must specify in which direction to move, from one of the following:

N (North) NE (North-East) E (East) SE (South-East) S (South) SW (South-West) W (West) NW (North-West)

YOU WIN WHEN THOR REACHES THE LIGHT OF POWER.

YOU LOSE: if Thor moves off the map. if Thor doesn't have enough energy left to reach the light of power.

EXAMPLE: THOR STARTS ON THE MAP ON THE SQUARE (3, 6). THE LIGHT OF POWER IS ON SQUARE (3, 8).

TURN 1 THOR MOVES SOUTHWARDS. NEW POSITION = (3, 7). THOR HAS ENOUGH ENERGY LEFT FOR 10 MOVES

TURN 2 THOR MOVES SOUTHWARDS. NEW POSITION = (3, 8). THOR HAS ENOUGH ENERGY LEFT FOR 9 MOVES

The program must first read the initialization data from standard input. Then, within an infinite loop, read the data from the standard input related to Thor's current state and provide to the standard output Thor's movement instructions.

INITIALIZATION INPUT: Line 1: 4 integers indicate the position of the light of power. 2 indicate Thor’s starting position.

INPUT FOR ONE GAME TURN: Line 1: The level of Thor’s remaining energy. This represents the number of moves he can still make.

OUTPUT FOR ONE GAME TURN: A single line providing the move to be made: N NE E SE S SW W or NW

Edit : it's a code golf on codingame.com

closed as unclear what you're asking by feersum, flawr, Blue, ASCIIThenANSI, steveOct 3 '15 at 15:51

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• To give you golfing tips, we need to know what your code is supposed to achieve. For example, a=int(input()) strikes me as odd, since you don't use a anywhere. – Dennis Oct 3 '15 at 4:56
• It is the remaining energy to see if I can move or not, I guess I never run out of energy in the simple test. – marsh Oct 3 '15 at 5:18
• The description hardly matches the code. – feersum Oct 3 '15 at 5:19
• x,y,q,w=map(int,raw_input().split()) saves 9 characters – TessellatingHeckler Oct 3 '15 at 7:11

N.B.: This may not be entirely correct as I don't know what your code is supposed to do

The first thing to do is remove unnecessary whitespace, especially that after the if statements:

x,y,q,w=[int(i) for i in raw_input().split()]
while 1:
a=int(input())
s=d=""
if q>x:s="W";q-=1
elif q<x:s="E";q+=1
if w>y:d="N";w-=1
elif w<y:d="S";w+=1
print d+s

This golfs your code down by 4 bytes.

What would be ideal for this challenge is a switch case which, unfortunaley, does not exist in Python. Instead we can be clever with an array's index:

x,y,q,w=[int(i) for i in raw_input().split()]
while 1:
a=int(input())
s=d=""
exec(['s="W";q-=1','s="E";q+=1'][q<x])
exec(['d="N";w-=1','d="S";w+=1'][w<y])
print d+s

Saving you x bytes.

Finally you can get rid of all whitespace now:

x,y,q,w=[int(i) for i in raw_input().split()]
while 1:a=int(input());s=d="";exec(['s="W";q-=1','s="E";q+=1'][q<x]);exec(['d="N";w-=1','d="S";w+=1'][w<y]);print d+s
• I think you have a bug where you don't allow q==x or w==y to do nothing; you always move Thor in a diagonal, never in a horizontal or vertical. – TessellatingHeckler Oct 3 '15 at 7:12
• if q>x:s="W";q-=1, I though I tried this? Doesnt it need a tab character? – marsh Oct 3 '15 at 13:45
• @TessellatingHeckler Yeah, that's a major glaw :P – Beta Decay Oct 3 '15 at 18:51
• You did not get rid of the space in int(i) for... – Jonathan Frech May 9 '18 at 21:45
• @Jonathan I can't remember exactly, but I think removing that space breaks the code for some reason – Beta Decay May 10 '18 at 12:13